Skip to content

Shorten inspect on AbstractController::Base and ActionDispatch::Request #39439

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions actionpack/lib/abstract_controller/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def self.supports_path?
true
end

def inspect # :nodoc:
"#<#{self.class.name}:#{'%#016x' % (object_id << 1)}>"
end

private
# Returns true if the name can be considered an action because
# it has a method defined in the controller.
Expand Down
4 changes: 4 additions & 0 deletions actionpack/lib/action_dispatch/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ def ssl?
super || scheme == "wss"
end

def inspect # :nodoc:
"#<#{self.class.name} method=#{method.dump}, original_url=#{original_url.dump}, remote_ip=#{remote_ip.dump}, media_type=#{media_type.dump}>"
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]}")
Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/controller/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def test_response_has_default_headers
ensure
ActionDispatch::Response.default_headers = original_default_headers
end

def test_inspect
assert_match(/#<EmptyController:0x[0-9a-f]+>/, @empty.inspect)
end
end

class PerformActionTest < ActionController::TestCase
Expand Down
5 changes: 5 additions & 0 deletions actionpack/test/controller/metal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ def test_response_does_not_have_default_headers
ensure
ActionDispatch::Response.default_headers = original_default_headers
end

def test_inspect
controller = SimpleController.new
assert_match(/#<MetalControllerInstanceTests::SimpleController:0x[0-9a-f]+>/, controller.inspect)
end
end
18 changes: 18 additions & 0 deletions actionpack/test/dispatch/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1290,3 +1290,21 @@ def setup
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" => "glu.ttono.us:443",
"PATH_INFO" => "/path/of/some/uri",
"QUERY_STRING" => "mapped=1",
"CONTENT_TYPE" => "application/x-www-form-urlencoded; charset=utf-8",
)
assert_match %r(#<ActionDispatch::Request method="POST"), request.inspect
assert_match %r(original_url="https://glu.ttono.us/path/of/some/uri\?mapped=1"), request.inspect
assert_match %r(remote_ip="1.2.3.4"), request.inspect
assert_match %r(media_type="application/x-www-form-urlencoded">), request.inspect
end
end