After creating multiple responses for each route, you can create more complex scenarios and serve the responses depending on the fulfillment of rules.
You can define an unlimited number of rules for each route. At each request, Mockoon will assert each response's rules and serve the response which contains the first matching rule(s). The rules are interpreted in the order they are declared:
[rule OR|AND rule] OR [rule OR|AND rule], the brackets symbolizing each route response.
To add a new rule to a response, go to the route response's Rules tab, click on "Add rule" and fill the fields:
By default, rules are interpreted in the order you added them. You can change their interpretation order by drag and dropping them:
You can temporarily disable the rules and serve the default response only. To activate this option, click on the "rules" icon next to the response list:
When this option is active, the default response will be always served and all the rules defined on this route will be ignored. Also, this option cannot be selected in addition to the random or sequential responses.
Inside a route response, rules are interpreted by default with the OR logical operator. When you have more than one rule in a route response, you can easily switch the operator applied when interpreting the rules, by clicking on the
OR|AND buttons at the left of the rules:
Rules have four parts:
In the dropdown menu you can choose between:
Depending on the target, the way to access properties may be different:
users.0.name. This is compatible with request's bodies of
text/xml. Properties containing dots are supported by escaping the dots:
multipart/form-dataonly supports fields. Uploaded files will be ignored.
filteror a path if the query string field is an object
For body and query string, if the property is an array, Mockoon will automatically check in the array if at least one item matches the value.
You can invert the comparison operator (! equals, ! regex match, etc.) by toggling on the exclamation mark button:
Multiple comparison operators are available in each rule:
Depending on the comparison operator chosen, equals or regex match, you can either set a simple text value like "expected value" or any kind of regex. To use a regex, you must write it without the leading and trailing slashes.
You can also test for empty values with the following regex:
The request number supports simple entries like
2 but also regexes, allowing you to return a different response for the first 3 calls
^[1-3]$ or failing on odd request indexes