pytest-httpserver is a httpserver for pytest
Actively maintained. Last release 3 weeks ago, commits in 14 of the last 26 weeks. A safe choice.
pip install pytest-httpserverpytest_httpserver
HTTP server for pytest
Nutshell
This library is designed to help to test http clients without contacting the real http server. In other words, it is a fake http server which is accessible via localhost can be started with the pre-defined expected http requests and their responses.
Example
Handling a simple GET request
def test_my_client(
httpserver,
): # httpserver is a pytest fixture which starts the server
# set up the server to serve /foobar with the json
httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"})
# check that the request is served
assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"}
Handing a POST request with an expected json body
def test_json_request(
httpserver,
): # httpserver is a pytest fixture which starts the server
# set up the server to serve /foobar with the json
httpserver.expect_request(
"/foobar", method="POST", json={"id": 12, "name": "foo"}
).respond_with_json({"foo": "bar"})
# check that the request is served
assert requests.post(
httpserver.url_for("/foobar"), json={"id": 12, "name": "foo"}
).json() == {"foo": "bar"}