Request matching
When a request reaches a project in Mock only or Proxy unmatched mode, MockHub compares it with the project’s Active mocks. A mock matches when every rule on it holds. Inactive mocks are never compared.
What a mock can look at
Section titled “What a mock can look at”| Part of the request | How it is compared |
|---|---|
| Method | Equal to the mock’s method. |
| Path | Path and query, Path only, Path with parameters such as /orders/{id}, or Regex against the path. |
| Query parameters | Each rule names a parameter and compares its value: equals, matches regex, contains, does not match regex, does not contain, or is absent. |
| Headers | The same comparisons as query parameters. |
| Cookies | Each rule names a cookie from the Cookie header and compares its value, with the same comparisons as query parameters. |
| Form fields | Each rule names a field of a form-encoded body and compares its value, with the same comparisons as query parameters. |
| Body | equals, matches regex, contains, does not match regex or does not contain compare the whole body as text. has JSON path and equals JSON read it as JSON. |
| Multipart parts | Each part names a field, its headers or its body, and some part of a multipart request must match it. |
| Scenario state | If the mock belongs to a scenario transition, the scenario must be in the state the mock requires. See Scenarios and response sequences. |
Regular expressions must match the whole value: [0-9]+ matches 42 but not order-42.
Path with parameters names the varying parts of a path, /users/{userId}/orders/{orderId}, and lets a response read them as request.path.orderId and a rule compare them. Use Regex only for a path that a template cannot say, such as digits only or a path of any depth. Path parameters lists the rules.
Exact path and the query string
Section titled “Exact path and the query string”Path and query compares the path and the query string. A mock for /orders then does not match /orders?page=2. Because it compares the whole query string, it cannot be combined with query-parameter rules.
Path only compares the path alone, so /orders matches /orders?page=2, and query-parameter rules decide about the query string.
Scenarios and response sequences
Section titled “Scenarios and response sequences”A scenario is a named state machine in a project. A mock can require the scenario to be in a given state, and move it to another state when it answers. Two mocks on the same path can then answer differently: the first POST /payments returns 202, and a later GET /payments/42 sees the state it left behind and returns settled.
A response sequence answers the same mock with a different response each time: the first request gets the first step’s response, the second the second’s, and so on. After the last step, the sequence either starts over or keeps answering with the last response.
MockHub keeps each scenario’s state and each sequence’s place apart from the project’s configuration, until they go seven days unused; the data store is kept until its records are deleted. Resetting a scenario from its page puts it back to its start. A mock whose answer depends on a state MockHub cannot read or save answers 503 with Scenario unavailable, and the request is not forwarded to the upstream, even in Proxy unmatched mode.
Which response of the mock answers
Section titled “Which response of the mock answers”Matching finds a mock, and the mock then decides which of its responses answers: its default response, or the next step of its sequence. A request can ask for another response of the mock it matched by sending X-MockHub-Response: <mock reference>:<response key>, the mock reference being the last 12 characters of the mock’s identifier.
That header is not a rule and takes no part in matching: it never makes one mock match instead of another, and a mock cannot match on it. If it names a response of a mock the request did not match, an inactive mock, a mock with overrides turned off, or a response that does not exist, the request is refused with Response selection unavailable, not answered by another mock, the default or an upstream. See Choose the response that answers.
When more than one mock matches
Section titled “When more than one mock matches”MockHub orders mocks by their paths and conditions, and there is no order to set yourself:
- A mock with an exact path, Path and query or Path only, is tried before one with a Path with parameters or Regex path.
GET /users/meanswers its own requests even besideGET /users/{id}. - Within each of those, a mock with a data store condition is tried before one without, so a guarded mock answers before its fallback.
Mocks alike in both have no order between them: when two of them match one request, either can answer. Make their rules specific enough that only one of them matches. Several responses in one mock are not competing mocks: they share the mock’s rules and never count as a duplicate.
The Mocks tab tags a mock Duplicate when another mock in the project has exactly the same rules, with a warning while both are active. Mocks that a scenario state or a data store condition tells apart are deliberate and not tagged. Rules written differently are not compared, so a pattern such as /orders/[0-9]+ beside the path /orders/1 is not tagged, although both match GET /orders/1.
When no mock matches
Section titled “When no mock matches”A request no mock matches is answered with a 404 No mock matched, naming its method and path, unless the project is in Proxy unmatched mode and forwards it upstream.
The live response says nothing about why nothing matched, because it goes to whoever called your address. To find out:
- In the mock editor, Try it sends a request to the mock you are editing. When it does not match, the answer names the closest mock and lists what differed between it and the request. See Test a mock before saving.
- Open the request logs. While Log requests no mock answered is on in the project settings, as it is for a new project, unmatched requests appear there, and insights list the paths nothing answered.
Limits
Section titled “Limits”- Try it tests one mock on its own. It ignores active chaos profiles.
- A mock holds up to 50 responses. An override chooses among them only, never among mocks.
- The order between mocks follows only from their paths and data store conditions, as described above. Two paths with parameters or regular expressions that can match the same path, such as
/a/{x}/cand/a/b/{y}, have no order between them. - A record’s contents never decide which mock answers: a data store condition asks only whether a record exists, and the response body reads what it holds.