File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments