Response templating

Response templating is a feature that allows you to specify the response that the mock service will return when it matches a request.

  response: # optional
    randomize: <bool> # optional
    header: <mapping> # optional
    trailer: <mapping> # optional, gRPC and HTTP/2 only
    body|stream: <mapping | sequence> # optional
    status: <OK|CANCELLED|UNKNOWN|...> # optional, both gRPC and HTTP
    statusDetails: <scalar> # optional, gRPC only, default empty

Randomize response

Set response.randomize flag to true if you need to generate a random response. Check out our Data generation article.

Header and trailer templating

Headers are treated as a regular mapping property with key-value pairs, so the templating is the same as for the body payload. It supports all templating operators and scripting features.

Example:

- method: greet.Greeter/SayHello
  response:
    header:
      Expires: "{{ date.now | date.add_days 2 | date.to_string '%a, %d %b %Y %T %Z' }}"
    trailer:
      user-defined-status: OK

Body and stream templating

  • The body property is used to specify the body for unary gRPC and HTTP calls.
  • The stream property is used to specify the stream for gRPC server streaming calls only.

Note that the response is converted to a target format eventually, which depends on a mock service protocol:

  • protobuf for gRPC service
  • JSON for HTTP service

Examples:

- method: greet.Greeter/SayHello
  response:
    body:
      message: "Hello Joe!"
- method: greet.Greeter/SayHello
  response:
    body:
      message: "Hello {{request.body.name}}!"

Operators

repeat

Useful for producing dynamic arrays or streaming responses:

Definition:

<property>:
  repeat:
    count: <scalar> # number of items of output array
    item: <mapping> # item to generate

Exposed runtime variables:

  • {{ repeat.index }} - index of the current item (0, 1, 2, etc)
  • {{ repeat.item }} - reference to the current item
  • {{ repeat.count }} - declared count of items
  • {{ repeat.items }} - list of items

Examples:

myArray:
  repeat:
    count: "{{ faker.random.number 2 10 }}"
    item:
      id: "{{ repeat.index }}"
      firstName: "{{ faker.name.first_name female }}"
      lastName: "{{ faker.name.last_name female }}"
      fullName: "{{ repeat.item.firstName }} {{ repeat.item.lastName }}"
      login: "{{ faker.internet.user_name repeat.item.firstName repeat.item.lastName }}"
      email: "{{ faker.internet.email repeat.item.firstName repeat.item.lastName }}"
      avatar: "{{ faker.internet.avatar }}"
      balance: "{{ faker.finance.amount }}"

will produce

{
  "myArray": [{
      "login": "Aniyah_Lueilwitz",
      "firstName": "Aniyah",
      "lastName": "Lueilwitz",
      "fullName": "Aniyah Lueilwitz",
      "email": "Aniyah.Lueilwitz@gmail.com",
      "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/521.jpg",
      "balance": 213.2
    }, {
      "id": 1,
      "login": "Jaiden54",
      "firstName": "Jaiden",
      "lastName": "Windler",
      "fullName": "Jaiden Windler",
      "email": "Jaiden55@yahoo.com",
      "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/477.jpg",
      "balance": 986.69
    }, {
      "id": 2,
      "login": "Bertrand52",
      "firstName": "Bertrand",
      "lastName": "Marvin",
      "fullName": "Bertrand Marvin",
      "email": "Bertrand.Marvin@gmail.com",
      "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/173.jpg",
      "balance": 595.65
    }
  ]
}

fromJsonFile

Loads a property value from a JSON file.

Definition:

<property>:
  fromJsonFile: <fileName>

Example:

# loading a whole response body from a json file
  response:
    body:
      fromJsonFile: "/body.json"
# loading a data property from a json file
  response:
    body:
      id: 1
      data:
        fromJsonFile: "/data.json"
# load a data property from a dynamically resolved json file
- url: /post/{id}  
  method: GET
  response:
    body:
      id: "{{ request.url_path.id }}"
      data:
        fromJsonFile: "/data-{{ request.url_path.id }}.json"

fromYamlFile

Loads a property value from a YAML file.

<property>:
  fromYamlFile: <fileName>

Status code and details

The status (or statusCode) property accepts gRPC codes in both text and numeric format as defined in the page below:

Definition:

  response:
    status: <OK|CANCELLED|UNKNOWN|...> # optional, both gRPC and HTTP, default OK
    statusDetails: <scalar> # optional, gRPC only, default empty

Examples:

  response:
    status: OK
  response:
    status: NOT_FOUND
    statusDetails: "Page not found"
  response:
    status: 1 # CANCELLED
    statusDetails: "Request has been cancelled"