Skip to content
Open
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
28 changes: 28 additions & 0 deletions lib/net/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@
# there is a protocol error.
#
class Net::HTTPResponse
# Valid keys for pattern matching via #deconstruct_keys.
PATTERN_MATCHING_KEYS = %i[code message http_version body content_type].freeze

class << self
# true if the response has a body.
def body_permitted?
Expand Down Expand Up @@ -408,6 +411,31 @@ def body=(value)

alias entity body #:nodoc: obsolete

# Returns a hash of response attributes for pattern matching.
#
# Valid keys are: +:code+, +:message+, +:http_version+, +:body+, +:content_type+
#
# Example:
#
# response = Net::HTTP.get_response(uri)
# case response
# in code: '200', content_type: /json/
# JSON.parse(response.body)
# in code: '404'
# handle_not_found
# end
#
def deconstruct_keys(keys)
valid_keys = keys ? PATTERN_MATCHING_KEYS & keys : PATTERN_MATCHING_KEYS
valid_keys.to_h do |key|
value = case key
when :body then @body
else public_send(key)
end
[key, value]
end
end

private

# :nodoc:
Expand Down
40 changes: 40 additions & 0 deletions test/net/http/test_httpresponse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,46 @@ def test_inspect_response
assert_equal '#<Net::HTTPUnknownResponse ??? test response readbody=true>', res.inspect
end

def test_deconstruct_keys
res = Net::HTTPOK.new('1.1', '200', 'OK')
res.body = 'test body'
res['content-type'] = 'text/plain'

keys = res.deconstruct_keys(nil)
assert_equal '200', keys[:code]
assert_equal 'OK', keys[:message]
assert_equal '1.1', keys[:http_version]
assert_equal 'test body', keys[:body]
assert_equal 'text/plain', keys[:content_type]
end

def test_deconstruct_keys_with_specific_keys
res = Net::HTTPOK.new('1.1', '200', 'OK')
res.body = 'test body'

keys = res.deconstruct_keys([:code, :message])
assert_equal({code: '200', message: 'OK'}, keys)
end

def test_pattern_matching
res = Net::HTTPOK.new('1.1', '200', 'OK')
res['content-type'] = 'application/json'

begin
matched = instance_eval <<~RUBY, __FILE__, __LINE__ + 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a similar trick as to what we had used around pattern matching for the 2.6 to 2.7 push, so I'm repeating it here. The other methods should function even without syntax bridges like this.

case res
in code: '200', content_type: /json/
true
else
false
end
RUBY
assert_equal true, matched
rescue SyntaxError
omit "Pattern matching requires Ruby 2.7+"
end
end

private

def dummy_io(str)
Expand Down