Adding templates to a simulationΒΆ

See also

Please carefully read through Templates alongside this tutorial to gain a high-level understanding of what we are about to cover.

In this tutorial, we are going to go through the steps required to generate and use a matching template.

Let’s begin by capturing some traffic and exporting a simulation.

hoverctl start
hoverctl mode capture
curl --proxy http://localhost:8500 http://echo.jsontest.com/foo/baz/bar/spam
hoverctl export simulation.json
hoverctl stop

Which gives us this output:

time="2016-12-22T08:56:24Z" level=info msg="Hoverfly is now running" admin-port=8888 proxy-port=8500 
time="2016-12-22T08:56:24Z" level=info msg="Hoverfly has been set to capture mode" 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100    38  100    38    0     0    167      0 --:--:-- --:--:-- --:--:--   168
{
   "foo": "baz",
   "bar": "spam"
}
time="2016-12-22T08:56:25Z" level=info msg="Successfully exported to simulation.json" 
time="2016-12-22T08:56:25Z" level=info msg="Hoverfly has been stopped" 

If you take a look at your simulation.json you should notice these lines in your request.

"request": {
	"requestType": "recording",
	"path": "/foo/baz/bar/spam",
	"method": "GET",

Modify them to:

    "request": {
      "requestType": "template",
      "path": "/foo/*/bar/spam",
      "method": "GET",

Save the file as simulationimport.json and run the following command to import it and cURL the simulated endpoint:

hoverctl start
hoverctl mode simulate
hoverctl import simulationimport.json
curl --proxy http://localhost:8500 http://echo.jsontest.com/foo/QUX/bar/spam
hoverctl stop

The same response is returned, even though we created our simulation with a request to http://echo.jsontest.com/foo/baz/bar/spam in Capture mode and then sent a request to http://echo.jsontest.com/foo/QUX/bar/spam in Simulate mode.

As you can see, templating allows us to match URLs using globbing.

Note

Key points:

  • To do templating, capture a simulation, export it, edit it
  • While editing, change "requestTypes" value to "template"
  • Substitute strings in URLs with the wildcard * to return one response for more than one request
  • Re-import the simulation