Skip to content

Choose the response that answers

Before you begin

Role
Editor or higher.
You need
  • A mock

One mock can hold several response variants: the success, the validation errors, the empty list. One of them is the default response and answers every ordinary request. A test that needs another asks for it in a single request, without changing the default for anyone else.

This page adds a 422 to the mock for POST /orders, and then asks for it.

  1. Open the mock from the project’s Mocks tab.

  2. Under What it answers, select Response, then A blank response. A new tab opens with a blank response and a key of its own. On a mock imported from an OpenAPI contract, the menu also lists the contract’s responses under From the contract, to add one from the contract.

  3. Enter a Response name, such as Invalid email, choose 422 as its Status code, and write its Response body.

  4. Select Save mock.

The tabs only choose which response you are editing. Choosing a tab, renaming a response or changing its status never changes which response answers. Duplicate response copies the open response into a new one, and Delete response removes the open one when you save.

  1. Open How a response is chosen, below the responses, and leave A default response selected beside Answer with.

  2. Choose the response in Default response. Alternatively, open the response and select Make default on save.

  3. Select Save mock.

Until you save, How this mock responds still describes the saved default, and marks your draft Unsaved changes. Changing the default affects requests from everyone using this mock. It is an ordinary save of the mock: it adds a version to the history, and requests get the new default a moment after the save.

From a terminal, mockhub mock default <mock> <response-key> does the same through a versioned update; see CLI commands.

Open Use this response in a request, below the response, and select Copy header or Copy curl. The header has the form X-MockHub-Response: <mock reference>:<response key>, the mock reference being the last 12 characters of the mock’s identifier:

Terminal window
curl -X POST https://orders-api.mockhub.dev/orders \
-H 'Content-Type: application/json' \
-H 'X-MockHub-Response: 123456789abc:invalid-email' \
-d '{"email":"nobody"}'

MockHub finds the mock for the request as it always does, from the method, path and other request rules, and then answers with the response you named instead of the default. Nobody else’s requests change, and neither does the mock.

Copy the value from the editor instead of writing it: it holds the end of the mock’s identifier, so a copy of the mock in another project has a header of its own. Save to copy a request for this response. appears for a response you have not saved yet, and a request copied for a response with unsaved edits is answered with the saved output until you save. A warning appears there when the saved mock refuses the header: it answers with a sequence, or overrides are turned off.

It never changes Because
The default response Only a saved edit of the mock does that
Which mock answers The header takes no part in matching; it picks among the responses of the mock found
Whether the mock is active An inactive mock answers nothing, whatever the header says
What a scenario, a data store or chaos does They still apply to the response that answers

An override is not a way past anything: the request still has to match, and project access, plan limits and quotas apply as they do to any request.

MockHub never answers the default in place of a response it was asked for, and never forwards such a request to an upstream:

Status Problem When
400 Response selector malformed The header is empty, repeated, or not <mock reference>:<response key>
422 Response selection unavailable The response cannot be served: it is not one of the mock’s, the mock is inactive, does not match the request or is another mock’s, or has overrides turned off

The 422 is the same answer for every reason, so a caller learns nothing about what exists in the project. To find the reason, open the request in the request logs, or see A response you asked for was refused.

To refuse overrides for a mock, turn off A request may ask for another response. A request that then sends the header is refused, not answered with the default.

Rules choose a response from the request itself: a 402 when the amount is over 99, a 202 for one tenant’s dry runs, a 404 for order 000. One mock holds them all, so you don’t need a second mock on the same path for each case.

  1. Add the responses the rules answer with, as in Add a response. Alternatively, under What it answers, select Response and then A response chosen by rule…, which adds a blank response and a rule that answers with it.

  2. Open How a response is chosen and select By rules beside Answer with.

  3. Select Add a rule. Under When every condition holds, select Add a condition for each thing the request must meet: a header, a query parameter, a path parameter, a cookie, a form field or the body, each with the same kinds of match as the mock’s request rules. A body condition can use has JSON path and equals JSON. Then choose the rule’s response in Answer with.

  4. Add more rules, and move them up or down: the first rule whose conditions all hold answers. A rule keeps at least one condition and the mock at least one rule; to answer without rules, choose A default response beside Answer with.

  5. Choose the response for every other request in Otherwise answer with.

  6. Select Save mock.

For POST /orders/{id}, three rules read as:

Rule When Answer with
1 The body has JSON path $[?(@.amount > 99)] payment-required
2 The header X-Tenant equals acme and the query parameter dryRun is yes accepted
3 The path parameter id matches 0+ not-found

A request no rule holds for gets the response in Otherwise answer with. A request that names a response with X-MockHub-Response gets that response before any rule is tried, unless A request may name a response, and gets it before any rule is tried is off. Rules never decide which mock answers: MockHub finds the mock as it always does, and the rules choose among its responses.

Each request log names the rule that answered, as Rule 2, the first whose conditions held, or says the default answered; see Read request logs. To check a rule before you rely on it, open Try it, choose Which response a request gets and select Send the request: it reads Rule 1 answered with the response, or No rule held; the default answered.

In a configuration file, the same mock’s responseSelection reads:

responseSelection:
mode: rules
defaultResponseKey: created
rules:
- when: { bodyPatterns: { matchesJsonPath: '$[?(@.amount > 99)]' } }
responseKey: payment-required
- when: { headers: { X-Tenant: acme }, queryParameters: { dryRun: 'yes' } }
responseKey: accepted
- when: { pathParameters: { id: { matches: '0+' } } }
responseKey: not-found

A request that is byte-for-byte identical gets the same response whoever sends it, because the default decides. These things change that:

You want Use
Every caller to see another response Change the default, which changes it for everyone
Different callers or payloads to see different responses Rules on the request’s query, headers, cookies, form, path or body
One request to see another response Send the header; it changes only that request, so the caller has to be able to add a header
Each call to get the next response in a list A sequence: the first call, the second, and so on
Answers that depend on what happened before A scenario

The mock’s row on the Mocks tab shows its number of responses and the default’s status. The editor’s How this mock responds names the default, whether Request override is on, and the other responses a request may ask for. Each request log names the response that answered; see Read request logs.

  • A mock holds up to 50 responses.
  • A mock holds up to 20 rules, and a rule up to 10 conditions; each cookie and form field counts one, and so does each body condition. A rule needs at least one condition, and a path parameter condition needs a path with that parameter, such as /orders/{id}.
  • A rule’s conditions are request matches only. A rule cannot test the data store, a scenario’s state or a multipart part, and it cannot start a sequence.
  • A mock that answers with a sequence has no default response and takes no override: a request that sends the header is refused.
  • Each response has its own name, status, headers, body and delay. Response templating, a scenario attachment and a data store use belong to the mock, shared by every response.
  • A browser can send X-MockHub-Response when the project’s browser access allows the page’s origin; see A browser cannot call the mock.
  • Try it previews one response and tests which response a request would get, for this mock alone; see Test a mock before saving.
  • With Record everything on, no mock runs, so a request that sends the header is refused before anything is forwarded.

Report a problem with this page