Spin up a mock HTTP server from a YAML/JSON spec with hot reload, dynamic rules, and request logging
Project description
mockapi
Spin up a fully functional mock HTTP server from a YAML or JSON spec in one line. Dynamic response rules, request logging, hot reload, and a CLI tool. Zero dependencies.
Installation
pip install mockapi # JSON specs only
pip install mockapi[yaml] # adds YAML support
Quick Start
# Start a mock server from a YAML spec
mockapi serve spec.yaml
# Or use it in Python tests
from mockapi import MockAPI
with MockAPI("spec.yaml") as api:
response = requests.get(api.url + "/users")
assert response.json() == [{"id": 1, "name": "Alice"}]
Spec Format
YAML (spec.yaml)
routes:
- method: GET
path: /users
status: 200
body:
- id: 1
name: Alice
- id: 2
name: Bob
- method: GET
path: /users/{id}
status: 200
body:
id: 1
name: Alice
rules:
- match:
path_param:
id: "999"
then:
status: 404
body:
error: User not found
- method: POST
path: /users
status: 201
body:
id: 3
name: Created
- method: GET
path: /slow
body: {}
delay: 1.5
- method: GET
path: /broken
error: timeout
JSON (spec.json)
{
"routes": [
{"method": "GET", "path": "/ping", "status": 200, "body": {"ok": true}},
{"method": "POST", "path": "/echo", "status": 201, "body": {"received": true}}
]
}
CLI
# Basic usage
mockapi serve spec.yaml
# Custom port and host
mockapi serve spec.json --port 9000 --host 0.0.0.0
# Disable hot reload
mockapi serve spec.yaml --no-reload
# Quiet mode (no request logs)
mockapi serve spec.yaml --quiet
Output:
=======================================================
mockapi — Mock API Server
URL: http://127.0.0.1:8888
Spec: spec.yaml
Routes: 4
Hot reload: ✅
=======================================================
GET /users → 200
GET /users/{id} → 200
POST /users → 201
GET /slow → 200 [delay 1.5s]
Python API
Context Manager (for tests)
from mockapi import MockAPI
with MockAPI("spec.yaml", port=8080) as api:
# api.url = "http://127.0.0.1:8080"
res = requests.get(api.url + "/users")
assert res.status_code == 200
Programmatic Routes
api = MockAPI(port=8888, verbose=False)
api.add_route("GET", "/ping", body={"ok": True})
api.add_route("POST", "/users", status=201, body={"created": True})
api.add_route("GET", "/slow", body={}, delay=2.0)
api.add_route("GET", "/fail", error="timeout")
api.add_route("GET", "/users/{id}", body={"id": 1})
api.start()
Dynamic Rules
Conditional responses based on path params, query strings, or request body:
api.add_route("GET", "/users/{id}", body={"id": 1, "name": "Alice"}, rules=[
{
"match": {"path_param": {"id": "999"}},
"then": {"status": 404, "body": {"error": "Not found"}}
},
{
"match": {"query": {"format": "minimal"}},
"then": {"status": 200, "body": {"id": 1}}
}
])
Request History & Stats
# All requests
history = api.history
# [{"method": "GET", "path": "/users", "status": 200, "duration_ms": 1.2, ...}]
# Requests for a specific path
api.history_for("/users")
# Summary stats
api.stats()
# {"total": 42, "by_status": {200: 38, 404: 4}, "by_method": {"GET": 40, "POST": 2}}
# Clear history
api.clear_history()
Hot Reload
The spec file is watched for changes automatically. Edit spec.yaml while the server is running and routes update within 1 second — no restart needed.
Error Simulation
error value |
Behaviour |
|---|---|
timeout |
Hangs for 30 seconds |
connection_reset |
Closes connection immediately |
empty |
Returns empty response body |
API Reference
MockAPI
| Method | Description |
|---|---|
__init__(spec, host, port, hot_reload, verbose) |
Create server |
add_route(method, path, status, body, ...) |
Add a route |
start() |
Start background server |
stop() |
Stop server |
reload() |
Manually reload spec from file |
history |
List of all recorded requests |
history_for(path) |
Requests for a specific path |
stats() |
Summary statistics |
clear_history() |
Clear request log |
url |
Base URL string |
Running Tests
pip install pytest pyyaml
pytest tests/ -v
License
MIT © prabhay759
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 mockapiv2-1.0.0.tar.gz.
File metadata
- Download URL: mockapiv2-1.0.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10d8705153b0a94897c6c829dacd1ed94fc937a3126698aea9df80f2bd613540
|
|
| MD5 |
eb6f1804f0ec170b1b398943a3468c8b
|
|
| BLAKE2b-256 |
179cac13f3b5b2a5258406114e15ccdedfe4226fa54ba721bdddcd92b144f286
|
Provenance
The following attestation bundles were made for mockapiv2-1.0.0.tar.gz:
Publisher:
publish.yml on prabhay759/mockapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mockapiv2-1.0.0.tar.gz -
Subject digest:
10d8705153b0a94897c6c829dacd1ed94fc937a3126698aea9df80f2bd613540 - Sigstore transparency entry: 1220687411
- Sigstore integration time:
-
Permalink:
prabhay759/mockapi@53690f8991ccc6ba649b0f8bc82b937f8348b6d5 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/prabhay759
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@53690f8991ccc6ba649b0f8bc82b937f8348b6d5 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file mockapiv2-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mockapiv2-1.0.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
253fa34970fbe70685b92099ec23d5459b100400114d6853bfd8040ae8a16c75
|
|
| MD5 |
baf801db2f09349d524facfe175de27b
|
|
| BLAKE2b-256 |
9556c1c21e23eeb5fcc128c5eeb1768998d9aa8a9f031742cdbab5c0718fafb6
|
Provenance
The following attestation bundles were made for mockapiv2-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on prabhay759/mockapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mockapiv2-1.0.0-py3-none-any.whl -
Subject digest:
253fa34970fbe70685b92099ec23d5459b100400114d6853bfd8040ae8a16c75 - Sigstore transparency entry: 1220687505
- Sigstore integration time:
-
Permalink:
prabhay759/mockapi@53690f8991ccc6ba649b0f8bc82b937f8348b6d5 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/prabhay759
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@53690f8991ccc6ba649b0f8bc82b937f8348b6d5 -
Trigger Event:
workflow_dispatch
-
Statement type: