Rules

Rules feature provide a robust mechanism for dynamically modifying API responses based on intercepted request data. By applying rules, users can tailor responses to specific conditions, enhancing the accuracy and flexibility of API testing scenarios.

Example :

We will utilize the Add_To_Cart request example from the previous section to apply rules and validate the response.

//Request Body
{
  "productId": "1000",
  "productName": "iphone 15",
  "company": "Apple",
  "price": "85000"
}

//Response Body
{
  "cartId": "CR230",
  "message": "Added Successfully"
}

// This above response User will get without using Rules.
  • Interceptor uses rules in two sections, one is Client Request Rules & the other is Client Response Rules.

Client Request Rules : These rules modify the request body according to specified scripts before forwarding the updated body to the client URL to match the client's url request body before execution.

Client Response Rules : These rules intercept the incoming response body from the client, apply specified scripts, and then provide the user with the updated response.

  • Now we have written Rules script to update response conditionally.

// Interceptor Client Request Rule 
 req.set("country","India");

 // Interceptor Client Response Rule 
 res.set("message","Added to cart successfully");

 // Hostbox Rule
 if(req.get("country") == "India") { 
    res.set("currency","INR");
} else if(req.get("country") == "USA") { 
    res.set("currency","USD");
}

  • Now navigate to API module and execute this Interceptor request following the steps provided in previous section and User will get the conditionally updated response.

//Expected Response Body after rule implementation
{
    "cartId": "CR230",
    "currency": "INR",
    "message": "Added to cart successfully"
}

  • For deeper insights into the practical applications of Transformer Rules, navigate to: : Rules .

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.