https://linen.dev logo
j

jaspal singh

09/07/2023, 6:34 PM
Hi everyone. I am new to Wiremock and need some help/suggestions. I am trying to set a request and response pair. This means I always want to get a response A when the request is B. Request files are of XML type. I am able to use the below code to use the XML file path in the response body using "bodyFileName" parameter.
Copy code
{
  "request": {
    "method": "GET",
    "url": "/body-file"
  },
  "response": {
    "status": 200,
    "bodyFileName": "path/to/myfile.xml"
  }
}
I am not sure how can I also give "bodyFileName" in the request body. As I want to give a path for my request xml file the way I have given on the response. Kindly advice. thanks
a

Aaron

09/07/2023, 7:06 PM
So you only want to deem the request as a match if the body data matches the data in a separate request.xml file?
j

jaspal singh

09/07/2023, 7:40 PM
I want to give path for my request XML and path for my response XML (I am able to do that using the above code). I need to know how to give path for request XML file.
Yes if the request matched only then it should have response
Copy code
{
  "request": {
    "method": "GET",
    "url": "/body-file"
  },
  "response": {
    "status": 200,
    "bodyFileName": "path/to/myfile.xml"
  }
}
is there a way to add "bodyFileName" in the request
a

Aaron

09/07/2023, 7:43 PM
AFAIK, no -
bodyFileName
is only available to the response object. Usually, the requests would have something more specific than the entire request body that differentiate them. Are your request bodys unique in specific ways that you can tell one request from another without matching the entire body? JSON example:
Copy code
// request 1
{
  "id": 1234,
  ...
}
vs.
Copy code
// request 2
{
  "id": 0987,
  ...
}
j

jaspal singh

09/07/2023, 7:45 PM
Thanks. Just to understand. I am hardcoding the XML in the request as Body?
Copy code
{
    "request": {
        "method": "GET",
        "url": "/body-file"
        "body": "add the xml content here??"
    }
Are you referring to the above format?
a

Aaron

09/07/2023, 7:48 PM
It would be
bodyPatterns
, but I’m not suggesting hardcoding the entire body.
j

jaspal singh

09/07/2023, 7:49 PM
Sorry I am very new to Wiremock...do yu have the example pls
a

Aaron

09/07/2023, 7:49 PM
Do you have an example of two bodies, or more specifically, what you could use to say “oh, that should get response A” and “oh, that should get response B”
j

jaspal singh

09/07/2023, 7:51 PM
Example of request XML:
<?xml version="1.0"?> <customers> <customer id="55000"> <name>Charter Group</name> <address> <street>100 Main</street> <city>Framingham</city> <state>MA</state> <zip>01701</zip> </address> <address> <street>720 Prospect</street> <city>Framingham</city> <state>MA</state> <zip>01701</zip> </address> <address> <street>120 Ridge</street> <state>MA</state> <zip>01760</zip> </address> </customer> </customers>
how can I have a path or use this XML as an input in the request.
a

Aaron

09/07/2023, 7:55 PM
So, assuming that custom ID is the unique portion of the request body, you can use XPath matching. Rough guessing is something like this, but gimme a few and I can refine it
Copy code
{
  "request": {
    ...
    "bodyPatterns" : [ {
      "matchesXPath" : "/customers/customer/@id = \"55000\""
    } ]
    ...
  },
  ...
}
Don’t use a ton of XML/XPath, so a little rusty on it 😅
j

jaspal singh

09/07/2023, 7:56 PM
Thanks Aaron. I will give this a try. Thanks for your time.
a

Aaron

09/07/2023, 8:06 PM
Ok! I was a little off - something like this should work:
Copy code
"bodyPatterns": [
      {
        "matchesXPath": {
          "expression": "//customers/customer/@id",
          "equalTo": "55000"
        }
      }
    ]