Tutorial 4: Response Templating
In this walkthrough you will create a simple API simulation which uses various types of response templating to modify the response.
Last updated
In this walkthrough you will create a simple API simulation which uses various types of response templating to modify the response.
Last updated
Response Templating in Hoverfly Cloud makes use of {{handlebar}} syntax to give your response access to variables sent in the request, perform equality logic, and perform conditional logic in structuring your response.
Hoverfly Cloud supports templating as described here: Using templating in simulations
In this tutorial we will create a service and configure it's simulation to support a single GET method that uses response templating.
Follow these steps:
Navigate to the Dashboard
Click the orange +Add button in the Services section, this will open a dialogue window.
Leave the Simulation field blank: we will create a Simulation directly on this service.
In the Name type "response-templating". You will notice that this prefixes your unique hoverfly.io URL and will be the endpoint for this service.
Leave the rest of the defaults and click Confirm
You will be returned to the dashboard, and if all went ok your new service will be spinning up. When it is ready it will have a green tick next to it.
Now let's configure a simple GET method on the /myapi path.
Click on the (unsaved) simulation link for this service.
You will be sent to the simulation that is unique to this service.
Click on +Add endpoint and modify the Request matcher to look for a GET method and with an Exact match of /myapi
For the Response, add some simple text in the Body
Click Apply Changes at the top of the page to save the simulation.
Test the your service is working. Check the path to the GET method you have configured in your API client. (The URL can be copied from the Dashboard, it will be something like: https://response-templating-xxxxxxx.hoverfly.io
and remember to append /mapapi
to the end.
Follow these steps:
Hoverfly can re-use values from the Request path, query string, headers and body. You can also use jsonpath or xpath to query the Request body which we won't cover here.
From the Dashboard, Navigate to the (unsaved) simulation for the service
Replace the response body with the block below. Make sure you enable templating at the bottom of the page.
This script will return the path used in the Request, as well as returning a specific block of text if the path is equal to "lucky".
Hoverfly's templating can perform simple equality checks to evaluate Request variables against strings, and then execute the statements contained within the {{#equal}} {/equal} block.
Save the simulation at the top of the page, and then test the GET endpoint again. You should receive a response:
We are not getting the "You hit the lucky path!" message. Even if you change the path in your API client to end in /lucky, you will get a matching error from Hoverfly. This is because our request matcher is looking for an Exact Path match for /myapi.
To fix this, change the Path's Exact match to a Glob match as follows:
Remember to click Apply changes. Now you can try https://response-templating-xxxxxxx.hoverfly.io
and append /lucky
to the end. You should receive the response:
Follow these steps:
Hoverfly can use simple conditional statements (#if
and #unless
) to check for the presence of variables in the request, and it can perform simple equality checks (#equal
) to establish if a variable is equal to a particular string.
From the Dashboard, Navigate to the (unsaved) simulation for the service
Add the block below to the the Response Body beneath what's already there, and then Apply Changes:
Resubmit the same GET query as before, without any query string: https://response-templating-xxxxxxx.hoverfly.io/lucky
You should receive the following:
Now try it again using a price of 10 and a price of 15 to check the various logic paths: https://response-templating-xxxxxxx.hoverfly.io/lucky?price=10
https://response-templating-xxxxxxx.hoverfly.io/lucky?price=15
- You will notice that the #if
statement checks for the presence of a variable only, it cannot check that the variable is equal or not equal to something or execute any other type of operator.
You can also perform conditional logic inversely using the {{#unless}} {{/unless}}
statement if this makes more sense in your scenario. This would be the equivalent of "Not If". Add the block below to the the Response Body beneath what's already there, and then Apply Changes:
Now execute the end point with and without a product variable in the query string: https://response-templating-xxxxxxx.hoverfly.io/lucky?product=apples
https://response-templating-xxxxxxx.hoverfly.io/lucky
Follow these steps:
Hoverfly can iterate through the query string parameters and the headers that were sent in the Request. At the same time it can perform simple equality logic and conditional logic.
Add the block below to the the Response Body beneath what's already there, and then Apply Changes:
Execute the endpoint once again and you will receive all of the headers sent with the request, their index, key and value.
Now let's add conditional logic to a loop. Add the block below to the the Response Body beneath what's already there, and then Apply Changes:
The block above will loop through and print each variable in the query parameter list, and it will print out a message for any that have a value of 10.
Now execute the end point with and without a variable in the query string with a value of 10:
https://response-templating-xxxxxxx.hoverfly.io/lucky?product=apples&quantity=5
results in:
https://response-templating-xxxxxxx.hoverfly.io/lucky?product=apples&quantity=10
results in:
Follow these steps:
Try and use some of the various helper methods to see how they work. You can learn how to use all these methods by starting to read from here: Render back any part of the Request
As a quick reference:
Congratulations! You have completed this Tutorial, "Response Templating."