Skip to content

Commit dcc0abf

Browse files
committed
example_pytest.py: add new example for pytest usage
As we can configure the port number by overriding the httpserver_listen_address fixture, a new example is added to demonstrate this.
1 parent a497368 commit dcc0abf

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

example_pytest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Run this code as 'pytest example_pytest.py'
2+
3+
import pytest
4+
import requests
5+
from pytest_httpserver import HTTPServer
6+
7+
# specify where the server should bind to
8+
# you can return 0 as the port, in this case it will bind to a free (ephemeral) TCP port
9+
@pytest.fixture
10+
def httpserver_listen_address():
11+
return ("127.0.0.1", 8000)
12+
13+
14+
# specify httpserver fixture
15+
def test_oneshot_and_permanent_happy_path1(httpserver: HTTPServer):
16+
# define some request handlers
17+
# more details in the documentation
18+
httpserver.expect_request("/permanent").respond_with_data("OK permanent")
19+
httpserver.expect_oneshot_request("/oneshot1").respond_with_data("OK oneshot1")
20+
httpserver.expect_oneshot_request("/oneshot2").respond_with_data("OK oneshot2")
21+
22+
# query those handlers with a real HTTP client (requests in this example but could by anything)
23+
# the 'url_for' method formats the final URL, so there's need to wire-in any ports
24+
assert requests.get(httpserver.url_for("/permanent")).text == "OK permanent"
25+
assert requests.get(httpserver.url_for("/oneshot1")).text == "OK oneshot1"
26+
assert requests.get(httpserver.url_for("/oneshot2")).text == "OK oneshot2"
27+
assert requests.get(httpserver.url_for("/permanent")).text == "OK permanent"
28+
29+
assert len(httpserver.oneshot_handlers) == 0

0 commit comments

Comments
 (0)