Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Have a better seperation between remote and local request and also fi…

…xing double redirects
  • Loading branch information...
commit c227f7ab0f3f0695028a523e4975d1985d52c834 1 parent 0659e86
@jeroenvandijk jeroenvandijk authored
Showing with 19 additions and 11 deletions.
  1. +19 −11 lib/capybara/driver/mechanize.rb
View
30 lib/capybara/driver/mechanize.rb
@@ -1,13 +1,13 @@
require 'mechanize'
class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
-
+
def initialize(*args)
super
@agent = ::Mechanize.new
@agent.redirect_ok = false
end
-
+
def visit(url)
get url
end
@@ -18,24 +18,25 @@ def cleanup!
end
def current_url
- (response_proxy && response_proxy.current_url) || super
+ last_request_remote? ? remote_response.current_url : super
end
def response
- response_proxy || super
+ last_request_remote? ? remote_response : super
end
-
+
# TODO see how this can be cleaned up
def follow_redirect!
unless response.redirect?
raise "Last response was not a redirect. Cannot follow_redirect!"
end
- if response.respond_to?(:page)
- location = response.page.response['Location']
- else
- location = response['Location']
- end
+ location = if last_request_remote?
+ remote_response.page.response['Location']
+ else
+ response['Location']
+ end
+
get(location)
end
@@ -60,17 +61,24 @@ def remote?(url)
private
+ def last_request_remote?
+ !!@last_request_remote
+ end
+
def process_remote_request(method, url, *options)
if remote?(url)
url = File.join((Capybara.app_host || Capybara.default_host), url) if URI.parse(url).host.nil?
reset_cache
@agent.send *( [method, url] + options)
+ @last_request_remote = true
follow_redirects!
true
+ else
+ @last_request_remote = false
end
end
- def response_proxy
+ def remote_response
ResponseProxy.new(@agent.current_page) if @agent.current_page
end
Please sign in to comment.
Something went wrong with that request. Please try again.