Mock files
Mocks can also live in YAML or JSON files (.json, .yaml or .yml).
- On start, the server loads the files matching
testdata/vlatte/_mocks/*mock.yamlandtestdata/vlatte/_mocks/*mock.yml, and the recordings intestdata/vlatte/_mocks_recorded, in name order: the folders of its work directory. Change the patterns withSetup().MockFilePatterns(...). - To load one file, use
m.MustMock(mockhttp.FromFile(path)). m.Reload()replaces the mocks of the files with what they hold now, in one step. When a file has problems, it returns them and the mocks stay as they were.
# testdata/vlatte/_mocks/hello_mock.yaml
id: hello # optional: names the mock, as in the admin API's /mocks/hello
request:
method: GET
path: /hello
response:
status: 200
header:
content-type: text/plain
body_file: hello.txt # relative to this filemockhttp/schema/http.schema.json describes the format, so editors can validate mock files. Its address is https://vlatte.dev/schemas/http.schema.json: start a YAML mock file with
# yaml-language-server: $schema=https://vlatte.dev/schemas/http.schema.jsonand editors that use the YAML language server, such as VS Code with the YAML extension, complete and check it. A JSON mock file names it in a "$schema" key. See Mock file schemas for other editors.
Keys
| Key | What it holds |
|---|---|
id, name, priority, enabled | The mock's features. |
request | The matchers. |
response | The reply. |
response_sequence, response_random | Sequences and random replies. |
proxy, proxy_all | Proxy replies and proxy all. |
delay, repeat | A delay before replying, and how many requests the mock serves: see Mock lifecycle. |
scenario | A scenario. |
on_complete | Events such as webhooks. |
pipes, wire_pipes, upstream_pipes | Faults. |
ext | Settings for the server's mock file handlers, Setup().MockFileHandlers(...) in Go. |
Types
Each key takes one type, as the schema says. Write text in quotes where a string goes, as in name: "123", and numbers bare, as in priority: 10. Bools read the way Go's strconv.ParseBool reads them, so true, "true", t and 1 are all true. A value of the wrong type is an error that says how to write it.
Bodies
A reply's body is text as is, and any other value as JSON. A body that isn't UTF-8 text, such as an image or text in another encoding, goes in body_base64, as standard base64 (body_base64: //4A), whose line breaks are ignored, or in a body_file. A reply takes one of body, body_base64, body_file, multipart and event_stream at most.
Durations
delay, proxy.timeout, the durations of faults and the delay and timeout of a webhook take whole milliseconds (1500) or a Go duration (1.5s). "1500" in quotes is neither.
Matching
The request key matches requests as the Go builder does: see Request matching for every key, and Matchers for the matcher lists, such as [contains, x] or [jsonpath, $.id, [gt, 1]], and Custom matchers for matchers of your own, in request.custom.
Proxy replies
A proxy reply (From in Go) answers with what its target answers: see Proxying.
Templates
A mock file is plain JSON or YAML. Only the settings that are templates, such as a templated body, render: see Templates.
Errors
A file with problems isn't loaded. The error lists every problem with its key and a fix:
mock: 2 problems
file testdata/vlatte/_mocks/users_mock.yaml: unknown_key: response.stauts is not a known setting; did you mean response.status?
file testdata/vlatte/_mocks/users_mock.yaml: conflict: proxy is set with response; keep one of them