You're reading Mocko v1 (legacy) documentation. Mocko v2 is the current version. View the v2 docs.

Getting Started with Standalone Mode

Installation

You need Node.js 14 or newer installed for this part. See Updating Node if needed.

Install the Mocko CLI with npm:

npm i -g @mocko/cli

On Linux or Mac you might need sudo:

sudo npm i -g @mocko/cli

Check the installation with the help flag:

$ mocko --help
Usage: mocko [options] <path to mocks folder>
Example: mocko -p 4000 mocks

Options:
  -h, --help       Shows this screen
  -v, --version    Shows the current version
  -w, --watch      Watches for file changes and restarts the server
  -p, --port       Port to serve the mocks (8080)
  -u, --url        URL to proxy requests when no mock is defined
  -t, --timeout    Max time to wait for a response from the proxied URL in millis (30000)

Creating your first project

Create a folder with a .hcl file inside:

hello-mocko
└── first-mocks.hcl

In your .hcl file, create your first mock with the mock stanza:

mock "GET /hello" {
body = "Hello from Mocko!"
}
Your IDE or editor will most likely have an extension for .hcl syntax highlighting.

Using Mocko

Inside your project folder, start Mocko with the watch flag:

mocko --watch ./

Your mocks are now served on port 8080. Use --port to change the port. The --watch flag auto-reloads changes to your mock files. Test it with curl:

$ curl http://localhost:8080/hello
Hello from Mocko!

The mock stanza

You can define multiple mocks in a single file with status codes, headers, and multi-line bodies:

# Our first, simple, hello mock
mock "GET /hello" {
body = "Hello from Mocko!"
}

# Mocking George, the cat
mock "GET /cats/george" {
status = 200
headers {
Content-Type = "application/json"
}
body = <<EOF
{
"id": 1,
"name": "George"
}
EOF
}

Method and path

Define the method and path right after the mock stanza. You can use * to match all methods. Specific paths take higher priority over generic ones: /cats/george always matches before /cats/{name}.

Parameters

  • status: any value from 200–599; defaults to 201 for POST, 200 otherwise
  • headers: response headers map
  • delay: delay in milliseconds before responding
  • body: the response body, supports Handlebars templating

Structuring your mocks

Mocks can be in nested folders at any depth. The folder structure is purely organizational and does not affect routing.

.
├── user
│   ├── homepage.hcl
│   └── profile.hcl
└── wallet
    ├── credit
    │   ├── credit.hcl
    │   └── indication.hcl
    └── refund.hcl

Next steps

Now that you know how to define mocks, learn how to make them dynamic with Templating.