Skip to content

Add a delay to a mock

Before you begin

Role
Editor or higher.
You need
  • A mock

A delay makes a mock wait before it answers, so you can see how your client copes with a slow API.

Set it in the mock editor, or in the project’s configuration document under Config as code.

type The mock waits Fields
fixed The same time on every request milliseconds, above 0
random Any time between two bounds, each as likely as the next min and max, both above 0, with max greater than min
realistic Mostly close to a typical time, and now and then much longer, like a real network median, 200 if left out; sigma, 0.4 if left out; both above 0
{ "type": "fixed", "milliseconds": 1500 }
{ "type": "random", "min": 200, "max": 1200 }
{ "type": "realistic", "median": 300, "sigma": 0.5 }

For a realistic delay, median is the typical wait in milliseconds: half the requests wait less, half wait more. sigma sets how far the waits spread. With 0.4, about two requests in three wait between two thirds of the median and one and a half times it; a larger sigma spreads them further.

In the mock editor:

  1. On the project’s Mocks tab, select the mock’s name.

  2. Under What it answers, open Delay and choose Fixed, Random range or Realistic (lognormal). Each response has its own delay: open the response’s tab first.

  3. Enter Milliseconds for a fixed delay, Minimum (ms) and Maximum (ms) for a random one, or Median (ms) and Sigma for a realistic one. Left empty, the median is 200 and sigma 0.4.

  4. Select Save mock. MockHub shows Mock saved.

To remove the delay, choose No delay and save.

In the configuration document:

  1. Open the project’s Settings tab.

  2. Under Config as code, select Open config.

  3. Select Edit a copy below. The project’s document appears in Config document (JSON); if the box already held text, confirm with Replace it.

  4. Find the mock in mocks by its name, and add a delay to the response of the entry in its responses that should wait:

    {
    "name": "List orders",
    "status": "Active",
    "request": { "method": "GET", "url": "/orders" },
    "responses": [
    {
    "key": "default",
    "name": "Default",
    "response": {
    "statusCode": 200,
    "headers": { "Content-Type": "application/json" },
    "body": "{\"orders\": []}",
    "delay": { "type": "fixed", "milliseconds": 1500 }
    }
    }
    ],
    "responseSelection": { "mode": "default", "defaultResponseKey": "default" }
    }
  5. Select Preview changes, and check that the plan lists the mock under Mocks as Update, with nothing to delete.

  6. Select Apply. MockHub shows Applied.

A mock with several responses, or a sequence over them, keeps a delay on each response, so a response a request asks for with X-MockHub-Response waits as long as that response says, and one without a delay answers at once.

From the next request the mock waits before answering, and its request logs show the longer response time and how much of it was the delay. Edit a project’s configuration in the browser explains the rest of the configuration page.

  • Each plan sets the longest a mock may wait. A longer delay is shortened to the plan’s maximum, without an error; the limits page lists it per plan. The wait also counts towards the gateway’s time limit for an answer; see How MockHub handles a request.
  • A delay does not take up one of the workspace’s requests in flight until the workspace has more delayed answers waiting at once than its plan allows (Delayed responses at once), or their bodies together pass 64 KiB for each answer it allows.
  • While a chaos profile with latency is active and covers the mock, its latency replaces the mock’s own delay, whichever response answers, rather than adding to it. To slow many mocks at once, a chaos profile is the simpler tool.
  • The mock editor states the plan’s maximum under the delay’s fields, and checks the numbers before it saves.
  • Try it in the mock editor waits for the delay too, up to the plan’s maximum.
  • A streamed response has no delay: its Time to first chunk takes the delay’s place, and the Delay control gives way to the Stream card. An AI model response sets its own pace from its first-token time and speed.

Report a problem with this page