| title | Mock Server | |
|---|---|---|
| tags |
|
Run a configurable HTTP mock server for testing.
Create .mock.yaml or .mock.json in your project:
port: 8080
host: localhost
logging: true
routes:
- name: Get Users
method: GET
path: /api/users
status: 200
headers:
Content-Type: application/json
body: |
[
{"id": 1, "name": "John Doe"},
{"id": 2, "name": "Jane Smith"}
]
- name: Create User
method: POST
path: /api/users
status: 201
headers:
Content-Type: application/json
body: '{"id": 3, "created": true}'
- name: Delayed Response
method: GET
path: /api/slow
status: 200
delay: 2000
body: '{"message": "Delayed by 2 seconds"}'
- name: Error Response
method: GET
path: /api/error
status: 500
body: '{"error": "Internal server error"}'
- name: Catch-all
method: GET
path: /api/
pathType: prefix
status: 404
body: '{"error": "Not found"}'method: HTTP method (GET, POST, PUT, DELETE, etc.)path: URL path to matchstatus: HTTP status code to return
name: Descriptive name for the routepathType: Match type -exact(default),prefix, orregexheaders: Response headers (map of key-value pairs)body: Inline response body (string)bodyFile: Path to file containing response body (relative to config file)delay: Response delay in millisecondsdescription: Route documentation
Exact Match (default)
path: /api/users/1
pathType: exactPrefix Match
path: /api/
pathType: prefixMatches /api/users, /api/posts, etc.
Regex Match
path: /api/users/[0-9]+
pathType: regexMatches /api/users/1, /api/users/123, etc.
routes:
- name: Large Response
method: GET
path: /api/data
status: 200
headers:
Content-Type: application/json
bodyFile: responses/data.jsonFile path is relative to the config file location.
Auto-discover config:
restcli mock startSearches for .mock.yaml, .mock.yml, or .mock.json in:
mocks/directory- Current directory
../mocks/(parent directory's mocks folder)..(parent directory)
Specify config file:
restcli mock start path/to/config.mock.yamlServer runs in foreground. Press Ctrl+C to stop.
These commands require the TUI:
# Start TUI and press 'M' for mock server management
restcliPress M in TUI to open mock server modal.
- Shows available mock config files
- Press
sto start server (uses first found config) - Press
ESCto close
- Shows server address and config file
- Displays recent 10 requests with:
- Timestamp
- Method and path
- Status code (colored by result)
- Response time in milliseconds
- Matched route name
- Auto-refreshes every 500ms - logs update in real-time
- Press
sto stop server - Press
cto clear logs - Press
ESCto close
↑/kor↓/j: Line up/downPgUp/PgDn: Page up/downg: Go to topG: Go to bottom
port: 8080
host: localhost
logging: true
routes:
- name: List Items
method: GET
path: /items
status: 200
headers:
Content-Type: application/json
body: '[{"id": 1, "name": "Item 1"}]'
- name: Get Item
method: GET
path: /items/[0-9]+
pathType: regex
status: 200
headers:
Content-Type: application/json
body: '{"id": 1, "name": "Item 1"}'
- name: Create Item
method: POST
path: /items
status: 201
headers:
Content-Type: application/json
Location: /items/2
body: '{"id": 2, "name": "New Item"}'
- name: Update Item
method: PUT
path: /items/[0-9]+
pathType: regex
status: 200
body: '{"id": 1, "name": "Updated"}'
- name: Delete Item
method: DELETE
path: /items/[0-9]+
pathType: regex
status: 204port: 8080
host: localhost
logging: true
routes:
- name: Rate Limited
method: GET
path: /api/rate-limited
status: 429
headers:
Retry-After: "60"
body: '{"error": "Too many requests"}'
- name: Unauthorized
method: GET
path: /api/protected
status: 401
body: '{"error": "Unauthorized"}'
- name: Forbidden
method: GET
path: /api/admin
status: 403
body: '{"error": "Forbidden"}'
- name: Validation Error
method: POST
path: /api/users
status: 422
body: '{"errors": [{"field": "email", "message": "Invalid format"}]}'
- name: Server Error
method: GET
path: /api/error
status: 500
body: '{"error": "Internal server error"}'port: 8080
host: localhost
logging: true
routes:
- name: Fast Response
method: GET
path: /api/fast
status: 200
body: '{"speed": "fast"}'
- name: Slow Response
method: GET
path: /api/slow
status: 200
delay: 2000
body: '{"speed": "slow"}'
- name: Very Slow
method: GET
path: /api/very-slow
status: 200
delay: 5000
body: '{"speed": "very slow"}'After starting the mock server, create .http files to test against it:
### Test Get Users
GET http://localhost:8080/api/users
### Test Create User
POST http://localhost:8080/api/users
Content-Type: application/json
{
"name": "New User",
"email": "new@example.com"
}
### Test Error Response
GET http://localhost:8080/api/error
### Test Slow Endpoint
GET http://localhost:8080/api/slowRun in CLI:
restcli test-get-users.httpOr use TUI to execute and view results interactively.
Routes are matched in configuration file order - first match wins.
Order your routes from most specific to least specific:
Example:
routes:
# This matches first for exact /api/users
- method: GET
path: /api/users
status: 200
body: '{"users": []}'
# This matches /api/users/123
- method: GET
path: /api/users/[0-9]+
pathType: regex
status: 200
body: '{"user": {}}'
# This matches anything under /api/ not matched above
- method: GET
path: /api/
pathType: prefix
status: 404
body: '{"error": "Not found"}'Organize by Environment
mocks/
dev.mock.yaml
staging.mock.yaml
errors.mock.yaml
Use Body Files for Large Responses
mocks/
api.mock.yaml
responses/
users.json
posts.json
Enable Logging for Debugging
logging: trueLogs show in TUI modal and server console.
Test Different Ports
port: 3000 # Avoid conflicts with other servicesCombine with Request Files
Store mock configs and test requests together:
project/
mocks/
api.mock.yaml
tests/
test-api.http