Some API calls don't have one right answer. A payment is pending, then processing, then succeeded. An export job is queued, then running, then done. To test how your app follows such a flow, the same request has to return a different response each time. Scenarios in Mockhop do that.
How a scenario works
A scenario has a trigger (a method and a path) and a list of steps. Every call to the trigger returns the next step:
- the first call gets step 1
- the second call gets step 2
- and so on
After the last step, you choose:
- Once: the last step repeats.
- Loop: it starts again from step 1.
Each step has its own status code, headers, body and an optional delay.
Example, trigger GET /charges/:id, mode Once:
| Step | Status | Body | Delay |
|---|---|---|---|
| 1 | 202 | {"status": "pending"} |
0 ms |
| 2 | 200 | {"status": "processing"} |
800 ms |
| 3 | 200 | {"status": "succeeded"} |
0 ms |
Step bodies are templates. They can read the request, for example the :id from the path:
{
"id": "{{ .pathParams.id }}",
"status": "pending"
}
One flow per client
Each client walks through the steps on its own. Mockhop identifies the session from, in this order:
- the
X-Mockhop-Sessionrequest header - the
mockhop_sessioncookie - the client's IP address
Two tests with different session values don't affect each other, even when they run at the same time.
$ curl -s $URL/payments/charges/ch_1 \
-H "X-Mockhop-Session: test-17"
Every response carries:
X-Mockhop-Scenario: which scenario answeredX-Mockhop-Scenario-Step: which step was served, for example2/3
Your test can assert on these headers. Browser apps can send the session header too.
Watching and resetting
The scenario page lists live sessions with their current step. You can reset one session or all of them; a reset session starts at step 1 on its next call. Sessions that haven't been used for 24 hours are removed automatically.
What scenarios don't do
A scenario doesn't store data. A POST doesn't change what a later GET returns; the steps are fixed in advance. Templates can still repeat values from the current request.
Good to know
- While a scenario is active, it answers its trigger before any endpoint on the same path.
- Scenarios are part of the Pro, Team and Business plans.
Step by step: Scenarios. Failure cases: Simulate API errors. Plans: pricing.