Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pytest_httpserver/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,22 @@ def clear_all_handlers(self):
self.oneshot_handlers = RequestHandlerList()
self.handlers = RequestHandlerList()

def expect(self, matcher: RequestMatcher, handler_type: HandlerType = HandlerType.PERMANENT) -> RequestHandler:
"""
Create and register a request handler.

:param matcher: :py:class:`RequestMatcher` used to match requests.
:param handler_type: type of handler
"""
request_handler = RequestHandler(matcher)
if handler_type == HandlerType.PERMANENT:
self.handlers.append(request_handler)
elif handler_type == HandlerType.ONESHOT:
self.oneshot_handlers.append(request_handler)
elif handler_type == HandlerType.ORDERED:
self.ordered_handlers.append(request_handler)
return request_handler

def expect_request(
self,
uri: str | URIPattern | Pattern[str],
Expand Down
11 changes: 11 additions & 0 deletions tests/test_matcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import requests

from pytest_httpserver import HTTPServer


def test_expect_method(httpserver: HTTPServer):
expected_response = "OK"
matcher = httpserver.create_matcher(uri="/test", method="POST")
httpserver.expect(matcher).respond_with_data(expected_response)
resp = requests.post(httpserver.url_for("/test"), json={"list": [1, 2, 3, 4]})
assert resp.text == expected_response
1 change: 1 addition & 0 deletions tests/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def test_sdist_contents(build: Build, version: str):
"test_urimatch.py",
"test_wait.py",
"test_with_statement.py",
"test_matcher.py",
},
}

Expand Down
Loading