From dc82635d92c4566f8f345e1ec539fe5f98611ceb Mon Sep 17 00:00:00 2001 From: Amazia Gur Date: Thu, 12 Dec 2024 18:41:03 +0000 Subject: [PATCH] bug: fix strict header enforcement --- mimicker/stub_group.py | 8 +++++--- tests/test_mimicker_get.py | 2 +- tests/test_stub_group.py | 15 ++++++++++----- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/mimicker/stub_group.py b/mimicker/stub_group.py index ec4be7d..41adc27 100644 --- a/mimicker/stub_group.py +++ b/mimicker/stub_group.py @@ -30,18 +30,20 @@ def match(self, method: str, path: str, matched_stub = None path_params = {} - for compiled_path, (status_code, response, response_func, headers)\ + for compiled_path, (status_code, response, response_func, headers) \ in self.stubs.get(method, {}).items(): match = compiled_path.match(path) if match: + headers_included = True if headers and request_headers: headers_included = all( header_name in request_headers and request_headers[header_name] == header_value for header_name, header_value in headers ) - if not headers_included: - continue + + if headers and not headers_included: + pass matched_stub = (status_code, response, response_func, headers) path_params = match.groupdict() diff --git a/tests/test_mimicker_get.py b/tests/test_mimicker_get.py index 3d1da3b..01c4734 100644 --- a/tests/test_mimicker_get.py +++ b/tests/test_mimicker_get.py @@ -78,4 +78,4 @@ def test_get_headers(mimicker_server): headers([("Content-Type", "text/plain")]) ) resp = Client().get('/hello', {"Content-Type": "application/json"}) - assert_that(resp.status_code, is_(404)) + assert_that(resp.status_code, is_(200)) diff --git a/tests/test_stub_group.py b/tests/test_stub_group.py index 0ef8e57..0f0d92e 100644 --- a/tests/test_stub_group.py +++ b/tests/test_stub_group.py @@ -1,4 +1,4 @@ -from hamcrest import assert_that, none, is_ +from hamcrest import assert_that, none, is_, not_none from mimicker.stub_group import StubGroup @@ -54,7 +54,7 @@ def dynamic_response(): assert_that(matched, is_((200, {}, dynamic_response, None))) -def test_no_match_given_unexpected_header(): +def test_match_given_unexpected_header(): stub_group = StubGroup() stub_group.add( "GET", "/hi", 200, @@ -62,10 +62,11 @@ def test_no_match_given_unexpected_header(): matched, _ = stub_group.match( "GET", "/hi", request_headers={"Content-Type": "application/json"}) - assert_that(matched, none()) + assert_that(matched, is_((200, {'message': 'hello'}, None, + [('Content-Type', 'text/plain')]))) -def test_match_given_expected_headers(): +def test_match_given_partial_expected_headers(): stub_group = StubGroup() stub_group.add("GET", "/hi", 200, {"message": "hello"}, @@ -77,4 +78,8 @@ def test_match_given_expected_headers(): matched, _ = stub_group.match( "GET", "/hi", request_headers={"Content-Type": "application/json"}) - assert_that(matched, none()) + assert_that(matched, is_((200, {'message': 'hello'}, + None, [ + ('Content-Type', 'application/json'), + ('Authorization', 'Bearer YOUR_TOKEN'), + ('Custom-Header', 'CustomValue')])))