Skip to main content

localstub

localstub is a Python library and CLI for testing HTTP clients against the behavior of a real server. It runs an asyncio HTTP server in your test process, returns whatever responses you configure, including error responses, slow responses, disconnects, and records each request exactly as it arrived on the wire for your tests to inspect.

A TLS proxy is provided so you can test your clients without having to make any config changes, most HTTP clients support HTTPS_PROXY and some environment variables for specifying a CA_BUNDLE. This also lets you inspect existing HTTP clients to see the exact bytes they are sending to servers.

[!WARNING] localstub is under active development. There may be breaking API changes until the 1.0.0 GA release.

import asyncio
import ssl

import httpx

from localstub import AsyncHTTPTestServer, AsyncTLSInterceptProxy


async def main() -> None:
    async with (
        AsyncHTTPTestServer() as server,
        AsyncTLSInterceptProxy(server=server) as proxy,
    ):
        # Configure the test server with whatever response you want.
        server.set_json_response({"ok": True})

        # Using a generic HTTP client here, but this would be YOUR
        # client library that's making calls to your remote service.
        async with httpx.AsyncClient(
            proxy=proxy.endpoint_url,
            verify=ssl.create_default_context(
                cafile=str(proxy.ca.ca_pem_path())
            ),
        ) as client:
            # Your client makes a request just like it normally
            # would, but it gets routed to the test server we've
            # setup which will return an `{"ok": true}` JSON response.
            response = await client.get("https://example.com/")
            assert response.json() == {"ok": True}

        # The server records every request it receives in
        # `server.requests`, so you can inspect the exact
        # request the client sent.
        request = server.requests[0]
        assert request.headers["host"] == "example.com"


asyncio.run(main())

Installation

uv add localstub

Development

This project requires Python 3.12 and uses uv to manage dependencies.

You can create a virtual environment with all the necessary dependencies by running:

uv sync --all-extras --dev

This will install all necessary dependencies and install this project in editable mode.

You can activate the venv with:

. .venv/bin/activate

Testing

Poe the Poet is the task runner used for this project, it's automatically installed as part of the dev dependencies. To see a list of available tasks, run the poe command with no args.

To run the tests for this project run:

poe test

Before submitting a PR, ensure the prcheck task runs successfully:

poe prcheck

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

localstub-0.0.1.tar.gz (218.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

localstub-0.0.1-py3-none-any.whl (72.0 kB view details)

Uploaded Python 3

File details

Details for the file localstub-0.0.1.tar.gz.

File metadata

  • Download URL: localstub-0.0.1.tar.gz
  • Upload date:
  • Size: 218.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for localstub-0.0.1.tar.gz
Algorithm Hash digest
SHA256 eff2aeff88816ac0f1b48630974e916d0787e75711d12afbcfc8f5868ce97c97
MD5 d1a07dace7081d6aa5890635385ad4b0
BLAKE2b-256 d037874e95a0396d964f76a514f5941f523566bfd73c2aab59a951a6b32ca111

See more details on using hashes here.

Provenance

The following attestation bundles were made for localstub-0.0.1.tar.gz:

Publisher: publish.yml on jamesls/localstub

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file localstub-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: localstub-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 72.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for localstub-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fadb0b678e1bf1c3f20b76a95a321b1deae0fb8bac4988d27eb76d007710aa13
MD5 c04153da882c59ee8da8909bc679b722
BLAKE2b-256 3d7aad3873755249ebc050423e1a33ad337ea261d788b42430718b2beac5c763

See more details on using hashes here.

Provenance

The following attestation bundles were made for localstub-0.0.1-py3-none-any.whl:

Publisher: publish.yml on jamesls/localstub

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page