Logo

Mocks for microservice environments

Getting Started

Learning Videos

Config Syntax

Request Matching

Response Templating

Async Actors (Kafka etc.)

Performance & Chaos Testing

Remote Management

Changelog

Response Templating

Response Fields

In response template, one can specify status, headers and body of HTTP message. Here’s quick example:

services:
  - port: 8080
    endpoints:
      - path: /api-call
        response:
          status: 201
          headers:
            content-type: application/json
            x-custom-id: 12345
          body: '{"result": "created"}'

Any of those fields allows using dynamic template that will be evaluated for each request. Like this:

services:
  - port: 8080
    endpoints:
      - path: /api-call
        response:
          status: "{{request.queryString.rc}}"
          headers:
            content-type: '{{request.headers.accept}}'
            x-custom-id: '{{random.int 0 1000}}'
          body: '{"result": "created", "name": "{{fake.lastname}}" }'

Note: for headers, only the value part is subject for templating. Mind the global headers feature, too.

The body can be direct response string:

response:
  body: 'hello world'

or a string that starts with @ sign to indicate a file on disk:

response:
  body: '@some/path/my_template.json.hbs'

Note: The template file path has to be relative to the directory of the config file.

Dynamic Values

By default, dynamic templates use Handlebars syntax that looks like this: {{namedValue}} or {{request.path}} or {{fake.address}} etc.

Note: To switch into Jinja2 as templating engine, use the templatingEngine option of configuration syntax.

Below is the reference of available dynamic value generators.

Random

For random names, addresses etc, please refer to Faker’s functionality.

Date

Here is a list of date shifting examples as a Handlebars response template:

{
  "now": "{{ date.date '%Y-%m-%d %H:%M %f' }}",
  "1_week_back": "{{ date.date '%Y-%m-%d %H:%M %f' -604800 }}",
  "1_week_forward": "{{ date.date '%Y-%m-%d %H:%M %f' 604800 }}",
  "1_day_back": "{{ date.date '%Y-%m-%d %H:%M %f' -86400 }}",
  "1_day_forward": "{{ date.date '%Y-%m-%d %H:%M %f' 86400 }}",
  "1_hour_back": "{{ date.date '%Y-%m-%d %H:%M %f' -3600 }}",
  "1_hour_forward": "{{ date.date '%Y-%m-%d %H:%M %f' 3600 }}",
  "1_minute_back": "{{ date.date '%Y-%m-%d %H:%M %f' -60 }}",
  "1_minute_forward": "{{ date.date '%Y-%m-%d %H:%M %f' 60 }}"
}

Faker

Faker library is provided for generating some dynamic data. It is available as fake object. Refer to the official docs for all capabilities. Below are some examples (Handlebars):

{
  "first_name": "{{ fake.first_name }}",
  "first_name_female": "{{ fake.first_name_female }}",
  "first_name_male": "{{ fake.first_name_male }}",
  "first_name_nonbinary": "{{ fake.first_name_nonbinary }}",
  "last_name": "{{ fake.last_name }}",
  "last_name_female": "{{ fake.last_name_female }}",
  "last_name_male": "{{ fake.last_name_male }}",
  "last_name_nonbinary": "{{ fake.last_name_nonbinary }}",
  "address": "{{ replace ( fake.address ) old='\n' new='\\n' }}",
  "city": "{{ fake.city }}",
  "country": "{{ fake.country }}",
  "hexify_args": "{{ fake.hexify text="MAC Address: ^^:^^:^^:^^:^^:^^" upper=true }}",
  "lexify_args": "{{ fake.lexify text="Random Identifier: ??????????" }}",
  "numerify_args": "{{ fake.numerify text="Intel Core i%-%%##K vs AMD Ryzen % %%##X" }}",
  "random_choices": "{{ fake.random_choices elements=( array 'a' 'b' 'c' 'd' 'e' ) }}",
  "random_digit": {{ fake.random_digit }},
  "random_element": "{{ fake.random_element elements=( array 'a' 'b' 'c' 'd' 'e' ) }}",
  "random_elements": {{ tojson ( fake.random_elements elements=( array 'a' 'b' 'c' 'd' 'e' ) length=3 unique=True ) }},
  "random_int_args": {{ fake.random_int min=10000 max=50000 step=500 }},
  "random_letter": "{{ fake.random_letter }}",
  "random_letters": {{ tojson ( fake.random_letters ) }},
  "random_letters_args": {{ tojson ( fake.random_letters length=32 ) }},
  "random_lowercase_letter": "{{ fake.random_lowercase_letter }}",
  "random_sample": {{ tojson ( fake.random_sample elements=( array 'a' 'b' 'c' 'd' 'e' ) ) }},
  "random_uppercase_letter": "{{ fake.random_uppercase_letter }}"
}

