Skip to content

Commit

Permalink
bug: fix strict header enforcement
Browse files Browse the repository at this point in the history
  • Loading branch information
Amazia Gur authored and Amazia Gur committed Dec 12, 2024
1 parent de2e974 commit dc82635
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
8 changes: 5 additions & 3 deletions mimicker/stub_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mimicker_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
15 changes: 10 additions & 5 deletions tests/test_stub_group.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -54,18 +54,19 @@ 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,
{"message": "hello"}, headers=[("Content-Type", "text/plain")])
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"},
Expand All @@ -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')])))

0 comments on commit dc82635

Please sign in to comment.