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.
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.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| localstub-0.0.3.tar.gz | 222.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| localstub-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 296.6 kB
Release files / localstub-0.0.3.tar.gz
| Download URL | localstub-0.0.3.tar.gz |
|---|---|
| Size | 222.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
db3cdf67691177d8e6c27a1af9b2205826996289ad339ca4c7c4bcd46daa1310
|
|
BLAKE2b-256 checksum How to use checksums |
18a9bae47e4d39e24e0382aff458520f7816571cce6f7c010bc1ed5792943fa4
|
| 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 31, 2026.
Transparency logRelease files / localstub-0.0.3-py3-none-any.whl
| Download URL | localstub-0.0.3-py3-none-any.whl |
|---|---|
| Size | 74.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
62c7ff8930a73d013c2a1dce19fbdb5e7547c50915c043afd9a191bcb1d4916a
|
|
BLAKE2b-256 checksum How to use checksums |
f9a02cbe699e7d33ffd007c15067ce6e01a6f65ccfd9050894bf2dabd61c2fec
|
| 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 31, 2026.
Transparency log