Skip to content

Commit

Permalink
Support default_host in Rack::Test::Methods
Browse files Browse the repository at this point in the history
This allows you to configure a default host the same way you
configure the application.

Fixes rack#317
  • Loading branch information
jeremyevans committed Aug 10, 2022
1 parent 23c543b commit 8278e9d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* Add `Rack::Test::Session#restore_state`, for executing a block
and restoring current state (last request, last response, and
cookies) after the block. (Jeremy Evans #316)
* Make `Rack::Test::Methods` support `default_host` method similar to
`app`, which will set the default host used for requests to the app.
(Jeremy Evans #317 #318)

## 2.0.2 / 2022-06-28

Expand Down
6 changes: 5 additions & 1 deletion lib/rack/test/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ def build_rack_test_session(_name) # :nodoc:
# Backwards compatibility for capybara
build_rack_mock_session
else
Session.new(app)
if respond_to?(:default_host)
Session.new(app, default_host)
else
Session.new(app)
end
end
end

Expand Down
21 changes: 21 additions & 0 deletions spec/rack/test/methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,25 @@
define_singleton_method(:build_rack_mock_session){session}
current_session.must_be_same_as session
end

it '#build_rack_test_session will use defined app' do
envs = []
app = proc{|env| envs << env; [200, {}, []]}
define_singleton_method(:app){app}

get '/'
envs.first['PATH_INFO'].must_equal '/'
envs.first['HTTP_HOST'].must_equal 'example.org'
end

it '#build_rack_test_session will use defined default_host' do
envs = []
app = proc{|env| envs << env; [200, {}, []]}
define_singleton_method(:app){app}
define_singleton_method(:default_host){'foo.example.com'}

get '/'
envs.first['PATH_INFO'].must_equal '/'
envs.first['HTTP_HOST'].must_equal 'foo.example.com'
end
end

0 comments on commit 8278e9d

Please sign in to comment.