Trying to use attribute of the request object while creating response while mocking a soap service #351

Closed
opened 2025-12-29 08:26:45 +01:00 by adam · 6 comments
Owner

Originally created by @msutar021 on GitHub (Jul 9, 2021).

I want to create a mock response for SOAP service which accepts XML request . I'm using XPathMatcher for request matching. Request is matching is done successfully. I need to read one attribute of request.body and create the response using that.
My XML request is like this. I need the token id to create the response body.

Request.txt

<?xml version='1.0' standalone='no'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.Test.nl/XMLHeader/10" xmlns:req="http://www.Test.nl/Betalen/COU/Services/RdplDbTknLystByOvkLyst/8/Req">
   <soapenv:Header>
      <ns:TestHeader>
         <ns:HeaderVersion>10</ns:HeaderVersion>
         <ns:MessageId>MsgId10</ns:MessageId>
         <ns:ServiceRequestorDomain>Betalen</ns:ServiceRequestorDomain>
         <ns:ServiceRequestorId>CRM</ns:ServiceRequestorId>
         <ns:ServiceProviderDomain>COU</ns:ServiceProviderDomain>
         <ns:ServiceId>RdplDbTknLystByOvkLyst</ns:ServiceId>
         <ns:ServiceVersion>8</ns:ServiceVersion>
         <ns:FaultIndication>N</ns:FaultIndication>
         <ns:MessageTimestamp>?</ns:MessageTimestamp>
      </ns:TestHeader>
   </soapenv:Header>
   <soapenv:Body>
      <req:RdplDbTknLystByOvkLyst_REQ>
         <req:AanleveraarCode>CRM</req:AanleveraarCode>
         <!--Optional:-->
         <req:AanleveraarDetail>CRMi</req:AanleveraarDetail>
         <req:BerichtId>BerId</req:BerichtId>
         <req:BerichtType>RdplDbTknLystByOvkLyst</req:BerichtType>
         <!--Optional:-->
         <req:OpgenomenBedragenGewenstIndicatie>N</req:OpgenomenBedragenGewenstIndicatie>
         <req:TokenIdLijst>
            <!--1 to 10 repetitions:-->
            <req:TokenId>0000083256</req:TokenId>
            <req:TokenId>0000083259</req:TokenId>
         </req:TokenIdLijst>
      </req:RdplDbTknLystByOvkLyst_REQ>
   </soapenv:Body>
</soapenv:Envelope>

I am using the below matcher models while creating mapping for the request.

  var matcherModel = new MatcherModel
            {
                Name = "WildcardMatcher",
                Pattern = "/xpathsoap",
                RejectOnMatch = false,
                IgnoreCase = true
            };

            var bodymatcherModel = new MatcherModel
            {
                Name = "XPathMatcher",
                Pattern = "//*[local-name() = 'RdplDbTknLystByOvkLyst_REQ']",
                IgnoreCase = true
            }; 
           
            var matcherModels = new[] { matcherModel };
            var pathModel = new PathModel { Matchers = matcherModels };

            var bodymatcherModels = new[] { bodymatcherModel };
            var BodyModel = new BodyModel { Matchers = bodymatcherModels };

            var requestModel = new RequestModel { Path = pathModel,  Body = BodyModel };
            return requestModel;

And response model in mapping

 IDictionary<string, object> header = new Dictionary<string, object>();
 header.Add("Content-Type", "Application/xml");

return new ResponseModel()
            {
                StatusCode = 201,
                Headers = header,
                Body = "{{ XPath.SelectNodes request.body \"//TokenIdLijst\"}}",
                UseTransformer = true
            };

The above code returns response as blank. I get whole request in the response.body if i use the below code without xpath

var response =  new ResponseModel()
            {
                StatusCode = 201,
                Headers = header,
                Body = "{{ request.body }}",
                UseTransformer = true
            };

I tried different examples of using xpath but nothing works. Where is the mistake, i could not find.

Please suggest..

