Responses behaviour

As developers we appreciate the versatility of the tools we use in our regular developments and how they improve our productivity. That's why Mockly seeks to improve your development speed helping you where others can't.

One thing we love in our developments it's the ability to change the services responses as we desire to check how the application behave in different scenarios.

Configuring responses

To modify services default responses you need to create, at least, one file called {name}.responses.json. This file should contain a responses key with the responses configuration you want to change. This key contains a configuration array where every item should follow the next schema:

Name

Type

Required

Description

body

object | string

false

Response body

cookies

object

false

Cookies setted by the response

delay

number

false

Determines how long takes the request to be resolved. Overrides generic delay configuration.

headers

object

false

Headers returned in the response

path

string

true

Request URI to match

method

string

true

Request HTTP method to match

status

string

false

Configuration example

Now you've learnt how you can configure the responses let see an example:

You can use a provided JSON schema to validate this file

all.responses.config.json
{
    "$shema": "node_modules/@mockly/server/tools/schemas/responses-config.json",
    "responses": [
        {
            "path": "/me",
            "method": "GET",
            "status": 401,
            "body": "Unauthorized"
        }, 
        {
            "path": "/artists",
            "method": "POST",
            "status": 500,
            "body": {
                "message": "Internal server error"
            }
        }
    ]
}

With this configuration the next GET request to /me will return a 401 status code and the POST requests to /artists will return a 500 status code.

Last updated