Change follow redirects into a driver option, closes #730

Thanks Thorben Schröder!
This commit is contained in:
Jonas Nicklas 2012-07-12 16:57:27 +02:00
parent bcae6bea02
commit 3b44c7da59
3 changed files with 29 additions and 4 deletions

View File

@ -33,10 +33,12 @@ class Capybara::RackTest::Browser
def process_and_follow_redirects(method, path, attributes = {}, env = {})
process(method, path, attributes, env)
Capybara.redirect_limit.times do
process(:get, last_response["Location"], {}, env) if last_response.redirect?
if driver.follow_redirects?
Capybara.redirect_limit.times do
process(:get, last_response["Location"], {}, env) if last_response.redirect?
end
raise Capybara::InfiniteRedirectError, "redirected more than #{Capybara.redirect_limit} times, check for infinite redirects." if last_response.redirect?
end
raise Capybara::InfiniteRedirectError, "redirected more than #{Capybara.redirect_limit} times, check for infinite redirects." if last_response.redirect?
end
def process(method, path, attributes = {}, env = {})

View File

@ -6,7 +6,8 @@ require 'cgi'
class Capybara::RackTest::Driver < Capybara::Driver::Base
DEFAULT_OPTIONS = {
:respect_data_method => false
:respect_data_method => false,
:follow_redirects => true
}
attr_reader :app, :options
@ -20,6 +21,10 @@ class Capybara::RackTest::Driver < Capybara::Driver::Base
@browser ||= Capybara::RackTest::Browser.new(self)
end
def follow_redirects?
@options[:follow_redirects]
end
def response
browser.last_response
end

View File

@ -87,4 +87,22 @@ describe Capybara::RackTest::Driver do
@driver.body.should include('foobar')
end
end
describe ':follow_redirects option' do
it "defaults to following redirects" do
@driver = Capybara::RackTest::Driver.new(TestApp)
@driver.visit('/redirect')
@driver.response.header['Location'].should be_nil
@driver.browser.current_url.should eq "#{@driver.browser.current_host}/landed"
end
it "is possible to not follow redirects" do
@driver = Capybara::RackTest::Driver.new(TestApp, :follow_redirects => false)
@driver.visit('/redirect')
@driver.response.header['Location'].should eq "#{@driver.browser.current_host}/redirect_again"
@driver.browser.current_url.should eq "#{@driver.browser.current_host}/redirect"
end
end
end