Loose request matching using a Request Matcher

See also

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

In some cases you may want Hoverfly to return the same stored response for more than one incoming request. This can be done using Request Matchers.

Let’s begin by capturing some traffic and exporting a simulation. This step saves us having to manually create a simulation ourselves and gives us a request to work with.

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

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

"path": [
	{
	   "matcher": "exact",
	   "value": "/foo/baz/bar/spam"
	}
 ]

Modify them to:

"path": [
	{
	   "matcher": "glob",
	   "value": "/foo/*/bar/spam"
	}
 ]

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.

See also

In this example we used the globMatch Request Matcher type. For a list of other Request Matcher types and examples of how to use them, please see the Request matchers section.

Note

Key points:

  • To change how incoming requests are matched to stored responses, capture a simulation, export it, edit it

  • While editing, choose a request field to match on, select a Request Matcher type and a matcher value

  • Re-import the simulation

  • Requests can be manually added without capturing the request