Skip to content

Commit

Permalink
Enable content__contains pattern (#236)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Lundberg <jonas@5monkeys.se>
  • Loading branch information
rjprins and lundberg authored Jul 7, 2023
1 parent 5a74249 commit 33438de
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,11 @@ respx.get("all://*.example.org/foo/")
### Content
Matches request raw *content*, using [eq](#eq) as default lookup.
> Key: `content`
> Lookups: [eq](#eq)
> Lookups: [eq](#eq), [contains](#contains)
``` python
respx.post("https://example.org/", content="foobar")
respx.post("https://example.org/", content=b"foobar")
respx.post("https://example.org/", content__contains="bar")
```

### Data
Expand Down
5 changes: 4 additions & 1 deletion respx/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def parse(self, request: httpx.Request) -> Any:


class Content(ContentMixin, Pattern):
lookups = (Lookup.EQUAL,)
lookups = (Lookup.EQUAL, Lookup.CONTAINS)
key = "content"
value: bytes

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

def _contains(self, value: Union[bytes, str]) -> Match:
return Match(self.value in value)


class JSON(ContentMixin, PathPattern):
lookups = (Lookup.EQUAL,)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ def test_url_pattern_hash():
[
(Lookup.EQUAL, b"foobar", True),
(Lookup.EQUAL, "foobar", True),
(Lookup.CONTAINS, b"bar", True),
(Lookup.CONTAINS, "bar", True),
(Lookup.CONTAINS, "baz", False),
],
)
def test_content_pattern(lookup, content, expected):
Expand Down

0 comments on commit 33438de

Please sign in to comment.