Check requests against the contract
Before you begin
- Plan
- Included in Pro and Team. How plans and limits work
- Role
- Admin or higher.
- You need
- A project with an OpenAPI contract
Turn on Report to have MockHub check every request to the project against one revision of its contract. The findings are recorded beside each request log and the request is served exactly as it would have been unchecked, so you learn which callers break the contract before anything refuses them.
-
Open the project’s OpenAPI tab.
-
In Request checks, choose Report under Mode.
-
Under Revision, choose the revision to check against, such as Revision 2 (active). The check stays on that revision when a newer one becomes active.
-
To check only some operations, add them under Operations, each written as the method and path, such as
GET /orders/{id}, pressing Enter after each. Leave it empty to check every operation. -
Select Save request checks.
Only a workspace Admin can change it; anyone who can read the project sees it. A plan without request checks refuses the change.
An operation the revision does not declare is refused under Operations, such as “"/orders" is not an operation of revision 2. Name operations as "METHOD /path".”; correct it and save again.
Result
Section titled “Result”Request checks shows how many requests were checked at that revision and how many were valid, invalid or incomplete. Select the number invalid or incomplete to open the request logs filtered to them.
On Request logs, a request found invalid carries a Request invalid tag, and one the check could not conclude Request not concluded; a valid request carries none. Under Filters, choose Request invalid or Request not concluded to list only those requests. In a log’s Check against the contract dialog, Request shows the verdict recorded when the request arrived, such as When it arrived, against revision 4: invalid, with what it broke, above Checked again from the stored request: a new check of what logging kept, which can differ.
A request is checked as the client sent it. A check never changes the answer: one that cannot finish is recorded as Incomplete and the request is served. A request no operation answers is Not checked, unless you named operations, in which case a request outside them is not checked at all. A post to the webhook inbox and a CORS preflight are never checked.
Check a logged request again
Section titled “Check a logged request again”In Check against the contract on a request log, choose Request to check the request the log kept, instead of the response. It is only as complete as logging was: a part that was not kept, such as a redacted body or the Cookie header, is Incomplete, because the log cannot show what was sent. A redacted or truncated media-type or content-encoding header also leaves the body incomplete; it does not prove the request was invalid. See Check a request log against the contract.
Check the requests your application sent in CI
Section titled “Check the requests your application sent in CI”In a pipeline, check every request your application sent while its tests ran, from the request logs, without turning on Report. Bring up an ephemeral environment, point the application at it, note the time before and after the tests, then check the window:
since=$(date -u +%Y-%m-%dT%H:%M:%SZ)npm test # your application's tests, against the environment's gateway addresssleep 1 # the window leaves out its end, and the time is cut to the seconduntil=$(date -u +%Y-%m-%dT%H:%M:%SZ)npx --yes @mockhub/cli openapi validate --project "$PROJECT_ID" --direction request --since "$since" --until "$until"A request’s log is stored a moment after it is answered, so when the window ended less than a minute ago the command reads it again every 2 seconds until no new log arrives, for at most 30 seconds, and warns if logs are still arriving.
$PROJECT_ID is the environment’s project id: environment.id in the file env up --report-json FILE writes. The command exits 1 when a request is invalid, and 2 when the window holds more than 500 logs, which it checks the first 500 of and never reports as a success: split the window. --fail-on incomplete also fails on a request logging did not keep whole. --log ID checks one log’s request, --logs FILE a list of log ids, and --compare-revision N checks against a second revision, which decides the exit code.
The project must store request logs and log request data, or there is nothing to read back. Requests are checked as the log kept them, so a redacted header or body is Incomplete.
See which mocks answered invalid requests
Section titled “See which mocks answered invalid requests”Permissive mocks, on the same tab, lists the requests the contract rejects that a mock answered with a success, such as List orders returned 200 for GET /orders to a request that breaks revision 2. It reads checks already recorded, so it needs a mode other than Off and stored request logs, and it checks nothing itself.
This says what was returned, not that the mock is wrong: a lenient endpoint can be intended. A request answered with an error, or forwarded to your upstream, is not listed.
Where a mock’s matcher asks nothing of an input the contract requires, such as a required query parameter, a note says so:
- On the import review, under Worth a look before applying, a line such as 4 mocks answer requests that leave out what the contract requires, and Accepts incomplete requests on each of those mocks.
- In an operation’s Responses, a line on the mock naming the inputs, such as query page.
A required cookie is read from a cookie rule of the same name, as a header is. A matcher kind that cannot be read, such as contains, a rule on the whole Cookie header, or any rule on the body, a form field or a multipart part, JSON ones included, leaves an input as one MockHub cannot tell. A path parameter is never listed, because a matched path supplies it.
Limits
Section titled “Limits”- Report mode needs stored request logs to keep what it finds. In a project that stores none, the gateway checks nothing, and Request checks says so.
- A form body (
application/x-www-form-urlencoded) is read as the object its schema describes, with+read as a space and%2Bas a plus. Plain values, arrays sent as repeated names or with a delimiter, and one level ofdeepObjectkeys such asmetadata[order]=1are checked. A nested key such asaddress[city][name]orexpand[], an object property not declared as adeepObject, a property with its own media type, and a single value sent twice are Incomplete. So is a key the schema does not list underpropertieswhen it usesallOf,anyOf,oneOforpatternPropertiesto describe its keys, in a form or a querydeepObject. - An OpenAPI 3.2
querystringparameter is read only when it is form-encoded. Of another media type, a request to its operation is Incomplete, unless another part of it breaks the contract. - A document larger than 2 MiB is not checked in the gateway: its requests are Incomplete.
- The check never reads whether the request was authenticated. A valid request is not an authorized one.
- Deleting the pinned revision ends the checks.