From 4852287b628ef023e3a6f056fddc7d984c154dad Mon Sep 17 00:00:00 2001 From: Christopher Reister Date: Sat, 6 Aug 2022 10:33:43 -0700 Subject: [PATCH] add Rack::Test.default_host= to change the default_host more easily. --- lib/rack/test.rb | 13 ++++++++++--- lib/rack/test/cookie_jar.rb | 4 ++-- spec/rack/test_spec.rb | 7 +++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/rack/test.rb b/lib/rack/test.rb index f0a5c27..bce71bb 100644 --- a/lib/rack/test.rb +++ b/lib/rack/test.rb @@ -30,7 +30,14 @@ module Rack module Test # The default host to use for requests, when a full URI is not # provided. - DEFAULT_HOST = 'example.org'.freeze + def self.default_host + @@default_host ||= 'example.org'.freeze + end + + # Sets the default host used when a full URI is not provided. + def self.default_host=(host) + @@default_host = host.freeze + end # The default multipart boundary to use for multipart request bodies MULTIPART_BOUNDARY = '----------XnJLe9ZIbbGUYtzPQJ16u1'.freeze @@ -54,7 +61,7 @@ class Session extend Forwardable include Rack::Test::Utils - def self.new(app, default_host = DEFAULT_HOST) # :nodoc: + def self.new(app, default_host = Rack::Test.default_host) # :nodoc: if app.is_a?(self) # Backwards compatibility for initializing with Rack::MockSession app @@ -96,7 +103,7 @@ def self.new(app, default_host = DEFAULT_HOST) # :nodoc: # submitted in #last_request. The methods store a Rack::MockResponse based on the # response in #last_response. #last_response is also returned by the methods. # If a block is given, #last_response is also yielded to the block. - def initialize(app, default_host = DEFAULT_HOST) + def initialize(app, default_host = Rack::Test.default_host) @env = {} @app = app @after_request = [] diff --git a/lib/rack/test/cookie_jar.rb b/lib/rack/test/cookie_jar.rb index 9a0b89d..27005df 100644 --- a/lib/rack/test/cookie_jar.rb +++ b/lib/rack/test/cookie_jar.rb @@ -20,7 +20,7 @@ class Cookie # :nodoc: # name=value format is name and value are provided. attr_reader :raw - def initialize(raw, uri = nil, default_host = DEFAULT_HOST) + def initialize(raw, uri = nil, default_host = Rack::Test.default_host) @default_host = default_host uri ||= default_uri @@ -133,7 +133,7 @@ def default_uri class CookieJar # :nodoc: DELIMITER = '; '.freeze - def initialize(cookies = [], default_host = DEFAULT_HOST) + def initialize(cookies = [], default_host = Rack::Test.default_host) @default_host = default_host @cookies = cookies.sort! end diff --git a/spec/rack/test_spec.rb b/spec/rack/test_spec.rb index 60932f8..9dc3cd8 100644 --- a/spec/rack/test_spec.rb +++ b/spec/rack/test_spec.rb @@ -73,6 +73,13 @@ last_request.env['SERVER_NAME'].must_equal 'example.org' end + it 'default_host can be changed' do + Rack::Test.default_host = 'rack.github.io' + request '/' + last_request.env['SERVER_NAME'].must_equal 'rack.github.io' + Rack::Test.default_host = 'example.org' + end + it 'yields the response to a given block' do request '/' do |response| response.must_be :ok?