-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add Rack::Test.default_host= to change the default_host more easily. #317
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we consider the following implementation: DEFAULT_HOST = 'example.org'.freeze
class << self
@default_host = DEFAULT_HOST
attr_accessor :default_host
end I'm not sure if we can remove Consider that We've followed this model elsewhere: https://github.com/rack/rack/blob/main/lib/rack/request.rb#L17-L41 Don't have a strong opinion about it, just wanted to make a suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We definitely cannot accept the use of class variables. Class variables in Ruby are broken and should never be used. This approach: class << self
@default_host = DEFAULT_HOST
attr_accessor :default_host
end Is broken. It sets the instance variable in the singleton class instead of the class. However, with that fixed, because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah using the approach above failed 1-44 tests in a somewhat random nature. Moving to this:
makes all the tests passing again. Since it is a module, I am not sure I see the difference. |
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just thinking about alternatives, is there no easy way to inject the default host argument on a per-test/spec basis? What about if we made this
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think per test/spec is possible. You can override the URI provided for each request, but the CookieJar always uses the default host for the session, and I think it may be better to modify |
||
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 = [] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could maybe leave in the constant DEFAULT_HOST for people using a work around?