Home > Enterprise >  WireMock: How to pick out a specific file based on one field in a XML
WireMock: How to pick out a specific file based on one field in a XML

Time:10-30

I have several XML files. Using wiremock, I wish to select one of the files based on one of its fields.

My mapping JSON file currently looks like this:

{
  "priority": 1,
  "request": {
    "method": "POST",
    "url": "/PAXGetEntitlement",

    "bodyPatterns" : [ {
      "equalToXml" : "<RNUM>AE767818</RNUM>"
    } ]
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "text/xml"
    }
  }
}

The top (and relevant) part of the file I want to pick out ('AE767818-get-entitlement.xml') looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <ns:getEntitlementDataResponse xmlns:ns="http://remote.interfaces.dataretrievalservices.pax.curam">
      <ns1:Document xmlns:ns1="http://remote.interfaces.paxdataretrievalservices.pax.curam">
        <GetEntitlementDetailsResponse>
          <ResponseHeader>
            <SessionID>PAX1600425562</SessionID>
            <VersionNumber>1.0</VersionNumber>
            <CompletionIndicator>true</CompletionIndicator>
            <RNUM>AE767818</RNUM>
            <CRN />
            <EventMessages />
          </ResponseHeader>

When I send a request from Postman, I get a response saying "Body does not match".

I was under the (seemingly incorrect) impression that I only had to specify 1 field in the equalToXml parameter to get a match but Wiremock is complaining that I'm not specifying the entire body of the file.

Where am I going wrong?

CodePudding user response:

Deems a match if the attribute value is valid XML and is semantically equal to the expected XML document. From the XML equality documentation. This means that the entire body of the request must match the value.

Instead, we can use xpath matching to only match if that individual field matches. Something like the following should work:

...
"bodyPatterns" : [ {
      "matchesXPath" : { 
        "expression": "/soapenv:Envelope/soapenv:Body/ns:getEntitlementDataResponse/ns1:Document/GetEntitlementDetailsResponse/ResponseHeader/RNUM/text()",
        "equalTo": "AE767818"
        }
    } ]
...
  • Related