Skip to content

Response Templating

Shubhendu Madhukar edited this page Mar 21, 2021 · 5 revisions

Response Templating

Most of the fields in your payload would be random data (for the testing purposes, of course). For example, phone numbers, current timestamps, future dates, invoice numbers, order numbers, service request ids, UUIDs etc need to be generated randomly using the testing tool. With response templating you can keep your scripts clean and without parameter files containing just random data.

You would still need to use your testing tools' APIs to use a seeded data source, for example if you want to use CSVs or .dat files. But if you just need a 10 digit random number, Kafka Service is all you need.

Currently Kafka Service supports all of the String Helpers provided by Handlebar.java (though few of those helpers might not make sense in this context). There are few custom helpers provided by Wiremock which can be used with Kafka Service.

Of course, pull requests are welcome in case any additional helpers are required.

Suppose you want to send following message to Kafka:

{
   "OrderCreatedAt": 1616320755763,
   "CustomerPhoneNuber": 1234567890
}

But you want to update these values for every message you send. You'd usually use your testing tool to generate random values and current timestamp for you. But you can also format POST body for any of the above APIs as:

{
  "message": "{ \"OrderCreatedAt\": {{now format='epoch'}}, \"CustomerPhoneNuber\": {{randomValue length=10 type='NUMERIC'}} }"
}

and you don't need to create parameters in your test tool, Kafka Service will handle that for you.

Following are few examples to help you get a sense of how templating works. Note that these examples have shamelessly been copied from Wiremock's documentation:

  • randomValue: Random strings of various kinds can be generated:

{{randomValue length=33 type='ALPHANUMERIC'}}

{{randomValue length=12 type='ALPHANUMERIC' uppercase=true}}

{{randomValue length=55 type='ALPHABETIC'}}

{{randomValue length=27 type='ALPHABETIC' uppercase=true}}

{{randomValue length=10 type='NUMERIC'}}

{{randomValue length=5 type='ALPHANUMERIC_AND_SYMBOLS'}}

{{randomValue type='UUID'}}

  • date/parseDate

{{date (parseDate '03/21/2021') offset='-1 days'}}

  • now (overwrites 'now' provided by StringHelpers in the documentation above)

{{now}}

{{now offset='3 days'}}

{{now offset='-24 seconds'}}

{{now offset='1 years'}}

{{now offset='10 years' format='yyyy-MM-dd'}}

{{now timezone='Australia/Sydney' format='yyyy-MM-dd HH:mm:ssZ'}}

{{now offset='2 years' format='epoch'}}

{{now offset='2 years' format='unix'}}

  • trim

{{trim ' Text with whitespaces '}}

  • base64

{{base64 'value to encode'}} {{base64 'aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kUXc0dzlXZ1hjUQ==' decode=true}}

  • urlEncode

{{urlEncode 'value to encode'}} {{urlEncode 'value%20to%20decode' decode=true}}

  • size

{{size 'abcde'}}

  • pickRandom {{{pickRandom '1' '2' '3'}}}
Clone this wiki locally