Repository: lundberg/respx
Pull request #236 (2023-06-13 19:56:44)

Enable content__contains pattern
Thanks for the awesome lib! :pray: 
I stumbled on some test cases where everything was the same except for the request contents and the contents only differed in some small details. This change seemed simple enough.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/lundberg/respx/pull/236?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jonas+Lundberg) Report
Patch coverage: **`100.00`**% and no project coverage change.
> Comparison is base [(`b014780`)](https://app.codecov.io/gh/lundberg/respx/commit/b014780bde8e82a65fc6bb02d62b89747189565c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jonas+Lundberg) 100.00% compared to head [(`48bbaa2`)](https://app.codecov.io/gh/lundberg/respx/pull/236?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jonas+Lundberg) 100.00%.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##            master      #236    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           20        20            
  Lines         2756      2758     +2     
  Branches       417       303   -114     
==========================================
+ Hits          2756      2758     +2     
```


| [Impacted Files](https://app.codecov.io/gh/lundberg/respx/pull/236?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jonas+Lundberg) | Coverage Δ | |
|---|---|---|
| [tests/test\_patterns.py](https://app.codecov.io/gh/lundberg/respx/pull/236?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jonas+Lundberg#diff-dGVzdHMvdGVzdF9wYXR0ZXJucy5weQ==) | `100.00% <ø> (ø)` | |
| [respx/patterns.py](https://app.codecov.io/gh/lundberg/respx/pull/236?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jonas+Lundberg#diff-cmVzcHgvcGF0dGVybnMucHk=) | `100.00% <100.00%> (ø)` | |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/lundberg/respx/pull/236?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jonas+Lundberg).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jonas+Lundberg).

rjprins: Friendly nudge :ping_pong: I think this is ready to merge.

Patch:
diff --git a/docs/api.md b/docs/api.md
index 4c8686f..7432767 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -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
diff --git a/respx/patterns.py b/respx/patterns.py
index 24316cc..ed4c51d 100644
--- a/respx/patterns.py
+++ b/respx/patterns.py
@@ -483,7 +483,7 @@ class ContentMixin:
 
 
 class Content(ContentMixin, Pattern):
-    lookups = (Lookup.EQUAL,)
+    lookups = (Lookup.EQUAL, Lookup.CONTAINS)
     key = "content"
     value: bytes
 
@@ -492,6 +492,9 @@ class Content(ContentMixin, Pattern):
             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,)


Test patch:
diff --git a/tests/test_patterns.py b/tests/test_patterns.py
index ed44f77..e704b40 100644
--- a/tests/test_patterns.py
+++ b/tests/test_patterns.py
@@ -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):