1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Shorten inspect on ActionDispatch::Request

Calling request in an action of a controller generates an endless stream of
characters, including the Rack app and middlewares.
This can be frustrating when using a debugger in a controller and
accidentally calling `request` generates output for a couple of seconds.

Inspect on ActionDispatch::Request is shortened to the most relevant
attributes and uses the same format as used for request in the logs:

    "#<ActionDispatch::Request POST "https://example.com/path/of/some/uri?q=1" for 1.2.3.4>"
This commit is contained in:
Petrik 2020-09-03 21:00:15 +02:00
parent 3d27653dfd
commit a610f61e02
2 changed files with 18 additions and 0 deletions

View file

@ -431,6 +431,10 @@ module ActionDispatch
super || scheme == "wss"
end
def inspect # :nodoc:
"#<#{self.class.name} #{method} #{original_url.dump} for #{remote_ip}>"
end
private
def check_method(name)
HTTP_METHOD_LOOKUP[name] || raise(ActionController::UnknownHttpMethod, "#{name}, accepted HTTP methods are #{HTTP_METHODS[0...-1].join(', ')}, and #{HTTP_METHODS[-1]}")

View file

@ -1290,3 +1290,17 @@ class EarlyHintsRequestTest < BaseRequestTest
assert_equal expected_hints, early_hints
end
end
class RequestInspectTest < BaseRequestTest
test "inspect" do
request = stub_request(
"REQUEST_METHOD" => "POST",
"REMOTE_ADDR" => "1.2.3.4",
"HTTP_X_FORWARDED_PROTO" => "https",
"HTTP_X_FORWARDED_HOST" => "example.com:443",
"PATH_INFO" => "/path/",
"QUERY_STRING" => "q=1"
)
assert_match %r(#<ActionDispatch::Request POST "https://example.com/path/\?q=1" for 1.2.3.4>), request.inspect
end
end