Fix pollution of rack-test visit url from previous visit

This commit is contained in:
Thomas Walpole 2022-05-08 12:17:59 -07:00
parent ac0ab33d6c
commit b21c2383d3
5 changed files with 45 additions and 2 deletions

View File

@ -1,3 +1,10 @@
# Version 3.37.1
Relesae date: unreleased
### Fixed
* Regression in rack-test visit - Issue #2548
# Version 3.37.0
Release date: 2022-05-07

View File

@ -20,6 +20,8 @@ class Capybara::RackTest::Browser
end
def visit(path, **attributes)
@new_visit_request = true
reset_cache!
reset_host!
process_and_follow_redirects(:get, path, attributes)
end
@ -45,7 +47,6 @@ class Capybara::RackTest::Browser
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?
driver.redirect_limit.times do
@ -69,6 +70,7 @@ class Capybara::RackTest::Browser
@current_scheme, @current_host, @current_port = new_uri.select(:scheme, :host, :port)
@current_fragment = new_uri.fragment || @current_fragment
reset_cache!
@new_visit_request = false
send(method, new_uri.to_s, attributes, env.merge(options[:headers] || {}))
end
@ -127,6 +129,18 @@ class Capybara::RackTest::Browser
dom.title
end
def last_request
raise Rack::Test::Error if @new_visit_request
super
end
def last_response
raise Rack::Test::Error if @new_visit_request
super
end
protected
def base_href

View File

@ -214,5 +214,11 @@ Capybara::SpecHelper.spec '#visit' do
@session.click_link('Bare query')
expect(@session).to have_current_path('/?a=3')
end
it 'should not use the base href with a new visit call' do
@session.visit('/base/with_other_base')
@session.visit('with_html')
expect(@session).to have_current_path('/with_html')
end
end
end

View File

@ -193,6 +193,22 @@ class TestApp < Sinatra::Base
HTML
end
get '/base/with_other_base' do
<<-HTML
<!DOCTYPE html>
<html>
<head>
<base href="/base/">
<title>Origin</title>
</head>
<body>
<a href="with_title">Title page</a>
<a href="?a=3">Bare query</a>
</body>
</html>
HTML
end
get '/csp' do
response.headers['Content-Security-Policy'] = "default-src 'none'; connect-src 'self'; base-uri 'none'; font-src 'self'; img-src 'self' data:; object-src 'none'; script-src 'self' 'nonce-jAviMuMisoTisVXjgLoWdA=='; style-src 'self' 'nonce-jAviMuMisoTisVXjgLoWdA=='; form-action 'self';"
<<-HTML

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true
module Capybara
VERSION = '3.37.0'
VERSION = '3.37.1'
end