What's New

Generate synthetic data in API responses

When setting up testing environments for software applications, it's often necessary to have realistic data to test various functionalities. Using synthetic data can help in creating diverse test cases without the need for real data or the risk of exposing sensitive data.

This can also be useful in early stages of development where the real services are not yet available and the mock API’s need to look and feel like the real thing for testing and demonstration purposes.

Hoverfly Cloud now supports built in functions to generate synthetic data in your simulated API responses.

Over and above the existing ability to generate random strings, time and date offsets, and numbers within ranges, as of Hoverfly 1.6.3, the templating engine now includes support for the Go-Fakeit library which contains over 200 functions for generating data such as names, places, foods, numbers, words, time-zones, currencies, languages and many more forms of data.

Supported Faker functions can be found here

CSV key-value data in API responses

When developing and testing a new system, a set of test data is usually pre-defined that will allow all execution flows to be tested. In these cases, we would want our API simulations to respond with appropriate meaningful data in a way that mimics a real back-end service with a data store.

We could do this by generating multiple hard-coded responses to cater for every conceivable request, however this does mean your simulation files will grow large and contain much duplication.

As of Hoverfly 1.7.0 we now support CSV data sources to be uploaded with the simulations, and these can be used in the templated responses to provide real data back based on a key and column combination.

Combined with the conditional logic built into the templating engine, Hoverfly Cloud can tailor the responses based on the lookup data in the CSV making the mocks both more intelligent, and realistic.

Read about CSV capabilities here

Simulating a persistent back-end

Hoverfly Cloud can now retrieve values previously passed into a service and render them back, creating the ability to mock a persistent back-end.

Each Hoverfly instance maintains an in-memory journal containing the previous 1000 Request-Response pairs made to the service.

Hoverfly contains a feature to allow rendering of previous parts of a pre-recorded Request-Response pair from this journal. An identifier for any part of the Request. For example: Request.QueryParam.id is passed into a {{ journal }} templating function along with a value that should satisfy that statement and has been previously recorded in the journal.

The {{ journal }} templating function then retrieves that Request-Response pair from the journal and then renders back any part of it. This is done by specifying an XPath or JSONPath expression along with whether to execute the query against the Request or the Response.

This feature in effect gives the user the ability to retrieve the state historically stored, and render it, which provides the useful illusion of a persistent back-end.

Read about Journal templating features here

Use variables in your simulations

The scripting engine supports custom functions to store and retrieve values in named variables. This allows for the generation of responses that might require looping a certain amount of times, or storing the values of arithmetic functions for further processing later in the template. Variable can also be used to store the result of a faker function and reuse it throughout the template in order to keep consistency in the output.

Read about the use of Variables here

Perform arithmetic in your simulations

You can perform simple addition, subtraction, multiplication and division between any two numbers using the new functions that have been added to the templating engine. These will return the result, either into the template, or if they are enclosed in another function, then into that function.

Read about the arithmetic functions here

Looping over JSON results

The {{#each}} block now supports looping over the results of a JSONPath query that returns an array.

This is useful when the incoming Request body contains a collection of keys through which you need to loop and render an appropriate response.

You can nest mathematical functions within a loop and when used with variables, calculate things like totals for rendering in the response as required.

Last updated