Skip to content

YAML request response file format

Omar Abdelhafith edited this page Jul 3, 2015 · 4 revisions

Each YAML file stored in the inout directory contains configuration for the request and the response.

Request configuration

The request can have any of these matchers.

URL and Method matching

request:
    method: GET
    url: .*sample/matching.*
response:
    status: 200

The above catches all the GET request that has sample/matching in its URL

Header matching

Matching multiple headers

request:
    headers:
        Authorization: Bearer.*123
response:
    status: 200

Matching multiple headers, partial matching

request:
    headers:
        Auth.*: B.*123
response:
    status: 200

The same can be achieved using

request:
    headers: "Auth.*123"
response:
    status: 200

Matching multiple headers with partial values

request:
    headers:
        - "H.*1.*V.*1"
        - "H.*2.*V.*2"
response:
    status: 200

The above will parse a request that has the following headers

    Header1: Value1
    Header2: Value2

Body matching

Matching a body that contains "SomeString" can be done as following

request:
    body: .*SomeString.*
response:
    status: 200

Response configuration

The YAML file can specify what status, header and body to return.

Status

status can be defined using status field, to return 200 use the following

request:
    url: .*sample/matching.*
response:
    status: 200

Headers

Headers are returned by adding the headers field in the response object

request:
    url: .*sample/matching.*
response:
    headers:
        Content-type: application/json

The above returns the Content-type header

Body

The body can be a string, a file, or an image. To return a string use the following:

request:
    url: .*sample/matching.*
response:
    body: some string to return

Returning json can be done as

request:
    url: .*sample/matching.*
response:
    status: 200
    body: {"key": "or even json!"}
    headers:
        Content-type: application/json

Notice that we also set the content type to be json

Body from an image

Images files are served from the specified res folder. To return an image file, we first need to add it in res folder, then we can use the following YAML.

request:
    url: .*sample/matching.*
response:
    status: 200
    body_image: cat.png

Don't forget to include cat.png in the res folder.

Body from a file

Files return in the response must exist in the res folder, in order to return a JSON file we might use the following

request:
    url: .*sample/matching.*
response:
    status: 200
    body_file: test1.json
    headers:
        Content-type: application/json

res/test1.json must exist in order for this to work. Please note that we need to specify the Content-type to be json too.