Skip to content

Commit 33438de

Browse files
rjprinslundberg
andauthored
Enable content__contains pattern (#236)
Co-authored-by: Jonas Lundberg <[email protected]>
1 parent 5a74249 commit 33438de

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

docs/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,11 @@ respx.get("all://*.example.org/foo/")
301301
### Content
302302
Matches request raw *content*, using [eq](#eq) as default lookup.
303303
> Key: `content`
304-
> Lookups: [eq](#eq)
304+
> Lookups: [eq](#eq), [contains](#contains)
305305
``` python
306306
respx.post("https://example.org/", content="foobar")
307307
respx.post("https://example.org/", content=b"foobar")
308+
respx.post("https://example.org/", content__contains="bar")
308309
```
309310

310311
### Data

respx/patterns.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def parse(self, request: httpx.Request) -> Any:
483483

484484

485485
class Content(ContentMixin, Pattern):
486-
lookups = (Lookup.EQUAL,)
486+
lookups = (Lookup.EQUAL, Lookup.CONTAINS)
487487
key = "content"
488488
value: bytes
489489

@@ -492,6 +492,9 @@ def clean(self, value: Union[bytes, str]) -> bytes:
492492
return value.encode()
493493
return value
494494

495+
def _contains(self, value: Union[bytes, str]) -> Match:
496+
return Match(self.value in value)
497+
495498

496499
class JSON(ContentMixin, PathPattern):
497500
lookups = (Lookup.EQUAL,)

tests/test_patterns.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ def test_url_pattern_hash():
311311
[
312312
(Lookup.EQUAL, b"foobar", True),
313313
(Lookup.EQUAL, "foobar", True),
314+
(Lookup.CONTAINS, b"bar", True),
315+
(Lookup.CONTAINS, "bar", True),
316+
(Lookup.CONTAINS, "baz", False),
314317
],
315318
)
316319
def test_content_pattern(lookup, content, expected):

0 commit comments

Comments
 (0)