From 1a48bc77162fff4484c1ded72d8e700b06e9a95a Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 13 Jul 2020 23:17:58 -0700 Subject: [PATCH] Rack test driver maintain fragment when redirecting --- lib/capybara/rack_test/browser.rb | 8 ++++++-- lib/capybara/spec/session/current_url_spec.rb | 12 +++++++++++- lib/capybara/spec/test_app.rb | 4 ++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/capybara/rack_test/browser.rb b/lib/capybara/rack_test/browser.rb index 4e4e23b1..ebd226ca 100644 --- a/lib/capybara/rack_test/browser.rb +++ b/lib/capybara/rack_test/browser.rb @@ -8,6 +8,7 @@ class Capybara::RackTest::Browser def initialize(driver) @driver = driver + @current_fragment = nil end def app @@ -42,6 +43,7 @@ class Capybara::RackTest::Browser end def process_and_follow_redirects(method, path, attributes = {}, env = {}) + @current_fragment = build_uri(path).fragment process(method, path, attributes, env) return unless driver.follow_redirects? @@ -65,7 +67,7 @@ class Capybara::RackTest::Browser method = method.downcase new_uri = build_uri(path) @current_scheme, @current_host, @current_port = new_uri.select(:scheme, :host, :port) - + @current_fragment = new_uri.fragment || @current_fragment reset_cache! send(method, new_uri.to_s, attributes, env.merge(options[:headers] || {})) end @@ -83,7 +85,9 @@ class Capybara::RackTest::Browser end 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 '' end diff --git a/lib/capybara/spec/session/current_url_spec.rb b/lib/capybara/spec/session/current_url_spec.rb index c797a650..5ee44e89 100644 --- a/lib/capybara/spec/session/current_url_spec.rb +++ b/lib/capybara/spec/session/current_url_spec.rb @@ -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_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 expect(@session).to have_content("Current host is #{scheme}://#{s.host}:#{s.port}") if path == '/host' end @@ -83,6 +83,16 @@ Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do @session.visit("#{bases[0]}/redirect") should_be_on 0, '/landed' 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 @session.visit('/with_js') diff --git a/lib/capybara/spec/test_app.rb b/lib/capybara/spec/test_app.rb index fae3836a..266e2a6f 100644 --- a/lib/capybara/spec/test_app.rb +++ b/lib/capybara/spec/test_app.rb @@ -33,6 +33,10 @@ class TestApp < Sinatra::Base redirect '/redirect_again' end + get '/redirect_with_fragment' do + redirect '/landed#with_fragment' + end + get '/redirect_again' do redirect '/landed' end