Skip to content

Commit 09bf47c

Browse files
committed
howto.rst: add text about using custom request handlers
This has been improved recently to handle assertion errors and to be able to obtain the unhandled exceptions, so this is now documented.
1 parent 5a2dd4d commit 09bf47c

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

doc/howto.rst

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,97 @@ In case you don't want to change the defaults, you can provide the
268268
assert requests.get(httpserver.url_for("/"), headers={"X-Bar": "bar"}).status_code == 200
269269
assert requests.get(httpserver.url_for("/"), headers={"X-Bar": "BAR"}).status_code == 200
270270
271+
Using custom request handler
272+
----------------------------
273+
In the case the repsonse is not static, for example it depends on the request,
274+
you can pass a function to the ``respond_with_handler`` function. This function
275+
will be called with a request object and it should return a Response object.
276+
277+
.. code:: python
278+
279+
from werkzeug.wrappers import Request, Response
280+
from random import randint
281+
282+
def test_expected_request_handler(httpserver: HTTPServer):
283+
def handler(request: Request):
284+
return Response(str(random.randint(1, 10))
285+
286+
httpserver.expect_request("/foobar").respond_with_handler(handler)
287+
288+
289+
The above code implements a handler which returns a random number between 1 and
290+
10. Not particularly useful but shows that the handler can return any computed
291+
or derived value.
292+
293+
In the response handler you can also use the ``assert`` statement, similar to
294+
the tests, but there's a big difference. As the server is running in its own
295+
thread, this will cause a HTTP 500 error returned, and the exception registered
296+
into a list. To get that error, you need to call ``check_assertions()`` method
297+
of the httpserver.
298+
299+
In case you want to ensure that there was no other exception raised which was
300+
unhandled, you can call the ``check_handler_errors()`` method of the httpserver.
301+
302+
Two notable examples for this:
303+
304+
.. code:: python
305+
306+
def test_check_assertions_raises_handler_assertions(httpserver: HTTPServer):
307+
def handler(_):
308+
assert 1 == 2
309+
310+
httpserver.expect_request("/foobar").respond_with_handler(handler)
311+
312+
requests.get(httpserver.url_for("/foobar"))
313+
314+
# if you leave this "with" statement out, check_assertions() will break
315+
# the test by re-raising the assertion error caused by the handler
316+
# pytest will pick this exception as it was happened in the main thread
317+
with pytest.raises(AssertionError):
318+
httpserver.check_assertions()
319+
320+
httpserver.check_handler_errors()
321+
322+
323+
def test_check_handler_errors_raises_handler_error(httpserver: HTTPServer):
324+
def handler(_):
325+
raise ValueError("should be propagated")
326+
327+
httpserver.expect_request("/foobar").respond_with_handler(handler)
328+
329+
requests.get(httpserver.url_for("/foobar"))
330+
331+
httpserver.check_assertions()
332+
333+
# if you leave this "with" statement out, check_handler_errors() will
334+
# break the test with the original exception
335+
with pytest.raises(ValueError):
336+
httpserver.check_handler_errors()
337+
338+
339+
If you want to call both methods (``check_handler_errors()`` and
340+
``check_assertions()``) you can call the ``check()`` method, which will call
341+
these.
342+
343+
344+
.. code:: python
345+
346+
def test_check_assertions(httpserver: HTTPServer):
347+
def handler(_):
348+
assert 1 == 2
349+
350+
httpserver.expect_request("/foobar").respond_with_handler(handler)
351+
352+
requests.get(httpserver.url_for("/foobar"))
353+
354+
httpserver.check()
355+
356+
.. note::
357+
The scope of the errors checked by the ``check()`` method may
358+
change in the future - it is added to check all possible errors happened in
359+
the server.
360+
361+
271362
Customizing host and port
272363
-------------------------
273364

0 commit comments

Comments
 (0)