Rules
Rules provide a robust mechanism for dynamically modifying intercepted requests and responses. By applying rules, users can tailor the request forwarded to the client and the response returned to the caller based on specific conditions, enhancing the accuracy and flexibility of API testing scenarios.
Interceptor applies rules in two sections — Client Request Rules and Client Response Rules.
Client Request Rules : Modify the request before it is forwarded to the client URL — for example ireq.setBody(...) or ireq.setHeader(...) — so it matches what the client endpoint expects.
Client Response Rules : Intercept the response coming back from the client and reshape it before it is returned to the caller — for example ires.setBody(...) or ires.setHeader(...).
- Mental model —
i*vso*:i= in-transit (live traffic),o= on-file (saved templates). ireq/iresedit the live request forwarded to the client and the live response returned to the caller — use these to actually change what moves.oreq/oresedit the saved expected request/response templates — they only affect matching and logging, and do not change live traffic.- The same convention applies to
setBody,setHeader, andsetParam. If a rule isn't changing what the other side sees, you're probably usingo*where you meanti*.
Example :
We will apply rules to the request created in the previous section and validate the updated response.
Add the scripts under the relevant section. The example below reshapes the live request forwarded to the client and the live response returned to the caller:
// Client Request Rule — runs before the request is forwarded to the client URL
ireq.setBody(`{ "field": "value" }`);
// Client Response Rule — runs on the response before it is returned to the caller
ires.setBody(`{ "status": "SUCCEEDED" }`);
ires.setHeader("x-custom-header", "{{yourVariable}}");Now navigate to the Studio module and execute this Interceptor request following the steps from the previous section. The Interceptor applies the rules, so the request forwarded to the client URL and the response returned to the caller are updated accordingly:
// Request forwarded to the client URL — set by the ireq rule
{
"field": "value"
}
// Response returned to the caller — set by the ires rule
{
"status": "SUCCEEDED"
}Through dynamic adjustments based on request conditions, Interceptor enhances adaptability and accuracy, thereby streamlining development cycles and ensuring smoother testing processes. To fully leverage the potential of Interceptor's rules feature, users are encouraged to thoroughly test and explore its capabilities.