Match requests
Before you begin
- Role
- Editor or higher.
- You need
- A mock to change, or a project to create one in
A mock answers a request only when every rule on it holds. You set the rules under Which requests it answers in the mock editor. How MockHub compares them, and why a request can match nothing, is explained in Request matching; Request matchers lists every comparison.
Match the method and path
Section titled “Match the method and path”-
Open the mock from the project’s Mocks tab.
-
Choose the method in the HTTP method list, such as
POST. Any method matches every method. -
Choose how to compare the path, as in the table below.
-
Enter the path.
| To answer | Choose | Enter |
|---|---|---|
/orders alone, with no query string |
Path and query | /orders |
/orders?status=open, and no other query string |
Path and query | /orders?status=open |
/orders whatever its query string |
Path only | /orders |
/orders/8821, /orders/42 and any other order |
Path with parameters | /orders/{id} |
/orders/8821 and /orders/42, but not /orders/new |
Regex | /orders/[0-9]+ |
With Path with parameters, each {name} stands for one part of the path, and a response can read it as request.path.name. To compare a value, open Match on more than the path, find Path parameters, select Match a path parameter, and choose the name, how to compare and the value. Typing /users/:id offers /users/{id}, because a colon is not a parameter.
A path starts with /. Path and query compares the whole query string, so it cannot be combined with query parameter rules; to match parameters one by one, choose Path only. Exact path and the query string explains the difference.
Use named parameters
Section titled “Use named parameters”A mock with a Regex path of escaped text and [^/]+ parts, which is what imports and promoted captures wrote before Path with parameters existed, can be converted.
-
Open the mock. Under the URL choices, a message names the path it would become, such as
/orders/{id}, and counts what else changes. -
Select Use named parameters.
-
Read the form. The path is now Path with parameters; every
request.pathSegments.[n]in a response body or header that reads a converted part is nowrequest.path.name; and a data store key that read such a part now names the parameter. -
Select Save mock.
Nothing is saved until you select Save mock; leaving the page discards the conversion. The names come from the OpenAPI operation the mock is linked to, when its parameters line up with the path, and are id, id2 and so on otherwise. Rename a parameter by editing the path and the reads that use it. A regex with anything else in it, such as [0-9]+ or .*, gets no offer.
Match headers, query parameters, cookies and form fields
Section titled “Match headers, query parameters, cookies and form fields”-
Open Match on more than the path.
-
Select Match a header, Match a query parameter, Match a cookie or Match a form field.
-
Enter the name, such as
Authorization. -
Choose how to compare, such as matches regex.
-
Enter the value to compare against, such as
Bearer .+. is absent needs no value. -
Select Save mock.
To remove a rule, select its remove button, labelled for the row, such as Remove cookie matcher 1.
| To answer only | Rule |
|---|---|
| Requests that send JSON | Header Content-Type contains application/json |
| Requests with a bearer token | Header Authorization matches regex Bearer .+ |
| Requests without an API key | Header X-Api-Key is absent |
| The second page | Query parameter page equals 2 |
| Requests from a signed-in browser | Cookie session matches regex .+ |
| A client credentials token grant | Form field grant_type equals client_credentials |
A form field is read only from a body sent as application/x-www-form-urlencoded. A field that is not sent, in a body of any other type too, meets is absent, does not match regex and does not contain. A cookie that is not sent meets only is absent.
Match the body
Section titled “Match the body”-
Open Match on more than the path.
-
Under Request body, select Match the body.
-
Choose how to compare: one of the text comparisons, has JSON path or equals JSON.
-
Enter what to compare against, as in the table below. For has JSON path, also choose what the path must do, such as selects something or equals, and enter the value.
-
Add more rows the same way. The body must meet every one.
-
Select Save mock.
| To answer only | Rule |
|---|---|
| Orders sent as paid | has JSON path $.status, equals paid |
| Orders with a bulk line | has JSON path $.items[?(@.quantity > 10)], selects something |
| Orders without a coupon | has JSON path $.coupon, is absent |
| One exact order, whatever its layout | equals JSON {"sku": "B-12", "quantity": 1} |
| A body naming a test account | contains test-account |
The text comparisons read the body exactly as the caller sent it: contains "status":"paid" does not match "status": "paid" with a space. The JSON comparisons ignore spacing and key order, and a body that is not JSON matches neither. equals JSON needs every field of the body unless Allow fields and trailing array items the JSON does not name is on. JSON paths lists what a path may hold.
Match a multipart upload
Section titled “Match a multipart upload”-
Open Match on more than the path.
-
Under Multipart parts, select Match a multipart part.
-
Enter the part’s field name in Field name, such as file.
-
To compare the part’s own headers or body, select Match a header of this part or Match the body of this part.
-
Add another part the same way. The request must have a part that matches each one.
-
Select Save mock.
Result
Section titled “Result”The mock answers only the requests that meet all its rules. Match on more than the path shows how many rules are set, even while it is closed. A request that misses a rule gets another mock’s answer, or what an unmatched request gets.
To see why a request does not match, send it with Try it: the answer lists where the request differs from what the mock expects. See Test a mock before saving.
Limits
Section titled “Limits”- The method list has
GET,POST,PUT,PATCH,DELETEandOPTIONS. To answerHEADor another method, choose Any method. - Path and query and query parameter rules cannot be combined. The editor refuses the pair with ““Path and query” compares the whole query string, so it cannot also match parameters one by one. Choose “Path only” to keep the parameter matchers, or remove them and type the query into the path.”
- With Path only, a query string typed into the path and query parameter rules cannot be combined either: “Put the query string in the path or in the parameter matchers, not both — together they ask for a request that cannot exist.”
- Path with parameters takes one
{name}per part of the path and has no wildcard for a path of any depth; a part such as{name}.jsonis allowed. Use Regex for anything else. Path parameters lists the rules. - The editor checks a regular expression with your browser’s rules, and MockHub’s regular expressions differ from your browser’s in places. A pattern the editor accepts can still match nothing, so test it with Try it.
- A few headers, such as
HostandContent-Length, never reach matching, so a rule on them never sees what the caller sent. Request matchers names them. - MockHub does not order mocks. When two could answer the same request, make one of them more specific.
- A request or rule holds up to 10 body rows, and a mock up to 10 equals JSON across its request, its parts and its rules. A mock holds up to 5 parts, each with up to 5 rules, its field name included.
- has JSON path takes a limited set of expressions: no functions,
=~, slices or filters within filters, and at most two filters. A filter compares a field with a value: it cannot only test that a field exists, as[?(@.a)]does, group comparisons in parentheses, compare a field with another field, read$or usein. To test that a field exists, add a row with its path and selects something. A name with characters other than letters, digits,_and-is written['…']. JSON paths lists them. - has JSON path compares a number as Java writes it, so equals
1does not match1.0.