allow headers to be pattern-matched as well

This commit is contained in:
HoneyryderChuck 2021-06-17 15:29:09 +01:00
parent c7704b6e15
commit 6229e23765
2 changed files with 19 additions and 2 deletions

View File

@ -3,11 +3,11 @@
module HTTPX
module ResponsePatternMatchExtensions
def deconstruct
[@status, @headers.to_h, @body]
[@status, @headers, @body]
end
def deconstruct_keys(_keys)
{ status: @status, headers: @headers.to_h, body: @body }
{ status: @status, headers: @headers, body: @body }
end
end
@ -21,6 +21,13 @@ module HTTPX
end
end
module HeadersPatternMatchExtensions
def deconstruct
to_a
end
end
Headers.include HeadersPatternMatchExtensions
Response.include ResponsePatternMatchExtensions
ErrorResponse.include ErrorResponsePatternMatchExtensions
end

View File

@ -3,6 +3,16 @@
module ResponsePatternMatchTests
include HTTPX
def test_response_headers_deconstruct
response = Response.new(request, 200, "2.0", { "host" => "google.com", "content-type" => "application/json" })
case response
in [_, [*, ["content-type", content_type], *], _]
assert content_type == "application/json"
else
raise "unexpected response"
end
end
def test_response_deconstruct
response_with_body = Response.new(request, 200, "2.0", { "x-with-body" => "true" })
response_with_body << "OK"