Skip to content

Commit d71bba2

Browse files
mhadamcsernazs
authored andcommitted
Add expect method
1 parent de47983 commit d71bba2

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

pytest_httpserver/httpserver.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,22 @@ def clear_all_handlers(self):
972972
self.oneshot_handlers = RequestHandlerList()
973973
self.handlers = RequestHandlerList()
974974

975+
def expect(self, matcher: RequestMatcher, handler_type: HandlerType = HandlerType.PERMANENT) -> RequestHandler:
976+
"""
977+
Create and register a request handler.
978+
979+
:param matcher: :py:class:`RequestMatcher` used to match requests.
980+
:param handler_type: type of handler
981+
"""
982+
request_handler = RequestHandler(matcher)
983+
if handler_type == HandlerType.PERMANENT:
984+
self.handlers.append(request_handler)
985+
elif handler_type == HandlerType.ONESHOT:
986+
self.oneshot_handlers.append(request_handler)
987+
elif handler_type == HandlerType.ORDERED:
988+
self.ordered_handlers.append(request_handler)
989+
return request_handler
990+
975991
def expect_request(
976992
self,
977993
uri: str | URIPattern | Pattern[str],

tests/test_matcher.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import requests
2+
3+
from pytest_httpserver import HTTPServer
4+
5+
6+
def test_expect_method(httpserver: HTTPServer):
7+
expected_response = "OK"
8+
matcher = httpserver.create_matcher(uri="/test", method="POST")
9+
httpserver.expect(matcher).respond_with_data(expected_response)
10+
resp = requests.post(httpserver.url_for("/test"), json={"list": [1, 2, 3, 4]})
11+
assert resp.text == expected_response

tests/test_release.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def test_sdist_contents(build: Build, version: str):
234234
"test_urimatch.py",
235235
"test_wait.py",
236236
"test_with_statement.py",
237+
"test_matcher.py",
237238
},
238239
}
239240

0 commit comments

Comments
 (0)