Preserve headers on redirects, closes #464

This commit is contained in:
Jonas Nicklas and Nicklas Ramhöj 2011-08-30 10:19:52 +02:00
parent 0858c26d94
commit 33b1a27c32
3 changed files with 14 additions and 10 deletions

View File

@ -12,29 +12,24 @@ class Capybara::RackTest::Browser
def visit(path, attributes = {})
reset_host!
process(:get, path, attributes)
follow_redirects!
end
def submit(method, path, attributes)
path = request_path if not path or path.empty?
process(method, path, attributes)
follow_redirects!
end
def follow(method, path, attributes = {})
return if path.gsub(/^#{request_path}/, '').start_with?('#')
process(method, path, attributes)
follow_redirects!
end
def follow_redirects!
5.times do
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
process(:get, last_response["Location"]) if last_response.redirect?
end
raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect?
end
@ -59,7 +54,6 @@ class Capybara::RackTest::Browser
reset_cache!
send(method, path, attributes, env)
follow_redirects!
end
def current_url

View File

@ -90,6 +90,10 @@ class TestApp < Sinatra::Base
env['HTTP_FOO']
end
get '/get_header_via_redirect' do
redirect '/get_header'
end
get '/:view' do |view|
erb view.to_sym
end

View File

@ -79,5 +79,11 @@ describe Capybara::RackTest::Driver do
@driver.find('.//input').first.click
@driver.body.should include('foobar')
end
it 'should keep headers on redirects' do
@driver = Capybara::RackTest::Driver.new(TestApp, :headers => {'HTTP_FOO' => 'foobar'})
@driver.visit('/get_header_via_redirect')
@driver.body.should include('foobar')
end
end
end