Make redirect_limit a driver option instead

This commit is contained in:
Jonas Nicklas 2012-07-12 17:03:28 +02:00
parent 3b44c7da59
commit ec56c52834
5 changed files with 45 additions and 49 deletions

View File

@ -21,7 +21,7 @@ module Capybara
attr_accessor :server_host, :server_port
attr_accessor :default_selector, :default_wait_time, :ignore_hidden_elements
attr_accessor :save_and_open_page_path, :automatic_reload
attr_writer :default_driver, :current_driver, :javascript_driver, :session_name, :redirect_limit
attr_writer :default_driver, :current_driver, :javascript_driver, :session_name
attr_accessor :app
##
@ -185,14 +185,6 @@ module Capybara
end
end
##
#
# @return [Integer] Current redirect limit
#
def redirect_limit
@redirect_limit
end
##
#
# @return [Symbol] The name of the driver to use by default
@ -309,7 +301,6 @@ module Capybara
self.default_driver = nil
self.current_driver = nil
self.redirect_limit = 5
autoload :DSL, 'capybara/dsl'
autoload :Server, 'capybara/server'

View File

@ -34,10 +34,10 @@ class Capybara::RackTest::Browser
def process_and_follow_redirects(method, path, attributes = {}, env = {})
process(method, path, attributes, env)
if driver.follow_redirects?
Capybara.redirect_limit.times do
driver.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?
raise Capybara::InfiniteRedirectError, "redirected more than #{driver.redirect_limit} times, check for infinite redirects." if last_response.redirect?
end
end

View File

@ -7,7 +7,8 @@ require 'cgi'
class Capybara::RackTest::Driver < Capybara::Driver::Base
DEFAULT_OPTIONS = {
:respect_data_method => false,
:follow_redirects => true
:follow_redirects => true,
:redirect_limit => 5
}
attr_reader :app, :options
@ -25,6 +26,10 @@ class Capybara::RackTest::Driver < Capybara::Driver::Base
@options[:follow_redirects]
end
def redirect_limit
@options[:redirect_limit]
end
def response
browser.last_response
end

View File

@ -265,41 +265,6 @@ shared_examples_for "driver with cookies support" do
end
end
shared_examples_for "driver with infinite redirect detection" do
context "with default redirect limit" do
let(:default_limit) { Capybara.redirect_limit }
it "should follow #{Capybara.redirect_limit} redirects" do
@driver.visit("/redirect/#{default_limit}/times")
@driver.body.should include('redirection complete')
end
it "should not follow more than #{Capybara.redirect_limit} redirects" do
running do
@driver.visit("/redirect/#{default_limit + 1}/times")
end.should raise_error(Capybara::InfiniteRedirectError)
end
end
context "with 21 redirect limit" do
let(:custom_limit) { 21 }
around(:each) do |example|
default_limit = Capybara.redirect_limit
Capybara.redirect_limit= custom_limit
example.run
Capybara.redirect_limit= default_limit
end
it "should follow 21 redirects" do
@driver.visit("/redirect/#{custom_limit}/times")
@driver.body.should include('redirection complete')
end
it "should not follow more than 21 redirects" do
running do
@driver.visit("/redirect/#{custom_limit + 1}/times")
end.should raise_error(Capybara::InfiniteRedirectError)
end
end
end
shared_examples_for "driver with referer support" do
before :each do
@driver.reset!

View File

@ -29,7 +29,6 @@ describe Capybara::RackTest::Driver do
it_should_behave_like "driver with header support"
it_should_behave_like "driver with status code support"
it_should_behave_like "driver with cookies support"
it_should_behave_like "driver with infinite redirect detection"
it_should_behave_like "driver with referer support"
describe '#reset!' do
@ -105,4 +104,40 @@ describe Capybara::RackTest::Driver do
@driver.browser.current_url.should eq "#{@driver.browser.current_host}/redirect"
end
end
describe ':redirect_limit option' do
context "with default redirect limit" do
before do
@driver = Capybara::RackTest::Driver.new(TestApp)
end
it "should follow 5 redirects" do
@driver.visit("/redirect/5/times")
@driver.body.should include('redirection complete')
end
it "should not follow more than 6 redirects" do
running do
@driver.visit("/redirect/6/times")
end.should raise_error(Capybara::InfiniteRedirectError)
end
end
context "with 21 redirect limit" do
before do
@driver = Capybara::RackTest::Driver.new(TestApp, :redirect_limit => 21)
end
it "should follow 21 redirects" do
@driver.visit("/redirect/21/times")
@driver.body.should include('redirection complete')
end
it "should not follow more than 21 redirects" do
running do
@driver.visit("/redirect/22/times")
end.should raise_error(Capybara::InfiniteRedirectError)
end
end
end
end