Regular Expression Matcher
You can also use regular expressions if you need to match a particular JSON structure. This is useful if you have various similar request payloads that you cannot discern using JSON, JSON Partial or JSONPath matchers. For example, if you need to match on the existence of a number of certain keys and you do not know their values.
Be aware that the regular expression engine is written in GO and conventions used by that language must be used. In particular it does not support Lookahead or Lookbehind assertions.
Regular expression matching example:
In this example the regular expression will match provided there is a transactionId with a value of 1000, a store, a clientUniqueId and Items. If any are missing it will not match.
This would not be possible with JSONPartial as the values of store, clientUniqueId and items are variable and not known and cannot be specified in a JSON snippet. Only transactionId is known.
JSONPath cannot currently be used if configured through the Hoverfly Cloud editor as it would only be able to return the existence of a single JSON key at a position within the document. Hoverfly does support matcher chaining which will allow you to chain a number of matchers together, however the Hoverfly Cloud editor does not yet support this yet, and if you would prefer to use this method over regular expressions you will need to manually edit the Hoverfly Simulation as described here.)
It should be noted that the regular expression can only check for JSON keys that are in the order stated in the matching string. It will not match if they are in a different order in the payload. The GO regexp package used in Hoverfly does not support Lookahead or Lookbehind assertions.
Matcher value
Positive payload
Non match payload
(missing store)
(?s).*("transactionId": 1000).*store.*clientUniqueId.*items.*
{
"someField": "a",
"transactionId": 1000,
"anotherField": "b",
"store": "c",
"clientUniqueId": "12345",
"items": ["item1", "item2", "item3"],
"extraField": "d"
}
{
"someField": "a",
"transactionId": 1000,
"anotherField": "b",
"shop": "c",
"clientUniqueId": "12345",
"items": ["item1", "item2", "item3"],
"extraField": "d"
}
Last updated