Skip to main content

Build Status Documentation Status License: MIT codecov Code style: black Ruff Downloads pre-commit

pytest_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"}

You can also use the library without pytest. There's a with statement to ensure that the server is stopped.

with HTTPServer() as httpserver:
    # 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
    print(requests.get(httpserver.url_for("/foobar")).json())

Documentation

Please find the API documentation at https://pytest-httpserver.readthedocs.io/en/latest/.

Features

You can set up a dozen of expectations for the requests, and also what response should be sent by the server to the client.

Requests

There are three different types:

  • permanent: this will be always served when there's match for this request, you can make as many HTTP requests as you want
  • oneshot: this will be served only once when there's a match for this request, you can only make 1 HTTP request
  • ordered: same as oneshot but the order must be strictly matched to the order of setting up

You can also fine-tune the expected request. The following can be specified:

  • URI (this is a must)
  • HTTP method
  • headers
  • query string
  • data (HTTP body of the request)
  • JSON (HTTP body loaded as JSON)

Responses

Once you have the expectations for the request set up, you should also define the response you want to send back. The following is supported currently:

  • respond arbitrary data (string or bytearray)
  • respond a json (a python dict converted in-place to json)
  • respond a Response object of werkzeug
  • use your own function

Similar to requests, you can fine-tune what response you want to send:

  • HTTP status
  • headers
  • data

Behave support

Using the BlockingHTTPServer class, the assertion for a request and the response can be performed in real order. For more info, see the test, the howto and the API documentation.

Missing features

  • HTTP/2
  • Keepalive
  • TLS

Donation

Currently, this project is based heavily on werkzeug and pytest.

Werkzeug does all the heavy lifting behind the scenes, parsing HTTP request and defining Request and Response objects, which are currently transparent in the API.

If you wish to donate to werkzeug: https://palletsprojects.com/donate

Pytest is the de-facto test library for python.

If you wish to donate to pytest: https://opencollective.com/pytest

Release files for pytest_httpserver 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest_httpserver 1.1.3
File Size Uploaded
pytest_httpserver-1.1.3.tar.gz 68.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest_httpserver 1.1.3
File Interpreter ABI Platform
pytest_httpserver-1.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 89.9 kB

Release files / pytest_httpserver-1.1.3.tar.gz

Download URL pytest_httpserver-1.1.3.tar.gz
Size 68.9 kB
Tags Source
SHA-256 checksum
How to use checksums
af819d6b533f84b4680b9416a5b3f67f1df3701f1da54924afd4d6e4ba5917ec
BLAKE2b-256 checksum
How to use checksums
f1d8def15ba33bd696dd72dd4562a5287c0cba4d18a591eeb82e0b08ab385afc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.12.8 Linux/6.1.132

Release files / pytest_httpserver-1.1.3-py3-none-any.whl

Download URL pytest_httpserver-1.1.3-py3-none-any.whl
Size 21.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f84757810233e19e2bb5287f3826a71c97a3740abe3a363af9155c0f82fdbb9
BLAKE2b-256 checksum
How to use checksums
0dd2dfc2f25f3905921c2743c300a48d9494d29032f1389fc142e718d6978fb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.12.8 Linux/6.1.132

Release history Release notifications | RSS feed

1.1.5

2 release files

1.1.4

2 release files

This release

1.1.3 This release

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.12

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

1 release file

0.2.1

2 release files

0.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page