-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: add howto for the new expect() method
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import requests | ||
from werkzeug import Request | ||
|
||
from pytest_httpserver import HTTPServer | ||
from pytest_httpserver import RequestMatcher | ||
|
||
|
||
class MyMatcher(RequestMatcher): | ||
def match(self, request: Request) -> bool: | ||
match = super().match(request) | ||
if not match: # existing parameters didn't match -> return with False | ||
return match | ||
|
||
# match the json's "value" key | ||
# if it it can be divided by 2, it returns True, False otherwise | ||
json = request.json | ||
if isinstance(json, dict) and isinstance(json.get("value"), int): | ||
return json["value"] % 2 == 0 | ||
|
||
return False | ||
|
||
|
||
def test_custom_request_matcher(httpserver: HTTPServer): | ||
httpserver.expect(MyMatcher("/foo")).respond_with_data("OK") | ||
|
||
resp = requests.post(httpserver.url_for("/foo"), json={"value": 42}) | ||
resp.raise_for_status() | ||
assert resp.text == "OK" | ||
|
||
resp = requests.post(httpserver.url_for("/foo"), json={"value": 43}) | ||
assert resp.status_code == 500 |