1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/test/application/system_test_case_test.rb
Petrik 4b78325a12 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.
2021-09-21 20:09:16 +02:00

60 lines
1.5 KiB
Ruby

# frozen_string_literal: true
require "isolation/abstract_unit"
require "rack/test"
class SystemTestCaseTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
end
def teardown
teardown_app
end
test "url helpers are delegated to a proxy class" do
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
get 'foo', to: 'foo#index', as: 'test_foo'
end
RUBY
app("test")
assert_not_includes(ActionDispatch::SystemTestCase.runnable_methods, :test_foo_url)
end
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'
end
RUBY
app("test")
system_test = ActionDispatch::SystemTestCase.new("my_test")
previous_app_host = ::Capybara.app_host
::Capybara.app_host = "https://my_test_example.com"
assert_equal("https://my_test_example.com/foo", system_test.test_foo_url)
ensure
::Capybara.app_host = previous_app_host
end
end