Rest Rules

Rules within PruTAN's Sandbox 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 reuse the POST Create Payment request from the previous section to apply rules and validate the response.

// Request Body
{
  "cardholderInformation": {
    "cardholderName": "John Doe",
    "companyName": "SP Ltd"
  },
  "paymentMethodSource": "CARD",
  "card": {
    "cardNumber": "4111111111111111",
    "expiryMonth": "12",
    "expiryYear": "27",
    "cardSecurityCode": "123"
  }
}

// Response Body (without rules)
{
  "paymentMethodId": "0105ARZ3NDEKTSV4RRFFQ69G5FAV",
  "merchantId": "0123456789101",
  "paymentMethodInformation": {
    "paymentMethodType": "CARD",
    "paymentMethodSource": "CARD"
  }
}

// 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("paymentMethodSource") == "CARD") {
  res.setBody("processingFee", 2.50);
  res.setBody("currency", "USD");
}

Now, navigate to the Studio 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
{
  "paymentMethodId": "0105ARZ3NDEKTSV4RRFFQ69G5FAV",
  "merchantId": "0123456789101",
  "cardholderInformation": {
    "cardholderName": "John Doe",
    "companyName": "SP Ltd"
  },
  "paymentMethodInformation": {
    "paymentMethodType": "CARD",
    "paymentMethodSource": "CARD",
    "cardInformation": {
      "bankIdentificationNumber": "123456",
      "cardBrand": "MASTERCARD",
      "cardType": "CREDIT",
      "cardFingerprint": "1Q2W3E4r5t6rfwewerwewrrw",
      "issuer": "RBC"
    },
    "paymentAccountReference": "111122223333444455556666677778",
    "storePaymentMethod": "DO_NOT_STORE"
  },
  "createdAt": "2019-07-30T06:43:40.252Z",
  "modifiedAt": "2019-07-30T06:43:40.252Z",
  "customData": {
    "property1": "string",
    "property2": "string"
  },
  "processingFee": 2.50,
  "currency": "USD"
}

In the above response, you will see two new conditionally added key:value pairs — "processingFee": 2.50 and "currency": "USD" — appended at the end by the rule because paymentMethodSource was CARD. The other fields (cardBrand, cardType, paymentAccountReference, createdAt, etc.) come from the simulated Sandbox response itself.

XML Request

The same scenario can be expressed using XML. Switch the request's Content Type to application/xml, provide an XML body, and apply a rule that copies the card number from the incoming request into the response while also adding a tokenized card value.

// Request Body
<paymentRequest>
  <cardholderInformation>
    <cardholderName>John Doe</cardholderName>
    <companyName>SP Ltd</companyName>
  </cardholderInformation>
  <paymentMethodSource>CARD</paymentMethodSource>
  <card>
    <cardNumber>4111111111111111</cardNumber>
    <expiryMonth>12</expiryMonth>
    <expiryYear>27</expiryYear>
    <cardSecurityCode>123</cardSecurityCode>
  </card>
</paymentRequest>

// Response Body (without rules)
<paymentResponse>
  <paymentMethodId>0105ARZ3NDEKTSV4RRFFQ69G5FAV</paymentMethodId>
  <merchantId>0123456789101</merchantId>
  <status>Success</status>
</paymentResponse>

Now, we have written a Rules script to update the response conditionally:

// Rules script
res.setBody("paymentResponse.echoedCardNumber", req.getBody("paymentRequest.card.cardNumber"));
res.setBody("paymentResponse.tokenizedCard", "{{$generateCard(16)}}");
  • Nested elements in JSON/XML data can be accessed using dot (.) notation.

Now, navigate to the Studio 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
<?xml version="1.0" encoding="UTF-8"?>
<paymentResponse>
  <paymentMethodId>0105ARZ3NDEKTSV4RRFFQ69G5FAV</paymentMethodId>
  <merchantId>0123456789101</merchantId>
  <status>Success</status>
  <echoedCardNumber>4111111111111111</echoedCardNumber>
  <tokenizedCard>8056076159109160</tokenizedCard>
</paymentResponse>
  • In the above response, the original cardNumber from the request has been carried over as echoedCardNumber, and a new tokenizedCard value has been generated dynamically using PruTAN's generateCard() function.

PruTAN's Sandbox 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.