Skip to content
Browse files

Added tests for remote to local redirection and vice versa. The norma…

…l redirect specs are also passing
  • Loading branch information...
1 parent 3e70a06 commit 9fbe04ae28a2fcb4d9300b3d3aec4ece89086d4a @jeroenvandijk jeroenvandijk committed Aug 19, 2010
View
24 lib/capybara/driver/mechanize.rb
@@ -11,10 +11,6 @@ def initialize(*args)
@agent = ::Mechanize.new
@agent.redirect_ok = false
end
-
- def visit(url)
- get url
- end
def cleanup!
@agent.cookie_jar.clear!
@@ -34,7 +30,7 @@ def follow_redirect!
unless response.redirect?
raise "Last response was not a redirect. Cannot follow_redirect!"
end
-
+
location = if last_request_remote?
remote_response.page.response['Location']
else
@@ -45,11 +41,19 @@ def follow_redirect!
end
def get(url, params = {}, headers = {})
- process_remote_request(:get, url) || super
+ if remote?(url)
+ process_remote_request(:get, url)
+ else
+ super
+ end
end
def post(url, params = {}, headers = {})
- process_remote_request(:post, url, params, headers) || super
+ if remote?(url)
+ process_remote_request(:post, url, params, headers)
+ else
+ super
+ end
end
def remote?(url)
@@ -63,10 +67,10 @@ def remote?(url)
end
end
- private
-
attr_reader :agent
+ private
+
def last_request_remote?
!!@last_request_remote
end
@@ -77,8 +81,6 @@ def process_remote_request(method, url, *options)
reset_cache
@agent.send *( [method, url] + options)
@last_request_remote = true
- follow_redirects!
- true
else
@last_request_remote = false
end
View
19 lib/capybara/spec/extended_test_app.rb
@@ -0,0 +1,19 @@
+require 'capybara/spec/test_app'
+
+class ExtendedTestApp < TestApp#< Sinatra::Base
+ set :environment, :production # so we don't get debug info that makes our test pass!
+
+ get %r{/redirect_to/(.*)} do
+ redirect params[:captures]
+ end
+
+ get '/host' do
+ "current host is #{request.host}"
+ end
+end
+
+if __FILE__ == $0
+ Rack::Handler::Mongrel.run ExtendedTestApp, :Port => 8070
+end
+
+
View
27 spec/driver/mechanize_driver_spec.rb
@@ -2,7 +2,7 @@
describe "Capybara::Driver::Mechanize, in local model" do
before do
- @driver = Capybara::Driver::Mechanize.new(TestApp)
+ @driver = Capybara::Driver::Mechanize.new(ExtendedTestApp)
end
it "should throw an error when no rack app is given" do
@@ -15,9 +15,7 @@
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"
-
- # Pending:
- # it_should_behave_like "driver with infinite redirect detection"
+ it_should_behave_like "driver with infinite redirect detection"
it "should default to local mode" do
@driver.remote?('http://www.local.com').should be false
@@ -50,10 +48,31 @@
it "should treat other urls as remote" do
@driver.remote?('http://www.remote.com').should be true
end
+
+ it "should receive the right host" do
+ @driver.visit('http://www.local.com/host')
+ @driver.body.should include('local.com')
+ end
+
+ it "should follow redirects from local to remote" do
+ @driver.visit("http://www.local.com/redirect_to/#{REMOTE_TEST_URL}/host")
+ @driver.body.should include(REMOTE_TEST_HOST)
+ end
after :each do
Capybara.default_host = nil
end
end
+ it "should include the right host when remote" do
+ @driver.visit("#{REMOTE_TEST_URL}/host")
+ @driver.body.should include(REMOTE_TEST_HOST)
+ end
+
+
+ it "should follow redirects from remote to local" do
+ @driver.visit("#{REMOTE_TEST_URL}/redirect_to/http://www.local.com/host")
+ @driver.body.should include('local.com')
+ end
+
end
View
9 spec/driver/remote_mechanize_driver_spec.rb
@@ -2,7 +2,7 @@
describe Capybara::Driver::Mechanize do
before(:each) do
- Capybara.app_host = "http://capybara-testapp.heroku.com"
+ Capybara.app_host = REMOTE_TEST_URL
end
after(:each) do
@@ -12,14 +12,13 @@
before do
@driver = Capybara::Driver::Mechanize.new(TestApp)
end
-
+
context "in remote mode" do
it_should_behave_like "driver"
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"
-
- # Pending:
- # it_should_behave_like "driver with infinite redirect detection"
+ it_should_behave_like "driver with infinite redirect detection"
end
+
end
View
2 spec/session/remote_mechanize_spec.rb
@@ -3,7 +3,7 @@
describe Capybara::Session do
context 'with remote mechanize driver' do
before(:each) do
- Capybara.app_host = "http://capybara-testapp.heroku.com"
+ Capybara.app_host = REMOTE_TEST_URL
end
after(:each) do
View
6 spec/spec_helper.rb
@@ -4,6 +4,8 @@
require 'sinatra'
require 'spec'
+require 'capybara/spec/extended_test_app'
+
# TODO move this stuff into capybara
require 'capybara/spec/driver'
require 'capybara/spec/session'
@@ -16,4 +18,6 @@
config.after do
Capybara.default_selector = :xpath
end
-end
+end
+REMOTE_TEST_HOST = "capybara-testapp.heroku.com"
+REMOTE_TEST_URL = "http://#{REMOTE_TEST_HOST}:8070"

0 comments on commit 9fbe04a

Please sign in to comment.
Something went wrong with that request. Please try again.