1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Rack::Test now does not send a referer when calling visit multiple times

This commit is contained in:
Jo Liss 2012-01-20 13:24:40 +01:00
parent 6a450276b4
commit 21d0b25dba
3 changed files with 19 additions and 32 deletions

View file

@ -18,30 +18,28 @@ class Capybara::RackTest::Browser
def visit(path, attributes = {})
reset_host!
process(:get, path, attributes)
follow_redirects!
process_and_follow_redirects(:get, path, attributes)
end
def submit(method, path, attributes)
path = request_path if not path or path.empty?
process(method, path, attributes)
follow_redirects!
process_and_follow_redirects(method, path, attributes, {'HTTP_REFERER' => current_url})
end
def follow(method, path, attributes = {})
return if path.gsub(/^#{request_path}/, '').start_with?('#')
process(method, path, attributes)
follow_redirects!
process_and_follow_redirects(method, path, attributes, {'HTTP_REFERER' => current_url})
end
def follow_redirects!
def process_and_follow_redirects(method, path, attributes = {}, env = {})
process(method, path, attributes, env)
5.times do
process(:get, last_response["Location"]) if last_response.redirect?
process(:get, last_response["Location"], {}, env) if last_response.redirect?
end
raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect?
end
def process(method, path, attributes = {})
def process(method, path, attributes = {}, env = {})
new_uri = URI.parse(path)
method.downcase! unless method.is_a? Symbol
@ -60,7 +58,7 @@ class Capybara::RackTest::Browser
end
reset_cache!
send(method, path, attributes, env)
send(method, path, attributes, env.merge(options[:headers] || {}))
end
def current_url
@ -107,20 +105,4 @@ protected
rescue Rack::Test::Error
""
end
def env
env = {}
begin
if last_response.redirect?
env["HTTP_REFERER"] = last_request.env["HTTP_REFERER"]
else
env["HTTP_REFERER"] = last_request.url
end
rescue Rack::Test::Error
# no request yet
end
env.merge!(options[:headers]) if options[:headers]
env
end
end

View file

@ -307,9 +307,6 @@ shared_examples_for "driver with referer support" do
end
it "should send no referer when visiting a second page" do
if @driver.is_a? Capybara::RackTest::Driver
pending 'Rack::Test sends referer on subsequent visit'
end
@driver.visit '/get_referer'
@driver.visit '/get_referer'
@driver.body.should include 'No referer'
@ -318,12 +315,18 @@ shared_examples_for "driver with referer support" do
it "should send a referer when following a link" do
@driver.visit '/referer_base'
@driver.find('//a[@href="/get_referer"]').first.click
@driver.body.should include '/referer_base'
@driver.body.should match %r{http://.*/referer_base}
end
it "should preserve the original referer URL when following a redirect" do
@driver.visit('/referer_base')
@driver.find('//a[@href="/redirect_to_get_referer"]').first.click
@driver.body.should include('/referer_base')
@driver.body.should match %r{http://.*/referer_base}
end
it "should send a referer when submitting a form" do
@driver.visit '/referer_base'
@driver.find('//input').first.click
@driver.body.should match %r{http://.*/referer_base}
end
end

View file

@ -25,7 +25,9 @@ class TestApp < Sinatra::Base
end
get '/referer_base' do
'<a href="/get_referer">direct link</a>; <a href="/redirect_to_get_referer">link via redirect</a>'
'<a href="/get_referer">direct link</a>' +
'<a href="/redirect_to_get_referer">link via redirect</a>' +
'<form action="/get_referer" method="get"><input type="submit"></form>'
end
get '/redirect_to_get_referer' do