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

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.1.0
File Size Uploaded Provenance Actions
localstub-0.1.0.tar.gz 333.5 kB With provenance Details for localstub-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 445.9 kB

Release files / localstub-0.1.0.tar.gz

Download URL localstub-0.1.0.tar.gz
Size 333.5 kB
Tags Source
SHA-256 checksum
How to use checksums
086b5bf338ac00f823dcdb52222f8cbf7fe2192987feb2942103389c46b3d6c4
BLAKE2b-256 checksum
How to use checksums
32ac568136f86d6838f5291bfbe118c9648bd6555d1ba99480dba341634e1ec7
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 Oct 29, 2026.

Transparency log

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

Download URL localstub-0.1.0-py3-none-any.whl
Size 112.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0b9103cb4e1b9a33d66ddc757ba3ba2efe4a07e5f6d0d815deb11769ca8672ca
BLAKE2b-256 checksum
How to use checksums
b3ce01400948c5207731c232fd34e5d1f90c707a31c7ba2a98de1e91d65692a5
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 Oct 29, 2026.

Transparency log

Security for localstub 0.1.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for localstub-0.1.0.tar.gz

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 Oct 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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