Class: Capybara::Driver::Mechanize

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey-patches/capybara-mechanize-patches.rb

Defined Under Namespace

Classes: FakeURI

Instance Method Summary collapse

Instance Method Details



76
77
78
79
80
81
82
83
84
# File 'lib/monkey-patches/capybara-mechanize-patches.rb', line 76

def add_cookie(attribs)
  c = Mechanize::Cookie.new(attribs[:name],attribs[:value])
  # remember: mechanize always removes leading '.' from domains
  c.domain = attribs[:domain].sub!(/^./, '')
  c.path = '/'
  c.expires = attribs[:expires]
  c.secure = attribs[:secure]
  agent.cookie_jar.add(FakeURI.new(c.domain),c)
end


55
56
57
# File 'lib/monkey-patches/capybara-mechanize-patches.rb', line 55

def cookie_named(name)
  cookies.find { |c| c[:name] == name }
end

#cookiesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/monkey-patches/capybara-mechanize-patches.rb', line 35

def cookies
  cookies = []
  
  agent.cookie_jar.jar.each do |domain|
    domain[1].each do |path|
      path[1].each do |cookie|
        cookies.push({
          :name => cookie[1].name,
          :value => cookie[1].value,
          :domain => cookie[1].domain,
          :secure => cookie[1].secure,
          :expires => cookie[1].expires,
          :path => cookie[1].path
        })
      end
    end
  end
  cookies
end

#delete_all_cookiesObject



71
72
73
# File 'lib/monkey-patches/capybara-mechanize-patches.rb', line 71

def delete_all_cookies
  agent.cookie_jar.clear!
end


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/monkey-patches/capybara-mechanize-patches.rb', line 59

def delete_cookie(cookie_name)
  agent.cookie_jar.jar.each do |domain|
    domain[1].each do |path|
      path[1].each do |cookie|
        if cookie[0] == cookie_name
          agent.cookie_jar.jar[domain[0]][path[0]].delete(cookie[0])
        end
      end
    end
  end
end

#process_remote_request(method, url, *options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/monkey-patches/capybara-mechanize-patches.rb', line 5

def process_remote_request(method, url, *options)
  if remote?(url)
    remote_uri = URI.parse(url)
  
    @scheme = remote_uri.scheme if remote_uri.scheme 

    if remote_uri.host.nil?
      #TODO: Ascertain whether this is really true...
      if(method == :post && url == "" && @prev_url) #patch
        #require 'uri'
        #url = "http://#{URI.parse(@prev_url).host}#{URI.parse(@prev_url).path}"
        #p url
        url = @prev_url #patch
      else
        remote_host = @last_remote_host || Capybara.app_host || Capybara.default_host
        url = File.join(remote_host, url)
        #url = "http://#{url}" unless url.include?("http")
        url = "#{@scheme}://#{url}" unless url.match(/^http.*/)
      end
    else
      @last_remote_host = "#{remote_uri.host}:#{remote_uri.port}"
    end
    @prev_url = url #patch
    reset_cache
    @agent.send *( [method, url] + options)

    @last_request_remote = true
  end
end