Originally created by @msutar021 on GitHub (Jul 9, 2021). I want to create a mock response for SOAP service which accepts XML request . I'm using XPathMatcher for request matching. Request is matching is done successfully. I need to read one attribute of request.body and create the response using that. My XML request is like this. I need the token id to create the response body. [Request.txt](https://github.com/WireMock-Net/WireMock.Net/files/6788936/Request.txt) ``` xml <?xml version='1.0' standalone='no'?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.Test.nl/XMLHeader/10" xmlns:req="http://www.Test.nl/Betalen/COU/Services/RdplDbTknLystByOvkLyst/8/Req"> <soapenv:Header> <ns:TestHeader> <ns:HeaderVersion>10</ns:HeaderVersion> <ns:MessageId>MsgId10</ns:MessageId> <ns:ServiceRequestorDomain>Betalen</ns:ServiceRequestorDomain> <ns:ServiceRequestorId>CRM</ns:ServiceRequestorId> <ns:ServiceProviderDomain>COU</ns:ServiceProviderDomain> <ns:ServiceId>RdplDbTknLystByOvkLyst</ns:ServiceId> <ns:ServiceVersion>8</ns:ServiceVersion> <ns:FaultIndication>N</ns:FaultIndication> <ns:MessageTimestamp>?</ns:MessageTimestamp> </ns:TestHeader> </soapenv:Header> <soapenv:Body> <req:RdplDbTknLystByOvkLyst_REQ> <req:AanleveraarCode>CRM</req:AanleveraarCode> <!--Optional:--> <req:AanleveraarDetail>CRMi</req:AanleveraarDetail> <req:BerichtId>BerId</req:BerichtId> <req:BerichtType>RdplDbTknLystByOvkLyst</req:BerichtType> <!--Optional:--> <req:OpgenomenBedragenGewenstIndicatie>N</req:OpgenomenBedragenGewenstIndicatie> <req:TokenIdLijst> <!--1 to 10 repetitions:--> <req:TokenId>0000083256</req:TokenId> <req:TokenId>0000083259</req:TokenId> </req:TokenIdLijst> </req:RdplDbTknLystByOvkLyst_REQ> </soapenv:Body> </soapenv:Envelope> ``` I am using the below matcher models while creating mapping for the request. ``` c# var matcherModel = new MatcherModel { Name = "WildcardMatcher", Pattern = "/xpathsoap", RejectOnMatch = false, IgnoreCase = true }; var bodymatcherModel = new MatcherModel { Name = "XPathMatcher", Pattern = "//*[local-name() = 'RdplDbTknLystByOvkLyst_REQ']", IgnoreCase = true }; var matcherModels = new[] { matcherModel }; var pathModel = new PathModel { Matchers = matcherModels }; var bodymatcherModels = new[] { bodymatcherModel }; var BodyModel = new BodyModel { Matchers = bodymatcherModels }; var requestModel = new RequestModel { Path = pathModel, Body = BodyModel }; return requestModel; ``` And response model in mapping ``` c# IDictionary<string, object> header = new Dictionary<string, object>(); header.Add("Content-Type", "Application/xml"); return new ResponseModel() { StatusCode = 201, Headers = header, Body = "{{ XPath.SelectNodes request.body \"//TokenIdLijst\"}}", UseTransformer = true }; ``` The above code returns response as blank. I get whole request in the response.body if i use the below code without xpath ``` c# var response = new ResponseModel() { StatusCode = 201, Headers = header, Body = "{{ request.body }}", UseTransformer = true }; ``` I tried different examples of using xpath but nothing works. Where is the mistake, i could not find. Please suggest..
adam added the bug label 2025-12-29 08:26:45 +01:00
adam closed this issue 2025-12-29 08:26:45 +01:00
Author
Owner

@StefH commented on GitHub (Jul 9, 2021):

@msutar021

Take a look at:
https://stackoverflow.com/questions/56933756/how-to-create-xpath-for-soap-header-and-body

In your case, I think you can use:

//*[local-name()='TokenIdLijst']
@StefH commented on GitHub (Jul 9, 2021): @msutar021 Take a look at: https://stackoverflow.com/questions/56933756/how-to-create-xpath-for-soap-header-and-body In your case, I think you can use: ``` xml //*[local-name()='TokenIdLijst'] ```
Author
Owner

@msutar021 commented on GitHub (Jul 9, 2021):

I have tried this, it is not working; Response is null.

@msutar021 commented on GitHub (Jul 9, 2021): I have tried this, it is not working; Response is null.
Author
Owner

@StefH commented on GitHub (Jul 10, 2021):

The issue was related to <?xml version='1.0' standalone='no'?>

A new version will be released shorty.

See also
https://github.com/WireMock-Net/WireMock.Net/blob/master/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithHandlebarsXPathTests.cs#L124

@StefH commented on GitHub (Jul 10, 2021): The issue was related to `<?xml version='1.0' standalone='no'?>` A new version will be released shorty. See also https://github.com/WireMock-Net/WireMock.Net/blob/master/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithHandlebarsXPathTests.cs#L124
Author
Owner

@msutar021 commented on GitHub (Jul 12, 2021):

Thank you so much for the reply. Please keep posted on the new version.

@msutar021 commented on GitHub (Jul 12, 2021): Thank you so much for the reply. Please keep posted on the new version.
Author
Owner

@StefH commented on GitHub (Jul 12, 2021):

It was released as version 1.4.18
https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md

@StefH commented on GitHub (Jul 12, 2021): It was released as version 1.4.18 https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md
Author
Owner

@msutar021 commented on GitHub (Jul 14, 2021):

Thank you. Its working now.

@msutar021 commented on GitHub (Jul 14, 2021): Thank you. Its working now.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#351