Advanced request matching, bodyPatterns matching and response definition
POST
to /api/to-do
. In order to test both success and failure cases it will be
necessary to return different responses depending on the post body (since the URL would always be the same).
We can do this by adding a body matching clause in the Advanced portion of the Request section.
Click the button to add the clause, select the match type from the drop-down, then write (or paste) the expected value or expression into the text area.
If your API uses JSON as its serialisation format you might want to match using equalToJson
:
NOT
checkbox can be used to negate the selected matcher.
ANY
, meaning that a request with any method
will match.
equalToJson
) and JSONPath (matchesJsonPath
).
equalToJson
performs a semantic comparison between the incoming JSON and the expected value, meaning that
it will return a match even when, for instance, the two documents have different amounts of whitespace.
You can also specify that array order an additional elements in the request JSON be ignored.
transaction_id
is unique to
each request:
${json-unit.regex}[A-Z]+
(any Java-style regular expression can be used)${json-unit.any-string}
${json-unit.any-boolean}
${json-unit.any-number}
matchesJsonPath
allows request bodies to be matched according to a JSONPath expression. The
JSONPath expression is used to select one or more values from the request body, then the result is matched against sub-matcher (equal to
, contains
etc.).
It is also possible to simply assert that the expression returns something, by selecting is present
from the list.
$.event
equal to
description-updated
) would match a request body of
equalToXml
and matchesXPath
.
equalToXml
performs a semantic comparison between the incoming and expected XML documents, meaning that it will return a match regardless of whitespace, comments and node order.
equalToXml
it is possible to ignore the value of specific elements using XMLUnit’s placeholder syntax. For instance if you
expected to receive an XML request body containing a transaction ID that changed on every request you could ignore that value like this:
matchesXPath
allows XML request bodies to be matched according to an XPath expression.
For instance, an XML request body like