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.

  • 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 uses hoverfly v1.3.5 which supports templating as described here: https://docs.hoverfly.io/en/latest/pages/keyconcepts/templating/templating.html

  • In this tutorial we will create a service and configure it's simulation to support a single GET method that uses response templating.

Part 1. Start up a new service and configure a GET method

Follow these steps:

  1. Navigate to the Dashboard

  2. Click the orange +Add button in the Services section, this will open a dialogue window.

  3. Leave the Simulation field blank: we will create a Simulation directly on this service.

  4. 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.

  5. Leave the rest of the defaults and click Confirm

  6. 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.

  7. Now let's configure a simple GET method on the /myapi path.

    1. Click on the (unsaved) simulation link for this service.

    2. You will be sent to the simulation that is unique to this service.

    3. Click on +Add endpoint and modify the Request matcher to look for a GET method and with an Exact match of /myapi

    4. For the Response, add some simple text in the Body

  8. Click Apply Changes at the top of the page to save the simulation.

  9. 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.

Part 2. Extracting values from the Request

Follow these steps:

  1. 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.

  2. From the Dashboard, Navigate to the (unsaved) simulation for the service

  3. Replace the response body with the block below. Make sure you enable templating at the bottom of the page.

    [1. CHECK THE PATH]
    The path of the request is {{Request.Path}}
    {{#equal Request.Path "lucky"}}You hit the lucky path!{{/equal}}
  4. 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".

  5. 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.

  6. Save the simulation at the top of the page, and then test the GET endpoint again. You should receive a response:

    [1. CHECK THE PATH]
    The path of the request is myapi
  7. 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.

  8. To fix this, change the Path's Exact match to a Glob match as follows:

  9. 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:

    [1. CHECK THE PATH]
    The path of the request is lucky
      You hit the lucky path!

Part 3. Conditional Statements

Follow these steps:

  1. 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.

  2. From the Dashboard, Navigate to the (unsaved) simulation for the service

  3. Add the block below to the the Response Body beneath what's already there, and then Apply Changes:

    [2. CHECK FOR QUERY PARAMETERS]
    {{#if (Request.QueryParam.price) }}
    	A price of {{ Request.QueryParam.price }} was provided
    	[3. CHECK EQUALITY]
    	{{#equal Request.QueryParam.price "10"}}
        		Price is 10!
      	{{/equal}}
    {{else}}
    	No price was provided
    {{/if}}
  4. Resubmit the same GET query as before, without any query string: https://response-templating-xxxxxxx.hoverfly.io/lucky

  5. You should receive the following:

    [1. CHECK THE PATH]
    The path of the request is lucky
      You hit the lucky path!
    
    [2. CHECK FOR QUERY PARAMETERS]
    	No price was provided	
  6. 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.

  7. 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:

    [4. USING UNLESS]
    {{#unless Request.QueryParam.product}}
    	No product was provided
    {{else}}
    	A product named "{{Request.QueryParam.product}}" was provided
    {{/unless}}
  8. 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

Part 4. Iterating over request collections

Follow these steps:

  1. 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.

  2. Add the block below to the the Response Body beneath what's already there, and then Apply Changes:

    [5. LOOP THROUGH HEADERS]
    {{#each Request.Header}}
    	{{@index}} : {{@key}} : {{this}}
    {{else}}
    	No Request Headers were provided
    {{/each}}
  3. Execute the endpoint once again and you will receive all of the headers sent with the request, their index, key and value.

  4. 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:

    [6. CHECK EQUALITY WITHIN A LOOP]
    {{#each Request.QueryParam}}
    	{{@index}} : {{@key}} : {{this}} {{#equal this "10"}} The {{@key}} query parameter has a value of 10!{{/equal}}
    {{else}}
    	No Query Prameters were provided
    {{/each}}
  5. 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.

  6. Now execute the end point with and without a variable in the query string with a value of 10:

    1. https://response-templating-xxxxxxx.hoverfly.io/lucky?product=apples&quantity=5 results in:

      [6. CHECK EQUALITY WITHIN A LOOP]
      	0 : product : apples 
      	1 : quantity : 5 
    2. https://response-templating-xxxxxxx.hoverfly.io/lucky?product=apples&quantity=10 results in:

      [6. CHECK EQUALITY WITHIN A LOOP]
      	0 : product : apples 
      	1 : quantity : 10  The quantity query parameter has a value of 10!

Part 5. Helper methods

Follow these steps:

  1. Try and use some of the various helper methods to see how they work. The full list including methods for working with dates and times can be found here: https://docs.hoverfly.io/en/latest/pages/keyconcepts/templating/templating.html

  2. As a quick reference:

Congratulations! You have completed this Tutorial, "Response Templating."

Last updated