From a5f81f97b66559ef20083b16050a364b1036a517 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Tue, 3 Oct 2017 10:18:03 -0700 Subject: [PATCH] fix issue when visiting / when app_host has a trailing / --- lib/capybara/session.rb | 21 +++++++++++---------- lib/capybara/spec/session/visit_spec.rb | 7 ++++++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 176774a8..678ec1bb 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -257,18 +257,19 @@ module Capybara uri_base.port ||= @server.port if @server && config.always_include_port - # 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 - if uri_base && [nil, 'http', 'https'].include?(visit_uri.scheme) - visit_uri = uri_base.merge(visit_uri.to_hash.delete_if { |k,v| v.nil? }) - end + visit_uri_parts = visit_uri.to_hash.delete_if { |k,v| v.nil? } + if visit_uri.scheme.nil? + # 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 + visit_uri_parts[:path] = uri_base.path + visit_uri.path + end + + visit_uri = uri_base.merge(visit_uri_parts) + end driver.visit(visit_uri.to_s) end diff --git a/lib/capybara/spec/session/visit_spec.rb b/lib/capybara/spec/session/visit_spec.rb index 5bea5f46..4313e000 100644 --- a/lib/capybara/spec/session/visit_spec.rb +++ b/lib/capybara/spec/session/visit_spec.rb @@ -113,8 +113,13 @@ Capybara::SpecHelper.spec '#visit' do @session.visit('/times') expect(@session).to have_content('redirection complete') end - end + it "should work if `app_host` has a trailing /", requires: [:server] do + Capybara.app_host = "http://#{@session.server.host}:#{@session.server.port}/" + @session.visit('/') + expect(@session).to have_content('Hello world!') + end + end it "should send no referer when visiting a page" do @session.visit '/get_referer'