Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
msutar021 opened this issue Jul 9, 2021 · 6 comments
Labels

Comments

@msutar021
Copy link

msutar021 commented 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:https://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http:https://www.Test.nl/XMLHeader/10" xmlns:req="http:https://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..

@StefH
Copy link
Collaborator

StefH commented 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']

@msutar021
Copy link
Author

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

@StefH
Copy link
Collaborator

StefH commented 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 StefH closed this as completed Jul 10, 2021
@StefH StefH added bug and removed question labels Jul 10, 2021
@msutar021
Copy link
Author

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

@StefH
Copy link
Collaborator

StefH commented Jul 12, 2021

@msutar021
Copy link
Author

Thank you. Its working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants