From 6229e2376551f201b0d47b0944b6678353e21cc6 Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Thu, 17 Jun 2021 15:29:09 +0100 Subject: [PATCH] allow headers to be pattern-matched as well --- lib/httpx/pmatch_extensions.rb | 11 +++++++++-- test/extensions/response_pattern_match.rb | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/httpx/pmatch_extensions.rb b/lib/httpx/pmatch_extensions.rb index 104919de..6bf75b33 100644 --- a/lib/httpx/pmatch_extensions.rb +++ b/lib/httpx/pmatch_extensions.rb @@ -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 diff --git a/test/extensions/response_pattern_match.rb b/test/extensions/response_pattern_match.rb index 64260214..dae979f9 100644 --- a/test/extensions/response_pattern_match.rb +++ b/test/extensions/response_pattern_match.rb @@ -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"