Recipes

Simulate Slow or Unstable APIs

Retry logic, loading spinners, and timeout handling only reveal their bugs when the network misbehaves. This recipe makes a dependency slow and flaky on demand, combining delay, the random helper, and proxying so successful requests still hit the real backend.

The recipe

mock "GET /reports/{id}" {
delay = 1000
body = <<-EOF
{{#lt (random 0 100) 30}}
{{setStatus 500}}
{ "message": "Internal server error" }
{{else}}
{{proxy}}
{{/lt}}
EOF
}

Started with mocko -u https://demo-api.mockoapp.net mocks (a live demo backend that serves GET /reports/{id}), this makes every reports request take an extra second, and roughly 30% of them fail with a 500. The rest are served by the real backend.

How it works

  • delay = 1000 waits one second before answering, and it applies to the proxied branch too, so even successful requests feel slow.
  • {{random 0 100}} rolls a fresh number per request (inclusive on both ends), and the lt block turns it into a 30% failure rate. Adjust the threshold to taste.
  • {{proxy}} forwards the request untouched, so response shapes stay real. Without a proxy target you can render a fixture body in the else branch instead.

Variations

Trigger client timeouts. Crank the delay past your client's timeout budget. delay goes up to 300000 milliseconds:

mock "GET /reports/{id}" {
delay = 30000
body = "{{proxy}}"
}

A hard outage. Keep a disabled mock in the file and flip it on when you want the dependency down. Enable it from the file or the UI:

mock "* /reports/{id}" {
enabled = false
status = 503
format = "json"
body = <<-EOF
{ "error": "Service unavailable" }
EOF
}

Failures on demand. For a toggle you can flip at runtime without touching files, gate the failure branch on a flag, as in the outage example on the Flags page. Tests can flip it through the SDK.

Random failures make test runs nondeterministic by design. Use them for manual exploration and resilience demos; for CI, prefer the flag-gated variant so each test controls exactly when the failure happens.