diff --git a/lib/capybara/rack_test/browser.rb b/lib/capybara/rack_test/browser.rb index 3f1d5386..b32fdeaa 100644 --- a/lib/capybara/rack_test/browser.rb +++ b/lib/capybara/rack_test/browser.rb @@ -26,7 +26,15 @@ class Capybara::RackTest::Browser def follow_redirects! 5.times do - follow_redirect! if last_response.redirect? + if last_response.redirect? + new_uri = URI.parse(last_response.original_headers['Location']) + + if new_uri.host + @current_host = new_uri.scheme + '://' + new_uri.host + end + + follow_redirect! + end end raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect? end @@ -38,17 +46,17 @@ class Capybara::RackTest::Browser 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('/') path = (folders[0, folders.size - 1] << path).join('/') end path = current_host + path end - + reset_cache! send(method, path, attributes, env) follow_redirects! diff --git a/lib/capybara/spec/session/current_host_spec.rb b/lib/capybara/spec/session/current_host_spec.rb index 6b76a2a3..f872cd4d 100644 --- a/lib/capybara/spec/session/current_host_spec.rb +++ b/lib/capybara/spec/session/current_host_spec.rb @@ -58,5 +58,11 @@ shared_examples_for "current_host" do @session.body.should include('Current host is http://capybara2.elabs.se') @session.current_host.should == 'http://capybara2.elabs.se' end + + it "is affected by following a redirect" do + @session.visit('http://capybara-testapp.heroku.com/redirect_secure') + @session.body.should include('Current host is https://capybara-testapp.heroku.com') + @session.current_host.should == 'https://capybara-testapp.heroku.com' + end end end diff --git a/lib/capybara/spec/test_app.rb b/lib/capybara/spec/test_app.rb index 124dfd44..e88712b0 100644 --- a/lib/capybara/spec/test_app.rb +++ b/lib/capybara/spec/test_app.rb @@ -67,6 +67,10 @@ class TestApp < Sinatra::Base redirect back end + get '/redirect_secure' do + redirect "https://#{request.host}/host" + end + get '/slow_response' do sleep 2 'Finally!'