Mock expectations
If you want an endpoint to return specific data, you can use mock expectations.
In DESIGN
mode, you can set expectations in the "Advanced mock" tab of the endpoint.
In DEBUG
mode, you can set expectations in the "Mock" tab.
Returning fixed data
You can add an unconditional expectation to return fixed data.
Returning conditional data
Apidog supports returning different mock data based on different request parameters.
When adding multiple mock expectations with different conditions, the mock engine will match the expectation conditions based on the request parameters. It will return the first matching mock expectation, following the order from top to bottom.
If none of the mock expectations match, the mock data will be returned according to the Mock method priority set in Project Settings - Feature Settings - Mock Settings.
You can choose which request parameters to use as conditions in the Param conditions. It supports query, path, header, cookie, and body parameters as conditions. Fill in the parameter's name and condition, and the Response below will be returned when this condition is met.
- When setting multiple conditions, they are treated as an intersection of these conditions.
- When selecting body parameters, you need to fill in the JSON path of the target property in the name field.
- Body parameters only support JSON, not XML.
- Parameter conditions do not support the use of
{{variables}}
. - The actual request body must match the API spec if the body is chosen as the parameter location for expectation criteria. For example, if the body request type is form-data in API spec, the parameter needs to be placed in form-data when mocking.
- You can add IP conditions to make certain responses effective only for specified IPs.
Returning dynamic mock data
Mock expectations support returning dynamic data. This feature supports using Faker.js and Nunjucks syntax.
For example, the following code:
{
"data": [
{% for i in range(0, 20) %}
{% if i>1 %},{% endif %}
{
"id": {{i}},
"firstname": "{{$person.firstName}}",
"lastname": "{{$person.lastName}}"
}
{% endfor %}
],
"success": true
}
This code will generate a response with:
- An array of 20 data objects
- Each object has an 'id' (0 to 19), a randomly generated 'firstname', and a randomly generated 'lastname'
- The 'success' field is set to true
The {{$ ... }}
syntax is used by Faker.js to generate random realistic data. However, unlike the native syntax, Apidog transforms it into the {{$ ... }}
reference format. The comparison with the native syntax is as follows:
- Faker.js:
faker.person.firstName()
- Apidog:
{{$person.firstName}}
The {% for ... %}
syntax is from Nunjucks, allowing you to create loops and generate multiple items.
This approach allows you to create dynamic, realistic mock data that can change with each request, simulating real API behavior more closely.
Note: The {{i}}
in the example is not an Apidog variable, but a Nunjucks variable. Apidog variables cannot be used in mock expectations.
More features
You can add custom headers to your mock expectations. This allows you to simulate specific HTTP headers in the mock response.
In the "More" tab of mock expectations, you can configure additional response properties:
- HTTP status code: You can set a custom HTTP status code for the response (default is 200). This allows you to simulate various API response scenarios, including error states.
- Response delay: You can set a delay for the response. This feature is useful for testing how your application handles slower API responses.
In the list of expectations, you can use toggles to control whether each expectation is active in local mock and/or Cloud mock environments.