mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Merge pull request #2369 from teamcapybara/rack_fragment
Maintain URI fragment when redirecting in the rack test driver
This commit is contained in:
commit
fc095122a4
3 changed files with 21 additions and 3 deletions
|
@ -8,6 +8,7 @@ class Capybara::RackTest::Browser
|
||||||
|
|
||||||
def initialize(driver)
|
def initialize(driver)
|
||||||
@driver = driver
|
@driver = driver
|
||||||
|
@current_fragment = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def app
|
def app
|
||||||
|
@ -42,6 +43,7 @@ class Capybara::RackTest::Browser
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_and_follow_redirects(method, path, attributes = {}, env = {})
|
def process_and_follow_redirects(method, path, attributes = {}, env = {})
|
||||||
|
@current_fragment = build_uri(path).fragment
|
||||||
process(method, path, attributes, env)
|
process(method, path, attributes, env)
|
||||||
|
|
||||||
return unless driver.follow_redirects?
|
return unless driver.follow_redirects?
|
||||||
|
@ -65,7 +67,7 @@ class Capybara::RackTest::Browser
|
||||||
method = method.downcase
|
method = method.downcase
|
||||||
new_uri = build_uri(path)
|
new_uri = build_uri(path)
|
||||||
@current_scheme, @current_host, @current_port = new_uri.select(:scheme, :host, :port)
|
@current_scheme, @current_host, @current_port = new_uri.select(:scheme, :host, :port)
|
||||||
|
@current_fragment = new_uri.fragment || @current_fragment
|
||||||
reset_cache!
|
reset_cache!
|
||||||
send(method, new_uri.to_s, attributes, env.merge(options[:headers] || {}))
|
send(method, new_uri.to_s, attributes, env.merge(options[:headers] || {}))
|
||||||
end
|
end
|
||||||
|
@ -83,7 +85,9 @@ class Capybara::RackTest::Browser
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_url
|
def current_url
|
||||||
last_request.url
|
uri = build_uri(last_request.url)
|
||||||
|
uri.fragment = @current_fragment if @current_fragment
|
||||||
|
uri.to_s
|
||||||
rescue Rack::Test::Error
|
rescue Rack::Test::Error
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,7 +22,7 @@ Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do
|
||||||
|
|
||||||
expect(@session.current_url.chomp('?')).to eq("#{scheme}://#{s.host}:#{s.port}#{path}")
|
expect(@session.current_url.chomp('?')).to eq("#{scheme}://#{s.host}:#{s.port}#{path}")
|
||||||
expect(@session.current_host).to eq("#{scheme}://#{s.host}") # no port
|
expect(@session.current_host).to eq("#{scheme}://#{s.host}") # no port
|
||||||
expect(@session.current_path).to eq(path)
|
expect(@session.current_path).to eq(path.split('#')[0])
|
||||||
# Server should agree with us
|
# Server should agree with us
|
||||||
expect(@session).to have_content("Current host is #{scheme}://#{s.host}:#{s.port}") if path == '/host'
|
expect(@session).to have_content("Current host is #{scheme}://#{s.host}:#{s.port}") if path == '/host'
|
||||||
end
|
end
|
||||||
|
@ -84,6 +84,16 @@ Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do
|
||||||
should_be_on 0, '/landed'
|
should_be_on 0, '/landed'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'maintains fragment' do
|
||||||
|
@session.visit("#{bases[0]}/redirect#fragment")
|
||||||
|
should_be_on 0, '/landed#fragment'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'redirects to a fragment' do
|
||||||
|
@session.visit("#{bases[0]}/redirect_with_fragment")
|
||||||
|
should_be_on 0, '/landed#with_fragment'
|
||||||
|
end
|
||||||
|
|
||||||
it 'is affected by pushState', requires: [:js] do
|
it 'is affected by pushState', requires: [:js] do
|
||||||
@session.visit('/with_js')
|
@session.visit('/with_js')
|
||||||
@session.execute_script("window.history.pushState({}, '', '/pushed')")
|
@session.execute_script("window.history.pushState({}, '', '/pushed')")
|
||||||
|
|
|
@ -33,6 +33,10 @@ class TestApp < Sinatra::Base
|
||||||
redirect '/redirect_again'
|
redirect '/redirect_again'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
get '/redirect_with_fragment' do
|
||||||
|
redirect '/landed#with_fragment'
|
||||||
|
end
|
||||||
|
|
||||||
get '/redirect_again' do
|
get '/redirect_again' do
|
||||||
redirect '/landed'
|
redirect '/landed'
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue