Check recorded responses in CI
Before you begin
- Plan
- Included in Pro and Team. How plans and limits work
- Role
- Viewer or higher.
- You need
- A project with an OpenAPI contract, unless you check against `--candidate` alone
- The MockHub CLI and a token in `MOCKHUB_TOKEN`, as in [Keep a project's configuration in a repository](/guides/cli/install-and-connect/)
Check the responses a test run recorded against the contract with mockhub openapi validate, so a pipeline fails when a response stops matching it. Nothing is sent to the gateway, and no traffic is spent.
-
Create a machine token with Viewer or higher on the project, and store it as a secret of the repository’s CI, named
MOCKHUB_TOKEN. -
Record the responses while your tests run, as a HAR file. Playwright writes one with
recordHar; a browser’s network panel exports one too. -
After the tests, check the file against the active revision of the contract:
Terminal window npx --yes @mockhub/cli openapi validate --env staging --har run.har --host api.example.com--hostkeeps only the entries sent to that host; without it, a HAR with several hosts is checked whole and a warning names them.
Result
Section titled “Result”Each response prints its result, its request and the operation it was checked against, then each finding with where it is in the body and in the document:
invalid GET /orders/o-1 200 → GET /orders/{id} body-schema at /total: The data (string) must match the type: integer (#/paths/~1orders~1{id}/get/responses/200/content/application~1json/schema/properties/total)The command exits 0 when nothing failed, 1 when a response did, and 2 on an error, a refusal or a run that stopped short. --fail-on incomplete also fails on a response that could not be checked in full, and --fail-on not-applicable on one no operation answers. --report-json PATH writes the whole report to a file for the pipeline to keep, and --json prints it instead.
Other inputs
Section titled “Other inputs”| Input | Checks |
|---|---|
--har FILE |
The responses of a HAR file, each against the operation its request finds |
--responses FILE |
A JSON array of responses you collected, each with its request or the operation to check it against: [{"request": {"method": "GET", "url": "/orders/o-1"}, "response": {"status": 200, "headers": {"Content-Type": "application/json"}, "body": "{\"total\": 12}"}}] |
--since TIME --until TIME |
The request logs stored in that window, at most 500, such as an ephemeral environment’s after a test run, or what your upstream answered in a capture |
--log ID, --logs FILE |
One request log, or a list of log ids |
--revision N checks against a retained revision instead of the active one.
Before you import a new document
Section titled “Before you import a new document”Check the same responses against a document you have not imported yet, to see what an update would break before you make it:
npx --yes @mockhub/cli openapi validate --env staging --har run.har --candidate openapi.next.yamlThe candidate is checked alone, so this works in a project without a contract too. Add --revision N to check each response against that revision as well, and see how its verdict moves. To compare two retained revisions instead, use --compare-revision N, which checks against the active revision, or the one --revision names, and against revision N. Use one of --candidate and --compare-revision. The second document decides the exit code, and each response prints its transition, such as nowInvalid for one valid at the first and invalid at the second:
nowInvalid log 0190… → GET /orders/{id}: valid at revision 4, invalid at the candidate body-schema at /total: The data (integer) must match the type: string (#/paths/~1orders~1{id}/get/responses/200/content/application~1json/schema/properties/total)A failure that both share still fails the run.
Limits
Section titled “Limits”- Checking responses needs a plan with OpenAPI contract checks.
- A HAR entry without a response, answered
304 Not Modifiedfrom the browser’s cache, a CORS preflight, a body neither JSON nor text, and a body the HAR left out (Playwright’scontent: 'omit'or'attach') are counted and not checked. - A body is checked up to 1 MiB, and up to the plan’s response body size; a longer one is Incomplete, with its status, media type and headers still checked.
- A request log is checked as logging kept it, so a redacted or cut-short part is Incomplete. A window with more than 500 logs is checked for its first 500 and never reported as a success: split it.
- A run counts towards the workspace’s 60 checks a minute, one for each call of up to 20 responses.
- To check the requests your application sent rather than the responses, see Check the requests your application sent in CI.