From 1f36f2294a44e0a951547719cf06ed3593587369 Mon Sep 17 00:00:00 2001 From: woodyza Date: Wed, 31 Jan 2024 04:24:20 +1300 Subject: [PATCH] fix missing header detection for the non-strict case (#702) --- responses/matchers.py | 2 +- responses/tests/test_matchers.py | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/responses/matchers.py b/responses/matchers.py index 1a915e64..20af1be6 100644 --- a/responses/matchers.py +++ b/responses/matchers.py @@ -421,7 +421,7 @@ def _compare_with_regex(request_headers: Union[Mapping[Any, Any], Any]) -> bool: else: if not v == request_headers[k]: return False - elif strict_match: + else: return False return True diff --git a/responses/tests/test_matchers.py b/responses/tests/test_matchers.py index e353265a..796b93e4 100644 --- a/responses/tests/test_matchers.py +++ b/responses/tests/test_matchers.py @@ -626,7 +626,7 @@ def run(): assert_reset() -def test_request_matches_headers_no_match(): +def test_request_header_value_mismatch_raises(): @responses.activate def run(): url = "http://example.com/" @@ -650,6 +650,27 @@ def run(): assert_reset() +def test_request_headers_missing_raises(): + @responses.activate + def run(): + url = "http://example.com/" + responses.add( + method=responses.GET, + url=url, + json={"success": True}, + match=[matchers.header_matcher({"x-custom-header": "foo"})], + ) + + with pytest.raises(ConnectionError) as excinfo: + requests.get(url, headers={}) + + msg = str(excinfo.value) + assert ("Headers do not match: {} doesn't match {x-custom-header: foo}") in msg + + run() + assert_reset() + + def test_request_matches_headers_strict_match(): @responses.activate def run():