1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Rewrite Session#visit using URI#merge

This commit is contained in:
Thomas Walpole 2014-06-30 14:44:33 +09:00
parent fdf54b16bb
commit af73a1dd26
2 changed files with 35 additions and 16 deletions

View file

@ -206,30 +206,30 @@ module Capybara
#
# @param [#to_s] url The URL to navigate to. The parameter will be cast to a String.
#
def visit(url)
def visit(visit_uri)
raise_server_error!
url = url.to_s
@touched = true
url_relative = URI.parse(url).scheme.nil?
visit_uri = URI.parse(visit_uri.to_s)
if url_relative && Capybara.app_host
url = Capybara.app_host + url
url_relative = false
uri_base = if @server
visit_uri.port = @server.port if Capybara.always_include_port && (visit_uri.port == visit_uri.default_port)
URI.parse(Capybara.app_host || "http://#{@server.host}:#{@server.port}")
else
Capybara.app_host && URI.parse(Capybara.app_host)
end
if @server
url = "http://#{@server.host}:#{@server.port}" + url if url_relative
if Capybara.always_include_port
uri = URI.parse(url)
uri.port = @server.port if uri.port == uri.default_port
url = uri.to_s
end
# TODO - this is only for compatability with previous 2.x behavior that concatenated
# Capybara.app_host and a "relative" path - Consider removing in 3.0
# @abotalov brought up a good point about this behavior potentially being useful to people
# deploying to a subdirectory and/or single page apps where only the url fragment changes
if visit_uri.scheme.nil? && uri_base
visit_uri.path = uri_base.path + visit_uri.path
end
driver.visit(url)
visit_uri = uri_base.merge(visit_uri) unless uri_base.nil?
driver.visit(visit_uri.to_s)
end
##

View file

@ -79,6 +79,25 @@ Capybara::SpecHelper.spec '#visit' do
end
end
context "with Capybara.app_host set" do
it "should override server", requires: [:server] do
another_session = Capybara::Session.new(@session.mode, @session.app.dup)
Capybara.app_host = "http://#{@session.server.host}:#{@session.server.port}"
another_session.visit('/foo')
expect(another_session).to have_content("Another World")
expect(another_session.current_url).to start_with(Capybara.app_host)
expect(URI.parse(another_session.current_url).port).not_to eq another_session.server.port
expect(URI.parse(another_session.current_url).port).to eq @session.server.port
end
it "should append relative path", requires: [:server] do
Capybara.app_host = "http://#{@session.server.host}:#{@session.server.port}/redirect/0"
@session.visit('/times')
expect(@session).to have_content('redirection complete')
end
end
it "should send no referer when visiting a page" do
@session.visit '/get_referer'
expect(@session).to have_content 'No referer'