[mostly code] musings of a software developer from nairobi 🇰🇪 | learning
Using Wiremock to Mock API Responses: Part 3 - Response Templating using Handlebars Helpers
October 18, 2021
In the previous 2 parts of this series, we:
Part 1: introduced Wiremock, and showed how it could be used to mock a simple response from a dummy “Songs API”
Part 2: showed how to return dynamic responses using details from the incoming request (i.e. request parameters)
In this part, we’ll continue with the topic of dynamic responses, but this time making use of Handlebars Helpers, as well as other built-in helpers, to generate even more dynamic responses.
This post is full of practical examples to ease understanding of Handlebars usage.
Note: Before proceeding, please ensure you’ve enabled response templating in Wiremock; this has been outlined in Part 2 of the series.
Let’s take another look at our Songs API response which we’re using for a demo:
Let’s say, for example, that we want the songInfo to be in UPPER CASE; i.e. have the API return:
We can make use of the upper helper which is enabled by Handlebars:
Note: we’re using a new URL pattern (dynamic=true) to allow differentiation between this and the previous version in Part 2.
Handlebars also provides a helper to get the current timestamp.
For example, let’s say we wanted to add a "requestedAt" field to the response metadata. For this, we can use the now helper:
The response would be:
Handlebars also provides date helpers to allow for formatting of timestamps.
For example, let’s say we wanted the likedOn date format to be something like ‘Saturday, 4th December, 2021’. To do this, we can use the dateFormat helper:
This would produce:
upper, now, dateFormat are only a subset of all the available helpers.
For a full list, check out the StringHelpers.java file on the Handlebars repo.
Additionally, I’ve provided a “kitchen sink” API that demonstrates all of these helpers.
NB: Some special notes on exclusions from the kitchen sink:
There seems to be a bug with handling of JSON that has special characters (e.g. newline characters) between Handlebars and Wiremock. As a result, the following helpers have not been added to the kitchen sink: wordWrap
numberFormat has flags for parseIntegerOnly and roundingMode, but these don’t seem to be in use
We’ll use these exclusions as use-cases later in this series to build custom extensions for Wiremock.
Summary
In this part of the series, we dove deeper into Response Templating in Wiremock, seeing how to make use of Handlebars Helpers to enable us to generate even more dynamic responses.
We also showed how (almost) all of the string helpers can be used via a “Kitchen Sink API” (this is available on this series’ accompanying repository on GitHub: wiremock-docker-demo).
References
Shout out to rodolpheche for building and maintaining the Wiremock docker image.
All the code used in this blog post is available in this repository: wiremock-docker-demo
Coming in Part 4: Using Custom Extensions in Wiremock
In the next part, we’ll see how to load custom extensions to Wiremock, for when we want to enable very customized functionality in our mocked APIs.
We’ll use the custom extensions to go around some of the exclusions noted above.