Skip to content

Commit 9d28fc6

Browse files
committed
add docs for readiness check
1 parent 5d9c324 commit 9d28fc6

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

doc/howto.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,41 @@ the ``httpserver`` fixture).
299299
return ("127.0.0.1", 8000)
300300
301301
302+
Waiting for server to be ready
303+
------------------------------
304+
305+
By default, the ``httpserver`` fixture ensures that the server is bound to the
306+
configured address and listening for incoming connections. While this is
307+
sufficient for most use cases, you can optionally wait for the server to be
308+
fully ready to serve HTTP requests before proceeding with your tests. This is
309+
particularly useful when your client has strict timeout requirements or when the
310+
HTTP server has a slow startup time.
311+
312+
To enable this check, *pytest-httpserver* issues an HTTP probe request to the
313+
server before your test starts to ensure the server is ready to serve requests.
314+
You can configure the timeout for this probe request as needed.
315+
316+
317+
.. warning::
318+
319+
This check is not supported in SSL mode, as the probe request is not
320+
configured to accept the self-signed certificate used by the server. To
321+
enable this feature with SSL, override the ``wait_for_server_ready()``
322+
method with your implementation.
323+
324+
325+
To enable this feature, set the ``startup_timeout`` keyword argument when
326+
initializing ``HTTPServer``. This parameter specifies the maximum time to wait
327+
for the server to become ready to serve HTTP requests.
328+
329+
330+
.. literalinclude :: ../tests/examples/test_howto_readiness.py
331+
:language: python
332+
333+
334+
In the case the server times out, the test will fail.
335+
336+
302337
Multi-threading support
303338
-----------------------
304339

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from collections.abc import Generator
2+
3+
import pytest
4+
import requests
5+
6+
from pytest_httpserver import HTTPServer
7+
8+
# By overriding make_httpserver fixture, you can override the object which is
9+
# used by the httpserver fixture. You can put this to your conftest.py so in
10+
# that case it will be used for all tests in the project, or you can put it to a
11+
# test file if you want to use it only for that file.
12+
13+
14+
@pytest.fixture(scope="session")
15+
def make_httpserver() -> Generator[HTTPServer, None, None]:
16+
server = HTTPServer(startup_timeout=10) # wait for 10 seconds for the server to be ready
17+
server.start()
18+
yield server
19+
server.clear()
20+
if server.is_running():
21+
server.stop()
22+
23+
24+
def test_example(httpserver: HTTPServer):
25+
# the server is ready at this point, you can add request handlers and start sending requests
26+
httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"})
27+
28+
response = requests.get(httpserver.url_for("/foobar"))
29+
assert response.status_code == 200
30+
assert response.json() == {"foo": "bar"}

0 commit comments

Comments
 (0)