Rest Rules
Rules within PruTAN's Hostbox module offer powerful capabilities for response customization. When a simulated request is executed, these rules allow dynamic, conditional adjustments to the response. This enables tailored and contextually relevant responses, enhancing the flexibility and accuracy of the testing environment.
Example:
JSON Request
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"
}
// The above response is returned without using Rules.Now, we have written a Rules script to update the response conditionally:
// Rules script
if (req.getBody("price") > 5000) {
res.setBody("discount", 500);
}Now, navigate to the API module and execute this request following the steps provided in the previous section. You will get the conditionally updated response:
// Expected Response Body after rule implementation
{
"cartId": "CR230",
"discount": 500,
"message": "Added Successfully"
}In the above response, you will see a new conditionally added key:value pair "discount": 500 using rules.
XML Request
We will create another request within the E-Comm collection with contentType XML & label Order_Request with sample request & response bodies, apply rules & validate the response.
// Request Body
<orderRequest>
<customer>
<customerId>123456</customerId>
<name>John Doe</name>
<email>john.doe@email.com</email>
</customer>
<products>
<product>
<productId>789</productId>
<productName>Laptop</productName>
<quantity>2</quantity>
<price>1000.00</price>
</product>
</products>
<shippingAddress>
<address>123 Main St</address>
<city>Anytown</city>
<zipCode>12345</zipCode>
<country>USA</country>
</shippingAddress>
<paymentMethod>
<!-- <cardNumber>{{fn.generateCard(16)}}</cardNumber> -->
<expiryDate>12/25</expiryDate>
<cvv>123</cvv>
</paymentMethod>
</orderRequest>
// Response Body
<orderResponse>
<orderId>ODR123456789</orderId>
<status>Success</status>
<message>Your order has been placed successfully!</message>
<totalAmount>2000.00</totalAmount>
<shippingDetails>
<estimatedDeliveryDate>2024-01-05</estimatedDeliveryDate>
<shippingMethod>Standard</shippingMethod>
<shippingCost>50.00</shippingCost>
</shippingDetails>
</orderResponse>Now, we have written a Rules script to update the response conditionally:
// Rules script
res.setBody("orderRequest.cardNumber", req.getBody("orderRequest.paymentMethod.cardNumber"));- Nested element in JSON/XML data can be accessed using dot (.) notation.
Now, navigate to the API module and execute this request following the steps provided in the previous section. You will get the conditionally updated response:
// Client request body from API module
<orderRequest>
<customer>
<customerId>123456</customerId>
<name>John Doe</name>
<email>john.doe@email.com</email>
</customer>
<products>
<product>
<productId>789</productId>
<productName>Laptop</productName>
<quantity>2</quantity>
<price>1000.00</price>
</product>
</products>
<shippingAddress>
<address>123 Main St</address>
<city>Anytown</city>
<zipCode>12345</zipCode>
<country>USA</country>
</shippingAddress>
<paymentMethod>
<cardNumber>{{$generateCard(16)}}</cardNumber>
<expiryDate>12/25</expiryDate>
<cvv>123</cvv>
</paymentMethod>
</orderRequest>// Expected Response Body after rule implementation
<?xml version="1.0" encoding="UTF-8"?>
<orderResponse>
<orderId>ODR123456789</orderId>
<status>Success</status>
<message>Your order has been placed successfully!</message>
<totalAmount>2000.00</totalAmount>
<shippingDetails>
<estimatedDeliveryDate>2024-01-05</estimatedDeliveryDate>
<shippingMethod>Standard</shippingMethod>
<shippingCost>50.00</shippingCost>
</shippingDetails>
<orderReques>
<cardNumber>8056076159109160</cardNumber>
</orderReques>
</orderResponse>- In above response user will be able to see a new conditionally added value
cardNumbernested withinorderRequestusing randomly generated value using pruTAN'sgenerateCard()function, used in client's request body and updated it in response using rules.
PruTAN's Hostbox Rules feature empowers users to fine-tune responses with precision and agility. By harnessing dynamic adjustments based on request conditions, it enhances the testing environment's adaptability and accuracy, ultimately facilitating smoother development cycles