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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eff2aeff88816ac0f1b48630974e916d0787e75711d12afbcfc8f5868ce97c97
|
|
| MD5 |
d1a07dace7081d6aa5890635385ad4b0
|
|
| BLAKE2b-256 |
d037874e95a0396d964f76a514f5941f523566bfd73c2aab59a951a6b32ca111
|
Provenance
The following attestation bundles were made for localstub-0.0.1.tar.gz:
Publisher:
publish.yml on jamesls/localstub
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
localstub-0.0.1.tar.gz -
Subject digest:
eff2aeff88816ac0f1b48630974e916d0787e75711d12afbcfc8f5868ce97c97 - Sigstore transparency entry: 2573753071
- Sigstore integration time:
-
Permalink:
jamesls/localstub@480ec3f9d2d2175215f89d80f46cb5d3594f18ce -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/jamesls
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@480ec3f9d2d2175215f89d80f46cb5d3594f18ce -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fadb0b678e1bf1c3f20b76a95a321b1deae0fb8bac4988d27eb76d007710aa13
|
|
| MD5 |
c04153da882c59ee8da8909bc679b722
|
|
| BLAKE2b-256 |
3d7aad3873755249ebc050423e1a33ad337ea261d788b42430718b2beac5c763
|
Provenance
The following attestation bundles were made for localstub-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on jamesls/localstub
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
localstub-0.0.1-py3-none-any.whl -
Subject digest:
fadb0b678e1bf1c3f20b76a95a321b1deae0fb8bac4988d27eb76d007710aa13 - Sigstore transparency entry: 2573753090
- Sigstore integration time:
-
Permalink:
jamesls/localstub@480ec3f9d2d2175215f89d80f46cb5d3594f18ce -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/jamesls
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@480ec3f9d2d2175215f89d80f46cb5d3594f18ce -
Trigger Event:
push
-
Statement type: