From af73a1dd26c89073d28be025c1c75f6404bcff82 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 30 Jun 2014 14:44:33 +0900 Subject: [PATCH] Rewrite Session#visit using URI#merge --- lib/capybara/session.rb | 32 ++++++++++++------------- lib/capybara/spec/session/visit_spec.rb | 19 +++++++++++++++ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index c54f728a..cb348681 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -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 ## diff --git a/lib/capybara/spec/session/visit_spec.rb b/lib/capybara/spec/session/visit_spec.rb index dac6740e..f9b56d65 100644 --- a/lib/capybara/spec/session/visit_spec.rb +++ b/lib/capybara/spec/session/visit_spec.rb @@ -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'