XPath请求与WireMock.net匹配

时间:2019-10-15 11:57:34

标签: c# xml xpath wiremock

当请求符合以下特征时,我想从服务中发送模拟响应:

  • URL与 / BATConnectWS / services / CoverApplication
  • 匹配
  • HTTP方法与POST
  • 匹配
  • XPath匹配器必须匹配5000

代码设置:

var server = FluentMockServer.Start(new FluentMockServerSettings
{
    Urls = new[] { "http://+:8099" },
    StartAdminInterface = true,
    Logger = new WireMockConsoleLogger()
});

server
    .Given(Request.Create().WithPath("/*")).AtPriority(10)
    .RespondWith(Response.Create()
        .WithProxy("https://TheRealService.com/"));

server
    .Given(Request.Create().WithPath("/BATConnectWS/services/CoverApplication").UsingPost()
    .WithBody(new XPathMatcher(@"//applyForCreditLimit/application/RequestedAmount/text() = 5000")))
    .AtPriority(1)
    .RespondWith(Response.Create()
    .WithHeader("Content-Type", "text/xml; charset=utf-8")
    .WithCallback(req =>
    {
        return CoverApplicationResponseBuilder.CreateResponse(req);
    }));

请求消息:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <soap:Header>
        <wsa:Action>urn:applyForCreditLimit</wsa:Action>
        <wsa:MessageID>urn:uuid:6da4a592-90a0-4623-8c71-1e685cbdac33</wsa:MessageID>
        <wsa:ReplyTo>
            <wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
        </wsa:ReplyTo>
        <wsa:To>http://localhost:58070/BATConnectWS/services/CoverApplication</wsa:To>
        <wsse:Security soap:mustUnderstand="1">
            <wsu:Timestamp wsu:Id="Timestamp-6befddc7-4e4f-4a76-a203-49b729bd483a">
                <wsu:Created>2019-10-15T08:22:37Z</wsu:Created>
                <wsu:Expires>2019-10-15T08:27:37Z</wsu:Expires>
            </wsu:Timestamp>
            <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-x-x-x-x-x">
                <wsse:Username>xxx</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxx</wsse:Password>
                <wsse:Nonce>xxx</wsse:Nonce>
                <wsu:Created>2019-10-15T08:22:37Z</wsu:Created>
            </wsse:UsernameToken>
        </wsse:Security>
    </soap:Header>
    <soap:Body>
        <applyForCreditLimit xmlns="http://atradius.com/connect/_2007_08/">
            <application>
                <CustomerId xmlns="">1234</CustomerId>
                <PolicyNr policyTypeIdentifier="NON_LEG" xmlns="">
                    <Id>5678</Id>
                </PolicyNr>
                <ExternalCoverId xmlns="">9101112</ExternalCoverId>
                <CustomerReference xmlns="">areference</CustomerReference>
                <Buyer registeredOffice="SYMPH" xmlns="">
                    <id xmlns="http://atradius.com/organisation/_2007_08/type/">13141516</id>
                    <countryTypeIdentifier xmlns="http://atradius.com/organisation/_2007_08/type/">AUT</countryTypeIdentifier>
                </Buyer>
                <RequestedAmount xmlns="">5000</RequestedAmount>
                <CurrencyCode xmlns="">EUR</CurrencyCode>
            </application>
        </applyForCreditLimit>
    </soap:Body>
</soap:Envelope>

没有,将发送XPathMatcher模拟响应。
使用XPatchMatcher会调用(传递)真实服务,因为内容上没有 WithBody 匹配。

RequestedAmount元素中,XPath查询与5000匹配的内容应该是什么样的?

1 个答案:

答案 0 :(得分:0)

这可能与使用的命名空间有关?

如果您只对肥皂消息中是否存在任何值为5000的RequestedAmount元素感兴趣,则可以使用以下方式:

@"//RequestedAmount/text() = 5000"