Dynamic Mock API Responses with MockFly: A Deep Dive with Examples
One of the most valuable features of Mockfly, our API mocking tool, is the ability to create dynamic endpoint responses based on custom rules. In this article, we'll explore this feature in-depth and provide examples to showcase its power and flexibility, focusing on UI for defining rules and examining both request body and query parameters.
Creating Multiple Responses per Endpoint
With MockFly, you can define multiple responses for a single endpoint, allowing you to test different scenarios and outcomes. Using the UI, you can create two responses for a POST request to the "/users" endpoint, each with different data.
Setting Custom Rules
The UI allows you to create custom rules that decide which response to deliver based on conditions involving request body or query parameters. When creating a rule, the UI offers four configurable components:
- Source: Choose between "body" or "query params" using a selector.
- Property: Enter the property name using a text field.
- Comparator: Select one of the 16 available comparators using a selector — from "equal" and "distinct" to text matchers like "contains" or "regex", numeric ones like "greaterThan", and presence checks like "exists" or "isEmpty".
- Value: Input the value to compare using a text field (the presence comparators don't need one).
For example, you can return the second response if the request body contains a JSON object with a "name" property set to "John" by setting the source to "body", property to "name", comparator to "equal", and value to "John".
Alternatively, you can create a rule based on query parameters. Suppose you want to return the second response only if the query parameter "name" is "John". Set the source to "query params", property to "name", comparator to "equal", and value to "John". This rule will deliver the second response if the condition is met.
Beyond Equality: contains, regex and More
Exact matches only go so far. Suppose you want to serve a mobile-specific payload to every request coming from a mobile client: set the source to "header", the property to "User-Agent", the comparator to "contains", and the value to "Mobile". Any request whose User-Agent header includes that word — whatever comes before or after it — gets the mobile response.
For patterns, use the "regex" comparator. For instance, to return a validation error whenever the "email" property of the request body is a personal address, set the source to "body", the property to "email", the comparator to "regex", and the value to @(gmail|hotmail)\.com$. The full list of comparators — text, numeric and presence checks — is in the conditional responses documentation.
Combining Conditions with AND/OR Groups
Conditions can also be combined. The "Add condition" button stacks several conditions on the same rule — the Match toggle decides whether all of them (AND) or any of them (OR) must hold — and "Add group" nests groups with their own operator, up to 3 levels. A group is an object with an "operator" and a list of "conditions". For example, this rule — shown as the JSON you would send to the Public API — serves the response when the request declares Spain in a header or carries a Spanish SKU in the body:
{
"operator": "or",
"conditions": [
{ "source": "header", "property": "x-country", "comparator": "equal", "value": "ES" },
{ "source": "jsonPath", "property": "$.items[*].sku", "comparator": "startsWith", "value": "ES-" }
]
}Mockfly's dynamic mock API responses feature allows you to define multiple responses per endpoint and control their delivery using custom rules, all through an intuitive graphical user interface. With 16 comparators, JSONPath expressions over the body and AND/OR groups, Mockfly enables you to create more realistic and comprehensive testing scenarios, improving the quality of your final product.