mockRequest
The mockRequest action answers matching browser requests with a fixed response, a sequence of responses, or the responses recorded in a HAR file. The request never reaches the server. Use it to show stable data in screenshots, such as a fixed date or balance. You can also test how a page handles an error response.
A mock starts at its step and lasts until the test ends. Doc Detective then removes every mock the test set.
mockRequestworks in Chrome. Other browsers skip the step. In Firefox, for example, the step reportsSKIPPED:Skipped: `firefox` can't mock requests.
Set mockRequest to an object with these fields:
url: (Required unless you setharPath) A glob for the request URLs to answer.*matches any characters.- A pattern that starts with
httpmatches the whole URL, such ashttps://api.example.com/user. - Any other pattern matches the URL’s path, such as
**/api/invoices*. - With
harPath, it keeps only the recorded requests whose URL matches.
- A pattern that starts with
method: (Optional) The HTTP method to answer, such asGETorPOST. Omit it to answer every method. WithharPath, it keeps only the recorded requests with this method.response: (Optional) The response to send. Omit it to send an empty200response.statusCode: The HTTP status code. Defaults to200.headers: Response headers, as header names and string values.body: The response body. A string goes as is. An object or array goes as JSON.bodyPath: A file to send as the body, relative to the spec file. Binary files work too.
responses: (Optional) A list of responses to send in order, each with the same fields asresponse. The first matching request gets the first entry, and the next request gets the next one. The last entry answers every later request.harPath: (Optional) A HAR file to replay, relative to the spec file. See Replay recorded responses.
Set one of response, responses, or harPath. In each response, set body or bodyPath, not both. Breaking either rule is a validation error.
A later mockRequest for the same url and method replaces the earlier one. A mock with a different url or method runs alongside it.
Outputs. The step sets outputs.matched. It’s the number of requests already answered for that url and method, counting the mocks it replaced. With harPath, it’s the total for every request in the file. To reuse it, set the step’s variables object to $$matched.
Failures. These fail the step:
- A
bodyPaththat Doc Detective can’t read. For example:Couldn't read `bodyPath` `/abs/fixtures/user.json`. - A
harPaththat Doc Detective can’t read:Couldn't read `harPath` `/abs/fixtures/app.har`. - A
harPathfile that isn’t HAR JSON:Couldn't parse `harPath` `/abs/fixtures/app.har`: it isn't HAR JSON. - With
harPath, aurlthat isn’t a valid pattern, such as one with an unclosed(:Couldn't parse `url` pattern `**/api/(`. - A HAR file with a recorded URL that isn’t absolute:
Couldn't parse a URL in `harPath` `/abs/fixtures/app.har`. - A HAR file with no request that matches
url:Couldn't find HAR entries matching `**/api/**` in `/abs/fixtures/app.har`.Withmethodset, the message names it too:Couldn't find HAR entries matching `**/api/**` with method `POST` in `/abs/fixtures/app.har`.
For every field, see the
mockRequestreference.
Examples
The docs tests run the first two examples and the sequence example in headless Chrome against a local test page.
Answer with a JSON body
This example answers every request whose path starts with /api/invoices. The page then shows the same invoice date on every run.
Answer one method with an error
This example answers POST requests with a 503 error and lets every other method through. Use it to capture a page’s error state.
Send a file as the body
This example sends fixtures/user.json as the body of one exact URL. The path resolves against the spec file.
Answer in sequence
This example answers a job’s status as running on the first request and done on every later one. Use it to take screenshots of a page that polls, both before and after the work finishes.
Replay recorded responses
A HAR file records a browser session’s requests and responses. Browser developer tools can save one from the Network panel. Set harPath to replay it:
- Doc Detective answers each recorded URL and method exactly, query string included.
- It answers a request recorded more than once in recorded order. The last recording answers every later request.
- Requests that aren’t in the file reach the network.
This example replays only the API requests from fixtures/app.har:
Replace a mock and read its count
The second step replaces the first, because both have the same url and no method. It saves the first mock’s count in INVOICE_CALLS.