From a610f61e021a7a500520028769c6860219502897 Mon Sep 17 00:00:00 2001 From: Petrik Date: Thu, 3 Sep 2020 21:00:15 +0200 Subject: [PATCH] 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: "#" --- actionpack/lib/action_dispatch/http/request.rb | 4 ++++ actionpack/test/dispatch/request_test.rb | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index da7feb75ac..ed9ac6922e 100644 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -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]}") diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index d725e171c4..e0ee910227 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -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(#), request.inspect + end +end