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

Fail pattern match when JSON path not found #184

Merged
merged 2 commits into from
Nov 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion respx/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ def strip_base(self, value: Any) -> Any: # pragma: nocover
return value

def match(self, request: httpx.Request) -> Match:
value = self.parse(request)
try:
value = self.parse(request)
except Exception:
return Match(False)

# Match and strip base
if self.base:
Expand Down
14 changes: 4 additions & 10 deletions tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,21 +338,15 @@ def test_json_pattern(lookup, value, json, expected):
({"ham": [{"spam": "spam"}, {"egg": "yolk"}]}, "ham__1__egg", "yolk", True),
([{"name": "jonas"}], "0__name", "jonas", True),
({"pk": 123}, "pk", 123, True),
({"foo": {"bar": "baz"}}, "foo__ham", "spam", KeyError),
([{"name": "lundberg"}], "1__name", "lundberg", IndexError),
({"foo": {"bar": "baz"}}, "foo__ham", "spam", False),
([{"name": "lundberg"}], "1__name", "lundberg", False),
],
)
def test_json_pattern_path(json, path, value, expected):
request = httpx.Request("POST", "https://foo.bar/", json=json)
pattern = M(**{f"json__{path}": value})
if type(expected) is bool:
match = pattern.match(request)
assert bool(match) is expected
elif issubclass(expected, Exception):
with pytest.raises(expected):
pattern.match(request)
else:
raise AssertionError() # pragma: nocover
match = pattern.match(request)
assert bool(match) is expected


def test_invalid_pattern():
Expand Down