Class: Capybara::Mechanize::Browser
- Inherits:
-
RackTest::Browser
- Object
- RackTest::Browser
- Capybara::Mechanize::Browser
- Extended by:
- Forwardable
- Defined in:
- lib/capybara/mechanize/browser.rb
Defined Under Namespace
Classes: ResponseProxy
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
Instance Method Summary collapse
- #current_url ⇒ Object
- #delete(path, attributes = {}, headers = {}) ⇒ Object
-
#determine_path(path) ⇒ Object
TODO path Capybara to move this into its own method.
- #follow_redirect! ⇒ Object
- #follow_redirects! ⇒ Object
- #get(path, attributes = {}, headers = {}) ⇒ Object
-
#initialize(driver) ⇒ Browser
constructor
A new instance of Browser.
- #last_response ⇒ Object
- #post(path, attributes = {}, headers = {}) ⇒ Object
- #post_data(params) ⇒ Object
- #process(method, path, *options) ⇒ Object
- #process_without_redirect(method, path, attributes, headers) ⇒ Object
- #put(path, attributes = {}, headers = {}) ⇒ Object
- #racktest_delete ⇒ Object
- #racktest_get ⇒ Object
- #racktest_post ⇒ Object
- #racktest_put ⇒ Object
- #remote?(url) ⇒ Boolean
- #reset_host! ⇒ Object
Constructor Details
#initialize(driver) ⇒ Browser
Returns a new instance of Browser.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/capybara/mechanize/browser.rb', line 12 def initialize(driver) @agent = ::Mechanize.new @agent.redirect_ok = false @agent.user_agent = default_user_agent if !driver..empty? if driver.[:certificate] && driver.[:certificate][:key] && driver.[:certificate][:cer] @agent.cert = driver.[:certificate][:cer] @agent.key = driver.[:certificate][:key] end if driver.[:proxy] && !driver.[:proxy].empty? proxy = nil begin proxy = URI.parse(driver.[:proxy]) rescue URI::InvalidURIError => e raise "You have entered an invalid proxy address #{driver.[:proxy]}. Check proxy settings." end if proxy && proxy.instance_of?(URI::HTTP) @agent.set_proxy(proxy.host, proxy.port) else raise "ProxyError: You have entered an invalid proxy address #{driver.[:proxy]}. e.g. (http|https)://proxy.com(:port)" end end end super end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
167 168 169 |
# File 'lib/capybara/mechanize/browser.rb', line 167 def agent @agent end |
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
7 8 9 |
# File 'lib/capybara/mechanize/browser.rb', line 7 def driver @driver end |
Instance Method Details
#current_url ⇒ Object
44 45 46 |
# File 'lib/capybara/mechanize/browser.rb', line 44 def current_url last_request_remote? ? remote_response.current_url : super end |
#delete(path, attributes = {}, headers = {}) ⇒ Object
131 132 133 |
# File 'lib/capybara/mechanize/browser.rb', line 131 def delete(path, attributes = {}, headers = {}) process_without_redirect(:delete, path, attributes, headers) end |
#determine_path(path) ⇒ Object
TODO path Capybara to move this into its own method
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/capybara/mechanize/browser.rb', line 91 def determine_path(path) new_uri = URI.parse(path) current_uri = URI.parse(current_url) if new_uri.host @current_host = new_uri.scheme + '://' + new_uri.host end if new_uri.relative? path = request_path + path if path.start_with?('?') unless path.start_with?('/') folders = request_path.split('/') if folders.empty? path = '/' + path else path = (folders[0, folders.size - 1] << path).join('/') end end path = current_host + path end path end |
#follow_redirect! ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/capybara/mechanize/browser.rb', line 59 def follow_redirect! unless last_response.redirect? raise "Last response was not a redirect. Cannot follow_redirect!" end get(last_location_header) end |
#follow_redirects! ⇒ Object
52 53 54 55 56 57 |
# File 'lib/capybara/mechanize/browser.rb', line 52 def follow_redirects! 5.times do follow_redirect! if last_response.redirect? end raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect? end |
#get(path, attributes = {}, headers = {}) ⇒ Object
116 117 118 |
# File 'lib/capybara/mechanize/browser.rb', line 116 def get(path, attributes = {}, headers = {}) process_without_redirect(:get, path, attributes, headers) end |
#last_response ⇒ Object
48 49 50 |
# File 'lib/capybara/mechanize/browser.rb', line 48 def last_response last_request_remote? ? remote_response : super end |
#post(path, attributes = {}, headers = {}) ⇒ Object
121 122 123 |
# File 'lib/capybara/mechanize/browser.rb', line 121 def post(path, attributes = {}, headers = {}) process_without_redirect(:post, path, post_data(attributes), headers) end |
#post_data(params) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/capybara/mechanize/browser.rb', line 135 def post_data(params) params.inject({}) do |memo, param| case param when Hash param.each {|attribute, value| memo[attribute] = value } memo when Array case param.last when Hash param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value } else memo[param.first] = param.last end memo end end end |
#process(method, path, *options) ⇒ Object
67 68 69 70 71 |
# File 'lib/capybara/mechanize/browser.rb', line 67 def process(method, path, *) reset_cache! send(method, path, *) follow_redirects! end |
#process_without_redirect(method, path, attributes, headers) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/capybara/mechanize/browser.rb', line 73 def process_without_redirect(method, path, attributes, headers) path = @last_path if path.nil? || path.empty? if remote?(path) process_remote_request(method, path, attributes, headers) else register_local_request path = determine_path(path) reset_cache! send("racktest_#{method}", path, attributes, env.merge(headers)) end @last_path = path end |
#put(path, attributes = {}, headers = {}) ⇒ Object
126 127 128 |
# File 'lib/capybara/mechanize/browser.rb', line 126 def put(path, attributes = {}, headers = {}) process_without_redirect(:put, path, attributes, headers) end |
#racktest_delete ⇒ Object
130 |
# File 'lib/capybara/mechanize/browser.rb', line 130 alias :racktest_delete :delete |
#racktest_get ⇒ Object
115 |
# File 'lib/capybara/mechanize/browser.rb', line 115 alias :racktest_get :get |
#racktest_post ⇒ Object
120 |
# File 'lib/capybara/mechanize/browser.rb', line 120 alias :racktest_post :post |
#racktest_put ⇒ Object
125 |
# File 'lib/capybara/mechanize/browser.rb', line 125 alias :racktest_put :put |
#remote?(url) ⇒ Boolean
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/capybara/mechanize/browser.rb', line 153 def remote?(url) if Capybara.app_host true else host = URI.parse(url).host if host.nil? last_request_remote? else !Capybara::Mechanize.local_hosts.include?(host) end end end |
#reset_host! ⇒ Object
38 39 40 41 42 |
# File 'lib/capybara/mechanize/browser.rb', line 38 def reset_host! @last_remote_host = nil @last_request_remote = nil super end |