Rendered:

{
  "first_name": "Mark",
  "first_name_female": "Sharon",
  "first_name_male": "Brian",
  "first_name_nonbinary": "Jessica",
  "last_name": "Campbell",
  "last_name_female": "Rodriguez",
  "last_name_male": "Combs",
  "last_name_nonbinary": "Mcmillan",
  "address": "035 Angela Brook\nElizabethhaven, MO 35984",
  "city": "Hannaport",
  "country": "Burkina Faso",
  "hexify_args": "MAC Address: 84:DE:AD:1C:AD:D7",
  "lexify_args": "Random Identifier: ZDGINMgIkX",
  "numerify_args": "Intel Core i7-8517K vs AMD Ryzen 3 8887X",
  "random_choices": "['b', 'a', 'd', 'a', 'b']",
  "random_digit": 9,
  "random_element": "d",
  "random_elements": ["a", "a", "d"],
  "random_int_args": 26500,
  "random_letter": "A",
  "random_letters": ["M", "S", "N", "T", "r", "m", "p", "j", "R", "n", "g", "g", "A", "w", "o", "d"],
  "random_letters_args": ["F", "L", "X", "R", "Z", "T", "f", "k", "C", "v", "U", "d", "d", "S", "p", "j", "s", "F", "M", "X", "k", "J", "P", "R", "W", "m", "i", "A", "x", "o", "r", "H"],
  "random_lowercase_letter": "i",
  "random_sample": ["c", "a", "b"],
  "random_uppercase_letter": "U"
}

An extended list of Faker examples can be found in: Handlebars and Jinja2

Counters

There is special kind of template helper, offering named counters like {{counter 'counterName'}}. The counters are global and identified by name. You can also refer to last value of counter by its name like this: {{counterName}}

Request Object

You can reference parts of HTTP request in response templates. It is available as request object. These are the most useful attributes:

And some less frequently used:

Using JSONPath Syntax

You can reference certain fields from request’s JSON by using jsonPath helper like this: jsonPath request.json '$.key'.

Other Template Helpers

escapeHtml(text)

{{ escapeHtml '& < \" >' }} a helper to escape HTML special characters. (see html.escape)

tojson(text) (Handlebars only)

The equivalent of tojson filter in Jinja2.

Jinja2 usage: {{ fake.random_letters() | tojson }}

Handlebars usage: {{ tojson ( fake.random_letters ) }}

array(*args) (Handlebars only)

Provides array support in parameters. {{ array 'a' 'b' 'c' 'd' 'e' }} returns ['a', 'b', 'c', 'd', 'e'].

replace(text, old, new, count=None) (Handlebars only)

The equivalent of replace filter in Jinja2.

Jinja2 usage: {{ fake.address() | replace('\n','\\n') }}

Handlebars usage: {{ replace ( fake.address ) old='\n' new='\\n' }}

env(name, default)

Returns the value of environment variable given by name parameter. If the environment variable does not exists in the current shell then the value in default parameter is returned.