Intercept, simulate, replay, and observe outbound HTTP traffic from Python applications.
Project description
Routelab
Routelab is a Python library for intercepting outbound HTTP calls. It lets you rewrite requests, simulate failures, mock responses, record real traffic, replay it later, and keep a structured event log of what happened.
It is built around httpx.
What it does
- match requests by host, method, path, headers, or query values
- add request and response hooks
- inject latency, drops, timeouts, or forced status codes
- redirect traffic to another base URL
- return mock responses without touching application code
- record real traffic to a JSONL cassette
- replay traffic from a cassette later
- write a JSONL event log for inspection
Install
pip install -e .
For tests:
pip install -e .[dev]
An example
from routelab import Route, RouteLabSession, ResponseSpec
session = RouteLabSession(event_log_path="traffic.jsonl")
@session.before_request
def add_trace(req):
req.headers["x-trace-id"] = "dev-123"
return req
session.add(
Route.named("healthcheck")
.matching(path="/health")
.mock(ResponseSpec.json({"ok": True}))
)
client = session.client()
response = client.get("https://api.example.com/health")
print(response.json())
Routes
Routes are matched in order. The first matching route wins.
from routelab import Route
route = (
Route.named("vendor-delay")
.matching(host="api.vendor.com", method="GET", path_prefix="/v1")
.delay(1.25)
.status(503, body="temporary failure", rate=0.2)
)
Available actions in this version:
delay(seconds)status(code, body=None, headers=None, rate=1.0)drop(rate=1.0)timeout(rate=1.0)redirect(base_url)mock(response)record()replay()
Recording and replay
Session-wide recording:
from routelab import RouteLabSession
session = RouteLabSession(recorder_path="cassettes/vendor.jsonl")
client = session.client()
client.get("https://httpbin.org/get")
Replay from the same cassette later:
session = RouteLabSession(replay_path="cassettes/vendor.jsonl", replay_miss_policy="error")
client = session.client()
client.get("https://httpbin.org/get")
Replay miss policies:
error: raise an exception when no match is foundwarn: continue with a real request and annotate the event logpassthrough: continue with a real request quietly
You can also attach replay or record behavior to a specific route instead of the whole session.
Hooks
@session.before_request
def inject_auth(req):
req.headers["authorization"] = "Bearer local-token"
return req
@session.after_response
def tag_response(req, resp):
resp.headers["x-routelab"] = "1"
return resp
@session.on_error
def log_failure(req, exc):
print("request failed:", req.url, type(exc).__name__)
Event log
If event_log_path is set, routelab writes one JSON object per request. The log includes the URL, matched rule, final action, status code, elapsed time, replay hits, and errors.
You can inspect it with the CLI:
routelab inspect traffic.jsonl
Layout
routelab/
actions.py
cli.py
exceptions.py
logging.py
matcher.py
models.py
recorder.py
redaction.py
response.py
rules.py
session.py
Notes
This version is sync-first and httpx-only. It is meant to be simple and easy to extend.
Project details
Release history Release notifications | RSS feed
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 routelab_http-0.1.0.tar.gz.
File metadata
- Download URL: routelab_http-0.1.0.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
873e8b5b15f7531473365a0ed40ae181c9e2c5dc43e1a84ae6b5c80d76114463
|
|
| MD5 |
ee58cf685e733ce8284e6a309f5f4058
|
|
| BLAKE2b-256 |
9cec0291dff2fc230a6ed181740b292f8b820971ea6ff065ed73d6118209c0dd
|
File details
Details for the file routelab_http-0.1.0-py3-none-any.whl.
File metadata
- Download URL: routelab_http-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab820189e1c703a2b96651e4a67cd7c4653df66dcea3e3f6fa1f69515f10df6f
|
|
| MD5 |
cc1d29a6708b4888dec46cec62b23f1a
|
|
| BLAKE2b-256 |
a2667d37181849d2f3c7a651ae4d03cd4f4313acdc14bad1701c91478ee752a6
|