Skip to content

Commit e2507b1

Browse files
authored
Keep patterns with empty value and equal lookup (#206)
1 parent 019a8a8 commit e2507b1

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

respx/patterns.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,6 @@ def M(*patterns: Pattern, **lookups: Any) -> Optional[Pattern]:
504504
extras = None
505505

506506
for pattern__lookup, value in lookups.items():
507-
if not value:
508-
continue
509-
510507
# Handle url pattern
511508
if pattern__lookup == "url":
512509
extras = parse_url_patterns(value)
@@ -535,6 +532,10 @@ def M(*patterns: Pattern, **lookups: Any) -> Optional[Pattern]:
535532
lookup = Lookup(lookup_name) if lookup_name else None
536533
pattern = P(value, lookup=lookup)
537534

535+
# Skip patterns with no value, exept when using equal lookup
536+
if not pattern.value and pattern.lookup is not Lookup.EQUAL:
537+
continue
538+
538539
patterns += (pattern,)
539540

540541
# Combine and merge patterns

tests/test_patterns.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ def test_match_context():
6666
assert match.context == {"host": "foo.bar", "slug": "baz"}
6767

6868

69+
@pytest.mark.parametrize(
70+
"kwargs,url,expected",
71+
[
72+
({"params__eq": {}}, "https://foo.bar/", True),
73+
({"params__eq": {}}, "https://foo.bar/?x=y", False),
74+
({"params__contains": {}}, "https://foo.bar/?x=y", True),
75+
],
76+
)
77+
def test_m_pattern(kwargs, url, expected):
78+
request = httpx.Request("GET", url)
79+
assert bool(M(host="foo.bar", **kwargs).match(request)) is expected
80+
81+
6982
@pytest.mark.parametrize(
7083
"lookup,value,expected",
7184
[
@@ -217,6 +230,8 @@ def test_path_pattern():
217230
(Lookup.EQUAL, "y=2", "https://foo.bar/?x=1", False),
218231
(Lookup.EQUAL, {"x": ANY}, "https://foo.bar/?x=1", True),
219232
(Lookup.EQUAL, {"y": ANY}, "https://foo.bar/?x=1", False),
233+
(Lookup.EQUAL, {}, "https://foo.bar/?x=1", False),
234+
(Lookup.EQUAL, {}, "https://foo.bar/", True),
220235
(Lookup.EQUAL, "x=1&y=2", "https://foo.bar/?x=1", False),
221236
(Lookup.EQUAL, "y=2&x=1", "https://foo.bar/?x=1&y=2", True),
222237
(Lookup.EQUAL, "y=3&x=2&x=1", "https://foo.bar/?x=1&x=2&y=3", False), # ordered

0 commit comments

Comments
 (0)