examples Workflows and mocks
mocks.http
Mock routes, variants, matchers, selectors, latency, and fixture bodies.
start the local mock first
resterm _examples/mocks.httpRun this from the root of a clone of the Resterm repository.
Editor
Mock servers docs# Serve these mocks with `resterm mock _examples/mocks.http`, or open this file
# in the TUI and press `g Shift+M`.
# @const mock.base http://127.0.0.1:8080
### Health
# @mock method=GET path=/health name=healthy default=true
HTTP/1.1 200 OK
Content-Type: application/json
{"status":"ok"}
### Payment declined
# @mock method=POST path=/payments name=declined
# @match query={"mode":"decline"} headers={"X-Tenant":"demo"} json={"amount":0}
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json
{"error":"amount must be positive"}
### Payment accepted
# @mock method=POST path=/payments name=accepted default=true latency=150ms
HTTP/1.1 202 Accepted
Content-Type: application/json
X-Provider: sandbox
{"id":"pay_123","status":"pending"}
### Slow inventory lookup
# random gives every request its own delay between the bounds. The other forms
# are normal(mean,stddev) and jitter(base,spread), whose second argument may
# also be a percentage, as in jitter(200ms,20%).
# @mock method=GET path=/inventory latency=random(100ms,500ms)
HTTP/1.1 200 OK
Content-Type: application/json
{"sku":"widget","available":12}
### User fixture
# @mock method=GET path=/users/{id} name=user default=true
HTTP/1.1 200 OK
Content-Type: application/json
< ./mock-user.json
### Interpolated profile
# @mock method=GET path=/profiles/{id}
HTTP/1.1 200 OK
Content-Type: application/json
X-Request-ID: {{$uuid}}
{"id":{{json.path.id}},"generatedAt":"{{$timestampISO8601}}"}
### Poll payment
# @mock method=GET path=/payments/{id} sequence=polling sequence-key=path.id
HTTP/1.1 503 Service Unavailable
Retry-After: 1
{"id":{{json.path.id}},"status":"pending"}
---
HTTP/1.1 503 Service Unavailable
Retry-After: 1
{"id":{{json.path.id}},"status":"pending"}
---
HTTP/1.1 200 OK
Content-Type: application/json
{"id":{{json.path.id}},"status":"completed"}
### Payment webhook
# @mock method=POST path=/webhooks/payment
# @match headers={"Authorization":{"prefix":"Bearer "},"X-Correlation-ID":{"present":true}} json={"status":"completed"}
# @expect calls=1
HTTP/1.1 204 No Content
### Payment webhook rejected
# @mock method=POST path=/webhooks/payment name=rejected default=true
HTTP/1.1 401 Unauthorized
Content-Type: application/json
{"error":"missing or invalid webhook credentials"}
### Catalog for a supported client
# regex is unanchored, so ^ and $ are what limit X-Api-Version to a whole value
# like "v2". oneOf accepts any one of the listed values, while an exact array
# such as {"X-Env":["dev","prod"]} would require both of them, in that order.
# @mock method=GET path=/catalog name=supported
# @match headers={"User-Agent":{"contains":"Chrome"},"X-Api-Version":{"regex":"^v[0-9]+$"},"X-Env":{"oneOf":["dev","stage","prod"]}}
HTTP/1.1 200 OK
Content-Type: application/json
{"items":[{"id":"widget","price":499}]}
### Catalog for everything else
# @mock method=GET path=/catalog name=unsupported default=true
HTTP/1.1 426 Upgrade Required
Content-Type: application/json
{"error":"send a Chrome user agent, an X-Api-Version like v2, and a known X-Env"}
### Large order held for review
# A query value is always text, so an object there can only be a matcher and the
# operators are written bare. gte reads the value as a number, so a request with
# amount=none fails to match instead of erroring.
# Both forms are supported. This matcher is a single line, and the next scenario
# splits the same syntax over several comment lines.
# @mock method=POST path=/orders name=review
# @match query={"amount":{"gte":1000},"channel":{"oneOf":["web","ios"]},"dry-run":{"absent":true}}
HTTP/1.1 202 Accepted
Content-Type: application/json
{"id":"ord_9","status":"pending_review"}
### Bulk partner order queued
# The multiline form. An open bracket carries the matcher onto the next comment
# line, so this is the same matcher a single line would produce.
# @mock method=POST path=/orders name=bulk
# @match query={
# "amount": {"gte": 10000},
# "channel": {"oneOf": ["partner"]},
# "dry-run": {"absent": true}
# }
HTTP/1.1 202 Accepted
Content-Type: application/json
{"id":"ord_10","status":"queued_for_batch"}
### Order accepted
# @mock method=POST path=/orders name=accepted default=true
HTTP/1.1 201 Created
Content-Type: application/json
{"id":"ord_9","status":"accepted"}
### Account flagged for manual review
# json matches literal data while json-rules compares values. Both must match.
# An open bracket continues the matcher on the next comment line.
# @mock method=POST path=/accounts name=flagged
# @match json={"kind":"personal"}
# @match json-rules={
# "score": {"lt": 40},
# "tier": {"oneOf": ["free", "trial"]},
# "limits": {
# "daily": {"gte": 1, "lt": 100}
# }
# }
HTTP/1.1 403 Forbidden
Content-Type: application/json
{"error":"account needs manual review"}
### Account accepted
# @mock method=POST path=/accounts name=accepted default=true
HTTP/1.1 201 Created
Content-Type: application/json
{"id":"acct_7","status":"active"}
### Per-key rate limit
# @mock method=GET path=/quota sequence=quota sequence-key=header.X-Api-Key
HTTP/1.1 200 OK
Content-Type: application/json
{"remaining":1}
---
HTTP/1.1 200 OK
Content-Type: application/json
{"remaining":0}
---
HTTP/1.1 429 Too Many Requests
Retry-After: 60
Content-Type: application/json
{"error":"quota exhausted for this API key"}
### Legacy endpoint must stay unused
# @mock method=GET path=/v1/legacy
# @expect calls=0
HTTP/1.1 410 Gone
Content-Type: application/json
{"error":"use /payments instead"}
### Fallback for unmocked GET paths
# @mock method=GET path=/{rest...} default=true
HTTP/1.1 404 Not Found
Content-Type: application/json
{"error":"no mock for this path","path":{{json.path.rest}}}
### Poll payment until it completes
# @name poll-payment
GET {{mock.base}}/payments/pay_42
### Watch the inventory latency move between runs
# @name fetch-inventory
GET {{mock.base}}/inventory
### Spend the quota for one API key
# @name spend-quota
GET {{mock.base}}/quota
X-Api-Key: demo-key
### Fetch the catalog as a supported client
# @name fetch-catalog
GET {{mock.base}}/catalog
User-Agent: Mozilla/5.0 Chrome/120.0 Safari/537.36
X-Api-Version: v2
X-Env: stage
### Place an order large enough to need review
# @name place-order
POST {{mock.base}}/orders?amount=2500&channel=web
Content-Type: application/json
{"sku":"widget","qty":5}
### Place a bulk order that goes to the batch queue
# The multiline matcher selects this one. channel=partner is not in the web/ios
# list, so the review scenario above does not match.
# @name place-bulk-order
POST {{mock.base}}/orders?amount=25000&channel=partner
Content-Type: application/json
{"sku":"widget","qty":50}
### Submit an account that gets flagged
# @name submit-account
POST {{mock.base}}/accounts
Content-Type: application/json
{"kind":"personal","score":12,"tier":"trial","limits":{"daily":5}}
### Submit an account that is accepted
# This score misses the rule and uses the default scenario. The gt field remains
# ordinary request data.
# @name submit-account-ok
POST {{mock.base}}/accounts
Content-Type: application/json
{"kind":"personal","score":80,"tier":"free","limits":{"daily":5},"gt":1}
### Deliver the payment webhook
# The mock.* assertion needs the TUI-owned server (`g Shift+M`).
# @name deliver-webhook
# @assert mock.received({method:"POST", path:"/webhooks/payment", json:{status:"completed"}})
POST {{mock.base}}/webhooks/payment
Authorization: Bearer demo-token
X-Correlation-ID: {{$uuid}}
Content-Type: application/json
{"status":"completed"}{
"id": "user_123",
"name": "Ada"
}