API and code · Mock server docs
Sessions and control calls
The mock server is part of every plan except Time only. Request access · See the plans
A session holds one test run's state: its scenario, state, login tokens, one pending approval, forced answers and the request log. Anyone with the key may make sessions (≤ 500 open per key). A session ends after 24 hours without a call.
Control calls live under /<key>/__mock/…. They are never matched against your endpoints (an endpoint path under
/__mock cannot be saved). Bodies are JSON. In the examples <base> is https://mock.spectrail.app/<key>.
A control call for a session takes the session from the URL (<base>/__mock/s/<session>/__mock/<call>) or the
X-Mock-Session header.
POST /__mock/sessions — make (or reset) a session
POST <base>/__mock/sessions
Content-Type: application/json
{"session": "checkout-run-1", "scenario": "New customer", "state": {"balance": 50}}
{ "sessionId": "checkout-run-1", "scenario": "New customer", "state": { "accountActive": true, "balance": 50, "orders": [] } }
Answer 201. All keys optional:
session: your own key, 1..100 characters of letters, digits,_.-. Left out: a random id is made.scenario: a scenario name (any case); else theX-Mock-Scenarioheader; else none (empty state).state: merged on top of the scenario's start state (top-level keys replace).
Create = reset. Naming a session that exists starts it fresh: the scenario's start state, every login token revoked, the pending approval and forced answers cleared, the log emptied. Use the same key at the start of every test run.
Errors: 404 unknown_scenario · 429 session_limit {max: 500} · 400 invalid.
POST /__mock/reset
Back to the session's scenario start state. Tokens, pending approval, forced answers and log are cleared.
Answer {"state": {…}}.
POST /__mock/scenario
{ "name": "Account not active" }
Switch the session to another scenario, starting fresh (as reset). Answer {"scenario": "Account not active", "state": {…}}.
GET /__mock/state · PATCH /__mock/state
GET → {"scenario": "…", "state": {…}}.
PATCH with a JSON object merges it into the state: objects merge key by key, lists and values replace.
PATCH <base>/__mock/s/checkout-run-1/__mock/state
Content-Type: application/json
{"balance": 120, "profile": {"city": "Leipzig"}}
Answer {"state": {…}} (the whole state after the merge).
GET|POST|DELETE /__mock/force
Queue answers for the next calls of one endpoint. See Test helpers.
GET /__mock/log
The session's request log, oldest first. See Test helpers.
Errors of control calls
| Answer | When |
|---|---|
404 {"error":"unknown_session"} | No session given, or none with that key (ended after 24 h idle, or reset by a new key). |
404 {"error":"unknown_scenario"} | No scenario of that name in the config. |
404 {"error":"Unknown mock control call","calls":[…]} | A name that is not a control call (or sessions under a session URL). |
| 405 | Wrong method (Allow lists the right ones). |
400 {"error":"invalid","message":"…"} | A body that is not the expected JSON. |
429 {"error":"session_limit","max":500} | 500 sessions are open for this key. |
404 {"error":"No such mock key"} | A wrong or old key. |
A test run, step by step
POST <base>/__mock/sessions {"session": "run-42", "scenario": "New customer"}
POST <base>/__mock/s/run-42/api/auth/login/ username=sam.tester%40example.com&password=Test-pass-1
GET <base>/__mock/s/run-42/api/account-status/ Authorization: Token mt_…
…
GET <base>/__mock/s/run-42/__mock/log