1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add fallback host for SystemTestCase driven by RackTest

For links to work in SystemTestCases, it requires the `host` to be added
to the `default_url_options`. Since
78c734386c this is done by looking at
`Capybara.app_host` or `Capybara.current_session.server_url`.

This works correctly when using the Selenium webdriver, as Capybara sets
either the `Capybara.app_host` or the `Capybara.current_session.server_url`.
However the Capybara RackTest driver uses paths instead of URL's, so it doesn't
set Capybara.app_host or Capybara.current_session.server_url.

For SystemTestCase to work for the RackTest driver a host has to be
available for url options. We can fallback to "http://127.0.0.1" which
was the default before 78c734386c.
This commit is contained in:
Petrik 2021-09-21 12:45:13 +02:00
parent 1a240cf83e
commit 4b78325a12
3 changed files with 27 additions and 2 deletions

View file

@ -115,6 +115,8 @@ module ActionDispatch
include SystemTesting::TestHelpers::SetupAndTeardown
include SystemTesting::TestHelpers::ScreenshotHelper
DEFAULT_HOST = "http://127.0.0.1"
def initialize(*) # :nodoc:
super
self.class.driven_by(:selenium) unless self.class.driver?
@ -166,7 +168,11 @@ module ActionDispatch
include ActionDispatch.test_app.routes.mounted_helpers
def url_options
default_url_options.reverse_merge(host: Capybara.app_host || Capybara.current_session.server_url)
default_url_options.reverse_merge(host: app_host)
end
def app_host
Capybara.app_host || Capybara.current_session.server_url || DEFAULT_HOST
end
end.new
end

View file

@ -36,6 +36,10 @@ class SetDriverToSeleniumHeadlessFirefoxTest < DrivenBySeleniumWithHeadlessFiref
end
class SetHostTest < DrivenByRackTest
teardown do
Capybara.app_host = nil
end
test "overrides host" do
assert_deprecated do
host! "http://example.com"

View file

@ -26,7 +26,22 @@ class SystemTestCaseTest < ActiveSupport::TestCase
assert_not_includes(ActionDispatch::SystemTestCase.runnable_methods, :test_foo_url)
end
test "system tests set the Capybara host in the url_options by default" do
test "system tests use 127.0.0.1 in the url_options be default" do
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
get 'foo', to: 'foo#index', as: 'test_foo'
end
RUBY
app("test")
rack_test_case = Class.new(ActionDispatch::SystemTestCase) do
driven_by :rack_test
end
system_test = rack_test_case.new("my_test")
assert_equal("http://127.0.0.1/foo", system_test.test_foo_url)
end
test "system tests use Capybara.app_host in the url_options if present" do
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
get 'foo', to: 'foo#index', as: 'test_foo'