Skip to content

Commit 03a3d78

Browse files
committed
httpserver: http response code for no handler can be specified
The http response code sent when no handler is found for the request can be changed. It is set to 500 by default. Fixes #44.
1 parent 25e9214 commit 03a3d78

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

pytest_httpserver/httpserver.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,13 @@ class HTTPServer: # pylint: disable=too-many-instance-attributes
553553
:py:class:`Request` and :py:class:`Response` object which represents the
554554
incoming request and the outgoing response which happened during the lifetime
555555
of the server.
556+
557+
.. py:attribute:: no_handler_status_code
558+
559+
Attribute containing the http status code (int) which will be the response
560+
status when no matcher is found for the request. By default, it is set to *500*
561+
but it can be overridden to any valid http status code such as *404* if needed.
562+
556563
"""
557564

558565
DEFAULT_LISTEN_HOST = "localhost"
@@ -581,6 +588,7 @@ def __init__(self, host=DEFAULT_LISTEN_HOST, port=DEFAULT_LISTEN_PORT, ssl_conte
581588
self.default_waiting_settings = WaitingSettings()
582589
self._waiting_settings = copy(self.default_waiting_settings)
583590
self._waiting_result = queue.LifoQueue(maxsize=1)
591+
self.no_handler_status_code = 500
584592

585593
def clear(self):
586594
"""
@@ -593,6 +601,7 @@ def clear(self):
593601
self.clear_log()
594602
self.clear_all_handlers()
595603
self.permanently_failed = False
604+
self.no_handler_status_code = 500
596605

597606
def clear_assertions(self):
598607
"""
@@ -941,7 +950,7 @@ def respond_nohandler(self, request: Request):
941950
self._set_waiting_result(False)
942951
text = "No handler found for request {!r}.\n".format(request)
943952
self.add_assertion(text + self.format_matchers())
944-
return Response("No handler found for this request", 500)
953+
return Response("No handler found for this request", self.no_handler_status_code)
945954

946955
def respond_permanent_failure(self):
947956
"""
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
The http response code sent when no handler is found for the
5+
request can be changed. It is set to 500 by default.

tests/test_permanent.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def test_unexpected_request(httpserver: HTTPServer):
7979
httpserver.check_assertions()
8080

8181

82+
def test_no_handler_status_code(httpserver: HTTPServer):
83+
httpserver.no_handler_status_code = 404
84+
assert requests.get(httpserver.url_for("/foobar")).status_code == 404
85+
86+
8287
def test_server_cleared_for_each_test(httpserver: HTTPServer):
8388
assert httpserver.log == []
8489
assert httpserver.assertions == []

0 commit comments

Comments
 (0)