Skip to content

Commit

Permalink
Merge pull request #2549 from teamcapybara/Issue2548_new_visit_uri_re…
Browse files Browse the repository at this point in the history
…gression

Fix pollution of rack-test visit url from previous visit
  • Loading branch information
twalpole authored May 8, 2022
2 parents ac0ab33 + b21c238 commit 2f4a426
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
7 changes: 7 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
16 changes: 15 additions & 1 deletion lib/capybara/rack_test/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def options
end

def visit(path, **attributes)
@new_visit_request = true
reset_cache!
reset_host!
process_and_follow_redirects(:get, path, attributes)
end
Expand All @@ -45,7 +47,6 @@ def follow(method, path, **attributes)
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
Expand All @@ -69,6 +70,7 @@ def process(method, path, attributes = {}, env = {})
@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

Expand Down Expand Up @@ -127,6 +129,18 @@ def title
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
Expand Down
6 changes: 6 additions & 0 deletions lib/capybara/spec/session/visit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,11 @@
@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
16 changes: 16 additions & 0 deletions lib/capybara/spec/test_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ def initialize(string1, msg)
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
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Capybara
VERSION = '3.37.0'
VERSION = '3.37.1'
end

0 comments on commit 2f4a426

Please sign in to comment.