Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: fix strict header enforcement #11

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
14 changes: 10 additions & 4 deletions tests/test_stub_group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from hamcrest import assert_that, none, is_

from mimicker.stub_group import StubGroup


Expand Down Expand Up @@ -54,18 +55,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 +79,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')])))
Loading