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.
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.
10 11 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 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/capybara/mechanize/browser.rb', line 10 def initialize(driver) @agent = ::Mechanize.new @agent.redirect_ok = false @agent.user_agent = default_user_agent #@agent.agent.max_history = 0 if !driver..empty? if driver.[:certificate] if !driver.[:certificate].is_a? Hash @agent.cert = driver.[:certificate].certificate @agent.key = driver.[:certificate].key elsif driver.[:certificate][:key] && driver.[:certificate][:cer] @agent.cert = driver.[:certificate][:cer] @agent.key = driver.[:certificate][:key] end if driver.[:ca] @agent.ca_file = driver.[:ca] end end if driver.[:user_agent] @agent.user_agent = driver.[:user_agent] end if driver.[:username] and driver.[:password] @agent.basic_auth(driver.[:username], driver.[:password]) 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.
180 181 182 |
# File 'lib/capybara/mechanize/browser.rb', line 180 def agent @agent end |
Instance Method Details
#current_url ⇒ Object
57 58 59 |
# File 'lib/capybara/mechanize/browser.rb', line 57 def current_url last_request_remote? ? remote_response.current_url : super end |
#delete(path, attributes = {}, headers = {}) ⇒ Object
144 145 146 |
# File 'lib/capybara/mechanize/browser.rb', line 144 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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/capybara/mechanize/browser.rb', line 104 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
72 73 74 75 76 77 78 |
# File 'lib/capybara/mechanize/browser.rb', line 72 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
65 66 67 68 69 70 |
# File 'lib/capybara/mechanize/browser.rb', line 65 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
129 130 131 |
# File 'lib/capybara/mechanize/browser.rb', line 129 def get(path, attributes = {}, headers = {}) process_without_redirect(:get, path, attributes, headers) end |
#last_response ⇒ Object
61 62 63 |
# File 'lib/capybara/mechanize/browser.rb', line 61 def last_response last_request_remote? ? remote_response : super end |
#post(path, attributes = {}, headers = {}) ⇒ Object
134 135 136 |
# File 'lib/capybara/mechanize/browser.rb', line 134 def post(path, attributes = {}, headers = {}) process_without_redirect(:post, path, post_data(attributes), headers) end |
#post_data(params) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/capybara/mechanize/browser.rb', line 148 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
80 81 82 83 84 |
# File 'lib/capybara/mechanize/browser.rb', line 80 def process(method, path, *) reset_cache! send(method, path, *) follow_redirects! end |
#process_without_redirect(method, path, attributes, headers) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/capybara/mechanize/browser.rb', line 86 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
139 140 141 |
# File 'lib/capybara/mechanize/browser.rb', line 139 def put(path, attributes = {}, headers = {}) process_without_redirect(:put, path, attributes, headers) end |
#racktest_delete ⇒ Object
143 |
# File 'lib/capybara/mechanize/browser.rb', line 143 alias :racktest_delete :delete |
#racktest_get ⇒ Object
128 |
# File 'lib/capybara/mechanize/browser.rb', line 128 alias :racktest_get :get |
#racktest_post ⇒ Object
133 |
# File 'lib/capybara/mechanize/browser.rb', line 133 alias :racktest_post :post |
#racktest_put ⇒ Object
138 |
# File 'lib/capybara/mechanize/browser.rb', line 138 alias :racktest_put :put |
#remote?(url) ⇒ Boolean
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/capybara/mechanize/browser.rb', line 166 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
51 52 53 54 55 |
# File 'lib/capybara/mechanize/browser.rb', line 51 def reset_host! @last_remote_uri = nil @last_request_remote = nil super end |