SOAPAction
header and the request body XML.
Using the SOAPAction header
SOAP APIs typically use theSOAPAction
header to select the appropriate action for the call.
Although you can sometimes avoid this, it’s usually a good idea to add a header match for SOAPAction
as it’s more
efficient and faster than relying exclusively on the XML body.

Matching the request body with XML equality
When dealing with request bodies that are small and have no data of a transient nature (e.g. transaction IDs or today’s date)equalToXml
is a straightforward way to specify a match.
For instance given a SOAP service for managing a to do list, you may wish to mock an interaction matching a specific request to add an item:


Using placeholders to ignore transient values
The above example works fine when the request body doesn’t contain any transient data such as transaction IDs or the current date. However, if data that changes on each request is introduced it will be necessary allow a match to occur regardless of the actual value received. One way to do this is to use placeholders. Let’s assume the request body of our API now contains aTransactionId
element, which
must be a unique value for each request e.g.:

Matching the request body with XPath
When working with large SOAP requestsequalToXml
can become quite slow as it must perform a comparison on every node in the XML document.
It’s often faster to match specific elements within the document using the matchesXPath
operator,
and since this is a much looser approach to matching it’s another way to solve
the problem described above where frequently changing values are present.
When matching using XPath, your aim should be to target as few elements/attributes as possible while being able
to reliably distinguish between requests.
Given the same request body as in the previous section, we could use the following
XPath to match just on the value of the m:ToDoItem
element:

Using multiple XPath expressions
Sometimes you need to match on more than one XML element to be able to adequately distinguish between requests. Although XPath supports multiple predicates with logical and/or, often it can be easier to use multiple body matchers each targeting a single element. Suppose we added aUserId
field that we also wanted to target:
matchesXPath
body pattern for each of the elements we
care about:
