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.

Documentation

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

Metadata

Release files for localstub 0.0.2

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

Source distribution (sdist)

Source distribution for localstub 0.0.2
File Size Uploaded
localstub-0.0.2.tar.gz 220.8 kB Details

Built distribution (wheel)

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

Total release size: 294.7 kB

Release files / localstub-0.0.2.tar.gz

Download URL localstub-0.0.2.tar.gz
Size 220.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e1f2dda3af6a4b5efdae82ba8b75e03bc73369dbfa327baa50f51d01cab1c6a0
BLAKE2b-256 checksum
How to use checksums
9d02d3bec69811107c6dcf40d662a23388688b700ddfbdac114d566bc5d14f38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 28, 2026.

Transparency log

Release files / localstub-0.0.2-py3-none-any.whl

Download URL localstub-0.0.2-py3-none-any.whl
Size 73.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ac48908b16197c652e76ece1f01a5003ad09898ed75f577ffc433acb97c77fb
BLAKE2b-256 checksum
How to use checksums
d864dcae67ac01fe8726612d2fda09220ca12c24c3cacf6af61671e0e3307aca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 28, 2026.

Transparency log

Release history Release notifications | RSS feed

0.0.3

2 release files

This release

0.0.2 This release

2 release files

0.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