WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.39k stars 207 forks source link

Support for xml namespaces in XPathMatcher #1005

Closed cal-schleupen closed 1 year ago

cal-schleupen commented 1 year ago

We have round about 4800 WCF Services in our landscape, thus this faker is really helpful. In order to write more easy request body matchers I would like to write more easy xpath expressions using the xpath matcher as follows:

{
    "Title": "wcf test",
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/MyService.svc"
                }
            ]
        },
        "Methods": [
            "post"
        ],
                "Headers": [
                    {
                        "Name": "Content-Type",
                        "Matchers": [
                            {
                                "Name": "WildcardMatcher",
                                "Pattern": "text/xml; charset=utf-8",
                                "IgnoreCase": true
                            }
                        ],
                        "IgnoreCase": true
                    },
                    {
                        "Name": "SOAPAction",
                        "Matchers": [
                            {
                                "Name": "WildcardMatcher",
                                "Pattern": "*urn://MyService/Query*",
                                "IgnoreCase": true
                            }
                        ],
                        "IgnoreCase": true
                    }
                ],
        "Body": {
            "Matcher": {
                "Name": "XPathMatcher",
                "Pattern": "/s:Envelope/s:Body/q:QueryRequest",
                "XmlNamespaceMap": [
                    { 
                        "Prefix": "s",
                        "Uri": "http://schemas.xmlsoap.org/soap/envelope/"
                    },
                    { 
                        "Prefix": "i",
                        "Uri": "http://www.w3.org/2001/XMLSchema-instance"
                    },
                    { 
                        "Prefix": "q",
                        "Uri": "urn://Schleupen.AS.MT.BIB.Buecher.BuchQueryService_3.1"
                    }
                ]
            }
        }
    },
    "Response": {
        "StatusCode": 200,
        "Body": "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><QueryResponse xmlns=\"urn://MyService\"></QueryResponse></s:Body></s:Envelope>",
        "Headers": {
            "Content-Type": "text/xml"
        }
    }
}

This implementation handles this so that

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header></s:Header><s:Body><QueryRequest xmlns="urn://MyService"><MaxResults i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/><Restriction i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/><SearchMode i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/></QueryRequest></s:Body></s:Envelope> 

matches for example.

Yes, I can use 'local-name()' etc. but I think this way it is more easy.

StefH commented 1 year ago

@cal-schleupen Thank you for this addition.

I did add some review comments, can you please take a look?

cal-schleupen commented 1 year ago

Hi Stef,

thanks a lot for reviewing it so fast! I'm really impressed and I like WireMock.NET a lot. It is also well done under the hood. This weekend I am a little bit busy with my family, but I'll fix it not later than on Monday. Hopefully today ;-)

I am really happy that you could take a look at my pull request!

Kind regards, Carsten

cal-schleupen commented 1 year ago

Maybe we got it now ;-)

StefH commented 1 year ago

@cal-schleupen Thank you.

I'll merge this and create a new NuGet.