A tool for reading JSON/YAML files containing API endpoint definitions and running a local server that provides endpoints according to the given definitions.
- Supports GET, POST, PUT, DELETE, and PATCH methods
- Customizable HTTP status codes
- Delay simulation to mimic real API response times
- Support for JSON and YAML definition formats
- CORS support for frontend development
- Auto-reload when definition files change
- Simple CLI interface
- Python 3.10+
Install the dependencies:
pip install -r requirements.txtOr install as a package (with dev dependencies):
pip install -e ".[dev]"Run the mock API server with the command:
python mock_server.py <definition_file> [options]| Option | Short | Default | Description |
|---|---|---|---|
--port |
-p |
8000 |
Server port |
--host |
-H |
127.0.0.1 |
Server host |
--cors |
disabled | Enable CORS for all origins | |
--reload |
disabled | Auto-reload when definition file changes |
# Basic usage
python mock_server.py example_api.json
# Custom port with YAML definition
python mock_server.py example_api.yaml --port 3000
# Enable CORS for frontend development
python mock_server.py example_api.json --cors
# Enable auto-reload during development
python mock_server.py example_api.json --reload
# Bind to all interfaces with CORS and auto-reload
python mock_server.py example_api.json --host 0.0.0.0 --port 5000 --cors --reloadAPI definition files can be in JSON or YAML format. Basic structure:
{
"endpoints": [
{
"path": "/users",
"method": "GET",
"response": {
"data": []
},
"status_code": 200,
"delay": 0.5
}
]
}| Property | Required | Default | Description |
|---|---|---|---|
path |
Yes | - | Endpoint path (e.g., /users, /users/{id}) |
method |
Yes | - | HTTP method (GET, POST, PUT, DELETE, PATCH) |
response |
No | {} |
Response data to be returned |
status_code |
No | 200 |
HTTP status code |
delay |
No | 0 |
Delay time in seconds before returning the response |
See example_api.json and example_api.yaml for example API definitions.
This project includes comprehensive unit tests:
pip install -e ".[dev]"
python -m pytest test_mock_server.py -vApache-2.0
Please create a pull request if you want to add features or fix bugs.