Issue-2544 rack test base element support
This commit is contained in:
parent
614274b1a5
commit
ec5a50f933
|
@ -73,14 +73,18 @@ class Capybara::RackTest::Browser
|
|||
end
|
||||
|
||||
def build_uri(path)
|
||||
URI.parse(path).tap do |uri|
|
||||
uri.path = request_path if path.empty? || path.start_with?('?')
|
||||
uri.path = '/' if uri.path.empty?
|
||||
uri.path = request_path.sub(%r{/[^/]*$}, '/') + uri.path unless uri.path.start_with?('/')
|
||||
uri = URI.parse(path)
|
||||
base_uri = base_relative_uri_for(uri)
|
||||
|
||||
uri.path = base_uri.path + uri.path unless uri.absolute? || uri.path.start_with?('/')
|
||||
|
||||
if base_uri.absolute?
|
||||
base_uri.merge(uri)
|
||||
else
|
||||
uri.scheme ||= @current_scheme
|
||||
uri.host ||= @current_host
|
||||
uri.port ||= @current_port unless uri.default_port == @current_port
|
||||
uri
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -125,6 +129,25 @@ class Capybara::RackTest::Browser
|
|||
|
||||
protected
|
||||
|
||||
def base_href
|
||||
find(:css, 'head > base').first&.[](:href).to_s
|
||||
end
|
||||
|
||||
def base_relative_uri_for(uri)
|
||||
base_uri = URI.parse(base_href)
|
||||
current_uri = URI.parse(safe_last_request&.url.to_s).tap do |c|
|
||||
c.path.sub!(%r{/[^/]*$}, '/') unless uri.path.empty?
|
||||
c.path = '/' if c.path.empty?
|
||||
end
|
||||
|
||||
if [current_uri, base_uri].any?(&:absolute?)
|
||||
current_uri.merge(base_uri)
|
||||
else
|
||||
base_uri.path = current_uri.path if base_uri.path.empty?
|
||||
base_uri
|
||||
end
|
||||
end
|
||||
|
||||
def build_rack_mock_session
|
||||
reset_host! unless current_host
|
||||
Rack::MockSession.new(app, current_host)
|
||||
|
@ -136,6 +159,12 @@ protected
|
|||
'/'
|
||||
end
|
||||
|
||||
def safe_last_request
|
||||
last_request
|
||||
rescue Rack::Test::Error
|
||||
nil
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fragment_or_script?(path)
|
||||
|
|
|
@ -201,4 +201,18 @@ Capybara::SpecHelper.spec '#visit' do
|
|||
@session.visit('/get_cookie')
|
||||
expect(@session).to have_content('root cookie')
|
||||
end
|
||||
|
||||
context 'with base element' do
|
||||
it 'should use base href with relative links' do
|
||||
@session.visit('/base/with_base')
|
||||
@session.click_link('Title page')
|
||||
expect(@session).to have_current_path('/with_title')
|
||||
end
|
||||
|
||||
it 'should use base href with bare queries' do
|
||||
@session.visit('/base/with_base')
|
||||
@session.click_link('Bare query')
|
||||
expect(@session).to have_current_path('/?a=3')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -177,6 +177,22 @@ class TestApp < Sinatra::Base
|
|||
HTML
|
||||
end
|
||||
|
||||
get '/base/with_base' do
|
||||
<<-HTML
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<base href="/">
|
||||
<title>Origin</title>
|
||||
</head>
|
||||
<body>
|
||||
<a href="with_title">Title page</a>
|
||||
<a href="?a=3">Bare query</a>
|
||||
</body>
|
||||
</html>
|
||||
HTML
|
||||
end
|
||||
|
||||
get '/download.csv' do
|
||||
content_type 'text/csv'
|
||||
'This, is, comma, separated' \
|
||||
|
|
Loading…
Reference in New Issue