Templating
Templating is where Mocko really shines: it lets you create dynamic mocks and simulate complicated scenarios with ease. You can use templating in any installation of Mocko: standalone mode, complete stack, or hybrid.
Templates go in the body parameter of the mock stanza in standalone mode, or in the Body field in the UI.
Handlebars
Mocko uses Handlebars as its templating language. In a GET /cats/{name} mock, this template:
{
"id": 1,
"name": "{{ request.params.name }}"
}Produces this response on GET /cats/george:
{
"id": 1,
"name": "george"
}You can also use helpers from handlebars-helpers:
{
"id": 1,
"name": "{{capitalizeAll request.params.name }}"
}Context
Fields available in every template:
request.params: URL params defined in the pathrequest.headers: request headersrequest.query: query string parametersrequest.body: request body fields (JSON)
Blocks
Block helpers let you write conditionals and loops. Open with # and close with /. Blocks support else and can be nested using parentheses:
{{#startsWith 'g' (downcase request.params.name) }}
{
"id": 1,
"name": "{{capitalizeAll request.params.name }}"
}
{{else}}
{{setStatus 404}}
{
"error": "Not found error",
"message": "Cat not found"
}
{{/startsWith}}The setStatus helper changes the response status dynamically. Comments use {{! ... }}.
Logging
Use the loghelper to log to Mocko's console:
{{log 'Received a request for cat id ' request.params.id }}
{{log (JSONstringify request)}}Next steps
See Helpers for Mocko-specific helpers, or Persistence for stateful flags.