Skip to content

Commit ce72a8a

Browse files
authored
Merge pull request rails#43273 from p8/actionpack/add-fallback-host-for-rack-system-tests
Add fallback host for SystemTestCase driven by RackTest
2 parents eb04c7f + 4b78325 commit ce72a8a

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

actionpack/lib/action_dispatch/system_test_case.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class SystemTestCase < ActiveSupport::TestCase
115115
include SystemTesting::TestHelpers::SetupAndTeardown
116116
include SystemTesting::TestHelpers::ScreenshotHelper
117117

118+
DEFAULT_HOST = "http://127.0.0.1"
119+
118120
def initialize(*) # :nodoc:
119121
super
120122
self.class.driven_by(:selenium) unless self.class.driver?
@@ -166,7 +168,11 @@ def url_helpers
166168
include ActionDispatch.test_app.routes.mounted_helpers
167169

168170
def url_options
169-
default_url_options.reverse_merge(host: Capybara.app_host || Capybara.current_session.server_url)
171+
default_url_options.reverse_merge(host: app_host)
172+
end
173+
174+
def app_host
175+
Capybara.app_host || Capybara.current_session.server_url || DEFAULT_HOST
170176
end
171177
end.new
172178
end

actionpack/test/dispatch/system_testing/system_test_case_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class SetDriverToSeleniumHeadlessFirefoxTest < DrivenBySeleniumWithHeadlessFiref
3636
end
3737

3838
class SetHostTest < DrivenByRackTest
39+
teardown do
40+
Capybara.app_host = nil
41+
end
42+
3943
test "overrides host" do
4044
assert_deprecated do
4145
host! "http://example.com"

railties/test/application/system_test_case_test.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,22 @@ def teardown
2626
assert_not_includes(ActionDispatch::SystemTestCase.runnable_methods, :test_foo_url)
2727
end
2828

29-
test "system tests set the Capybara host in the url_options by default" do
29+
test "system tests use 127.0.0.1 in the url_options be default" do
30+
app_file "config/routes.rb", <<-RUBY
31+
Rails.application.routes.draw do
32+
get 'foo', to: 'foo#index', as: 'test_foo'
33+
end
34+
RUBY
35+
36+
app("test")
37+
rack_test_case = Class.new(ActionDispatch::SystemTestCase) do
38+
driven_by :rack_test
39+
end
40+
system_test = rack_test_case.new("my_test")
41+
assert_equal("http://127.0.0.1/foo", system_test.test_foo_url)
42+
end
43+
44+
test "system tests use Capybara.app_host in the url_options if present" do
3045
app_file "config/routes.rb", <<-RUBY
3146
Rails.application.routes.draw do
3247
get 'foo', to: 'foo#index', as: 'test_foo'

0 commit comments

Comments
 (0)