From ec56c5283445f80c93d3eefde39fa325852c0a7d Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Thu, 12 Jul 2012 17:03:28 +0200 Subject: [PATCH] Make redirect_limit a driver option instead --- lib/capybara.rb | 11 +-------- lib/capybara/rack_test/browser.rb | 4 +-- lib/capybara/rack_test/driver.rb | 7 +++++- lib/capybara/spec/driver.rb | 35 -------------------------- spec/driver/rack_test_driver_spec.rb | 37 +++++++++++++++++++++++++++- 5 files changed, 45 insertions(+), 49 deletions(-) diff --git a/lib/capybara.rb b/lib/capybara.rb index b7f1ed60..a7f75af8 100644 --- a/lib/capybara.rb +++ b/lib/capybara.rb @@ -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' diff --git a/lib/capybara/rack_test/browser.rb b/lib/capybara/rack_test/browser.rb index 8804f6a5..167d3753 100644 --- a/lib/capybara/rack_test/browser.rb +++ b/lib/capybara/rack_test/browser.rb @@ -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 diff --git a/lib/capybara/rack_test/driver.rb b/lib/capybara/rack_test/driver.rb index aa694b80..00649f42 100644 --- a/lib/capybara/rack_test/driver.rb +++ b/lib/capybara/rack_test/driver.rb @@ -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 diff --git a/lib/capybara/spec/driver.rb b/lib/capybara/spec/driver.rb index 93b9ad72..b53b5009 100644 --- a/lib/capybara/spec/driver.rb +++ b/lib/capybara/spec/driver.rb @@ -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! diff --git a/spec/driver/rack_test_driver_spec.rb b/spec/driver/rack_test_driver_spec.rb index 941f087e..4cc88bbf 100644 --- a/spec/driver/rack_test_driver_spec.rb +++ b/spec/driver/rack_test_driver_spec.rb @@ -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