From 80843e97cd36c1056949e2ca09ba6d4717747c19 Mon Sep 17 00:00:00 2001 From: mhadam Date: Thu, 16 Jan 2025 21:57:11 -0500 Subject: [PATCH] Add expect method --- pytest_httpserver/httpserver.py | 16 ++++++++++++++++ tests/test_matcher.py | 11 +++++++++++ tests/test_release.py | 1 + 3 files changed, 28 insertions(+) create mode 100644 tests/test_matcher.py diff --git a/pytest_httpserver/httpserver.py b/pytest_httpserver/httpserver.py index d29a6c7..1a5fa75 100644 --- a/pytest_httpserver/httpserver.py +++ b/pytest_httpserver/httpserver.py @@ -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], diff --git a/tests/test_matcher.py b/tests/test_matcher.py new file mode 100644 index 0000000..dee0ca4 --- /dev/null +++ b/tests/test_matcher.py @@ -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 diff --git a/tests/test_release.py b/tests/test_release.py index afbd849..765c2e2 100644 --- a/tests/test_release.py +++ b/tests/test_release.py @@ -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", }, }