Skip to content

Commit

Permalink
Merge pull request #43273 from p8/actionpack/add-fallback-host-for-ra…
Browse files Browse the repository at this point in the history
…ck-system-tests

Add fallback host for SystemTestCase driven by RackTest
  • Loading branch information
rafaelfranca committed Sep 21, 2021
2 parents eb04c7f + 4b78325 commit ce72a8a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
8 changes: 7 additions & 1 deletion actionpack/lib/action_dispatch/system_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class SystemTestCase < ActiveSupport::TestCase
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?
Expand Down Expand Up @@ -166,7 +168,11 @@ def url_helpers
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 16 additions & 1 deletion railties/test/application/system_test_case_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,22 @@ def teardown
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'
Expand Down

0 comments on commit ce72a8a

Please sign in to comment.