API and code · Mock server docs

State, rules and templates

The mock server is part of every plan except Time only. Request access · See the plans

State

A session's state is one JSON object (≤ 256 KB). It starts as the scenario's start state (plus state given to POST /__mock/sessions), gets a login user's state merged on top at login, and changes through:

  • the sets of endpoints after a 2xx answer;
  • a confirm that runs a pending approval's sets;
  • PATCH /__mock/state (deep merge) and POST /__mock/reset / scenario (start again).

Without a session there is no state: {{state.…}} gives null and presets with when never answer.

Paths

sets keys and when keys name a place in the state:

PathMeans
cashActivea top-level key
account.cash.activenested objects
depots[*].linkedToCashAccountthat field on every item of the list depots
depots[depotId=dp-1].linkedToCashAccounton the items whose depotId is dp-1 (compared as text)
depots[depotId={{path.depotId}}]the selector value from a template (one whole template)

Names: letters, digits, _ -. A path is ≤ 200 characters and ≤ 10 steps. when keys do not take list selectors.

sets

{
  "sets": [
    { "key": "cashActive", "value": false },
    { "op": "set", "key": "profile.city", "value": "{{body.city}}" },
    { "key": "depots[depotId={{path.depotId}}].linkedToCashAccount", "value": true },
    { "key": "depots[*].inWithdrawalWindow", "value": false },
    { "op": "remove", "key": "depots[depotId={{path.depotId}}]" },
    { "op": "remove", "key": "lastReason" },
    { "key": "hasOpenDepot", "value": false },
    { "op": "logout" }
  ]
}
opWhat
set (default)Put value at key. Missing objects on the way are made. With a selector as the last step, the matched items are replaced.
removeDrop the matched list items, or the key. Nothing matched → no change.
logoutEnd the session's login token after this call. The next call with it gets the unauthorized answer.

sets run in order, each reading the state as the step before left it. A selector that matches nothing changes nothing (no error). Derived values are not computed: when removing the last depot should make hasOpenDepot false, add that set yourself.

Templates

A string that is exactly one template is replaced by the value it names, keeping its type: a number stays a number, a list stays a list. Text around a template is not filled ("Hello {{body.name}}" stays as written).

TemplateValue
{{state.<path>}}the session state (dot path; null without a session)
{{path.<name>}}a path placeholder of the call
{{query.<name>}}a query value (the last one of a repeated name)
{{body.<name>}}a body field (JSON or form; dotted names reach nested JSON)
{{auth.token}}the login token the call issued or carried (null without a session)
{{approval.token}}, {{approval.id}}the reference of the approval this answer starts

Templates work in preset bodies, scenario preset bodies and sets values / selector values. A missing value gives null.

{
  "presets": [
    {
      "status": 200,
      "body": {
        "id": "{{path.orderId}}",
        "cashAccount": { "active": "{{state.cashActive}}", "balance": "{{state.balance}}" },
        "depots": "{{state.depots}}"
      }
    }
  ]
}

when and match

A preset answers when every key of its when (state) and match (request) fits:

  • when: {"<state path>": <value>}, equal by type (false is not "false", 0 is not "0").
  • match: {"body.<f>" | "query.<f>" | "path.<f>": <value>}, compared as text (1 matches "1").

For one status: the first preset with a when / match that fits; else the first one with neither; else fake values from the fields. In a session the scenario's presets are tried first, the same way.

{
  "presets": [
    { "status": 200, "when": { "accountActive": false }, "final": true, "body": { "status": false } },
    { "status": 200, "match": { "body.b": "1" }, "body": { "status": true, "via": "app" } },
    { "status": 200, "body": { "status": true, "via": "tan" } }
  ]
}