Stubbing Guide¶
The same stub can be defined three ways — pick whichever fits your workflow. No SDK is required for CI-only users.
Defining a stub¶
HTTP methods¶
All standard HTTP methods are supported.
from mimicker.mimicker import mimicker, get, post, put, delete, patch
mimicker(8080).routes(
get("/items").status(200).body({"items": []}),
post("/items").status(201).body({"created": True}),
put("/items/1").status(200).body({"updated": True}),
delete("/items/1").status(204),
patch("/items/1").status(200).body({"patched": True}),
)
Custom response headers¶
Response delay¶
Simulate slow APIs or test timeout handling.
Testing timeouts
Set delay longer than your client's timeout to reliably trigger ReadTimeout in tests.
Sequence responses¶
Return a different response on each successive call — ideal for simulating retries, flaky services, or multi-step workflows.
After the last step, the final step repeats by default. Pass cycle=True to loop:
Rate limiting¶
Simulate 429 Too Many Requests responses.
Rate limiting is configured in Python only (not yet in the YAML schema).
mimicker(8080).routes(
get("/api/data")
.status(200)
.body({"data": "ok"})
.rate_limit(max_requests=5, window_seconds=60)
)
# First 5 requests → 200
# 6th+ request → 429 Too Many Requests
Customize the 429 response:
mimicker(8080).routes(
get("/api/data")
.rate_limit(
max_requests=5,
window_seconds=60,
status_code=429,
body={"error": "rate_limit_exceeded", "retry_after": 60},
headers=[("Retry-After", "60")],
)
)
Per-client rate limiting via a header key:
Multiple routes¶
Loading YAML from Python¶
This is useful when you want to manage stubs in YAML but start the server from Python test fixtures.
Related¶
- Path & Query Params — dynamic path variables and query string matching
- Dynamic Responses — compute responses from request data with
response_func - Stub File Schema — complete YAML/JSON field reference
- Python API — full method reference