Files
mockapi/README.md

159 lines
3.3 KiB
Markdown

# MockAPI
OpenAPI Mock Server Generator - Generate functional mock APIs from OpenAPI 3.x specifications.
## Features
- **OpenAPI 3.x Support**: Parse and validate OpenAPI specifications
- **Mock Server Generation**: Generate working mock servers using connexion
- **Random Data Generation**: Create realistic test data from JSON schemas using Faker
- **Configurable Response Delays**: Simulate network latency with fixed or random delays
- **Error Simulation**: Simulate HTTP error responses for testing
- **Request Validation**: Validate incoming requests against OpenAPI spec
- **Hot-Reload**: Auto-restart server on spec file changes
- **YAML Configuration**: Configure mock behavior via `mockapi.yaml`
## Installation
```bash
pip install -e .
```
Or install from source:
```bash
git clone https://7000pct.gitea.bloupla.net/7000pctAUTO/mockapi.git
cd mockapi
pip install -e ".[dev]"
```
## Quick Start
```bash
# Validate an OpenAPI spec
mockapi validate examples/petstore.yaml
# Start a mock server
mockapi start examples/petstore.yaml
# Generate a summary
mockapi generate examples/petstore.yaml
```
## CLI Commands
### validate
Validate an OpenAPI specification file:
```bash
mockapi validate spec.yaml
```
### start
Start a mock server from an OpenAPI spec:
```bash
mockapi start spec.yaml --port 8080 --host 0.0.0.0
```
Options:
- `--port, -p`: Port number (default: 8080)
- `--host, -h`: Host to bind to (default: 0.0.0.0)
- `--delay, -d`: Fixed response delay in milliseconds
- `--random-delay`: Use random delays instead of fixed
- `--config, -c`: Path to mockapi.yaml configuration
- `--watch, -w`: Enable hot-reload on spec changes
- `--verbose, -v`: Enable verbose output
### generate
Generate a summary from an OpenAPI spec:
```bash
mockapi generate spec.yaml
```
### show-config
Show current configuration:
```bash
mockapi show-config
```
## Configuration
Create a `mockapi.yaml` file to configure mock behavior:
```yaml
port: 8080
host: 0.0.0.0
delay: 100
random_delay: false
seed: 42
validate_requests: true
error_probability: 0.0
```
### Configuration Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| port | int | 8080 | Server port |
| host | string | 0.0.0.0 | Server host |
| delay | int | 0 | Fixed delay in ms |
| random_delay | bool | false | Use random delays |
| seed | int | 42 | Random seed |
| validate_requests | bool | true | Validate requests |
| error_probability | float | 0.0 | Error probability |
### Environment Variables
- `MOCKAPI_PORT`: Override default port
- `MOCKAPI_HOST`: Override default host
- `MOCKAPI_SEED`: Override random seed
## Hot Reload
Enable automatic server restart when the spec file changes:
```bash
mockapi start spec.yaml --watch
```
## Request Validation
By default, incoming requests are validated against the OpenAPI spec. Disable with:
```yaml
validate_requests: false
```
## Error Simulation
Use the `x-mock-config` extension in your OpenAPI spec:
```yaml
paths:
/users:
get:
x-mock-config:
errorProbability: 0.1
errorCode: 500
errorMessage: "Service unavailable"
```
## Development
```bash
# Install development dependencies
pip install -e ".[dev]"
# Run tests
pytest tests/ -v
# Run linting
ruff check src/
# Type checking
mypy src/mockapi/
```
## License
MIT License