Reputation: 57
I've been trying to create a stub system for a SOAP service using wiremock standalone. I've got the response.xml s ready and I'm required to return them based on an id value in the SOAP request xml. For example, Soap Request:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddressType>
<addressId>Q1004DB</addressId>
</AddressType>
</soap:Body>
</soap:Envelope>
As I mentioned before, I have response xmls set for each of the address Ids. In this case, I've got a response xml named Q1004DB.xml which should be returned as response of this request. So I'm trying to figure out a logic to be put in place which will enable me to do this.
It would be great if someone can help me with how I can implement a logic in wiremock standalone where I can return a response based on the values in the input request xml. Thanks in advance !
As a first step, I tried returning the addressId as the response. I followed a couple of answers from StackOverflow and saw this format but this doesn't seem to work. Can someone please tell what might be wrong here ?
{
"request": {
"url": "/ws/AddressSoapService",
"method": "POST"
},
"response": {
"body": "{{$.addressType.addressId}}",
"headers": {
"Server": "Microsoft-IIS/8.0",
"Connection": "Keep-Alive",
"Web-Service": "DataFlex 18.1",
"Access-Control-Allow-Headers": "content-type",
"Date": "Tue, 26 Jun 2018 07:45:47 GMT",
"Strict-Transport-Security": "max-age=31536000",
"Cache-Control": "private, max-age=0",
"Content-Type": "application/soap+xml; charset=utf-8"
}
}
}
Upvotes: 1
Views: 902
Reputation: 4149
You need to use the xPath
template helper to extract values from the request in this case.
Assuming you want to generate a response that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddressType>
<addressId>Q1004DB</addressId>
</AddressType>
</soap:Body>
</soap:Envelope>
Then you'd need a stub definition like this:
{
"name" : "SOAP example",
"request" : {
"url" : "/ws/AddressSoapService",
"method" : "POST"
},
"response" : {
"status" : 200,
"body" : "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soap:Body>\n <MyResponse>\n <addressId>{{xPath request.body '//AddressType/addressId/text()'}}</addressId>\n </MyResponse>\n </soap:Body>\n</soap:Envelope>",
"headers" : {
"Content-Type" : "application/xml"
},
"transformers" : [ "response-template" ]
}
}
Note that the body is inlined here. You could also put the body into a file and reference it and it would work the same way.
Upvotes: 1