简体   繁体   English

Xpath 验证线模型匹配 XPath 表达式

[英]Xpath validation wiremock matchesXPath expression

I am creating cases with Wiremocks and I am generating a response mock.我正在使用 Wiremocks 创建案例,并且正在生成响应模拟。

I have a XML request like this:我有一个这样的 XML 请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xw="http://example.com">
    <soapenv:Header/>
    <soapenv:Body>
        <xw:gen>
            <xw:input>
                <xw:element1>0100</xw:element1>
                <xw:element2>741</xw:element2>
                <xw:element3>JAVA</xw:element3>
                <xw:element4>123</xw:element4>
            </xw:input>
            <xw:global>
                <xw:obj1>
                    <xw:attr1>john</xw:attr1>
                    <xw:attr2>doe</xw:attr2>
                </xw:obj1>
            </xw:global>
        </xw:gen>
    </soapenv:Body>
</soapenv:Envelope>

I only have to validate that xw:input/xw:element1 = 0100 , xw:input/xw:element2 = 741 and I need that the xw:global node has anything.我只需要验证xw:input/xw:element1 = 0100 , xw:input/xw:element2 = 741并且我需要xw:global节点有任何东西。 The only condition for xw:global is not empty . xw:global的唯一条件不是 empty (This node can be <xw:global></xw:global> ). (这个节点可以是<xw:global></xw:global> )。

This is my mock in json:这是我在 json 中的模拟:

{
    "request" : {
        "url" : "/myweb/myrequest.asmx",
        "headers": {
            "SOAPAction": {
                "equalTo": "\"http://example.com/gen\""
            }
        },
        "bodyPatterns" : [ {
            "matchesXPath": "//xw:input[xw:element1=\"0100\" and xw:element2=\"741\" ]",
            "xPathNamespaces" : {
                "xw" : "http://example.com"
            }
        }]
    },
    "response" : {
        "status" : 200,
        "headers": {
            "Content-Type": "text/xml;charset=UTF-8"
        },
        "body" : "<Abody>"
    }
}

The question is: how I can validate that the node xw:global is not empty or is not null?问题是:如何验证节点xw:global不为空或不为空?

I tried with this matchesXPath but I didn't have lucky:我试过这个matchesXPath,但我没有幸运:

"matchesXPath": "//xw:input[xw:element1=\\"0100\\" and xw:element2=\\"741\\" ] and count(//xw:global) > 0" . "matchesXPath": "//xw:input[xw:element1=\\"0100\\" and xw:element2=\\"741\\" ] and count(//xw:global) > 0"

Thanks.谢谢。

I'm not familiar with wiremock, but you may want to try the following XPath :我不熟悉wiremock,但您可能想尝试以下 XPath :

"//xw:gen[xw:input[xw:element1=\"0100\" and xw:element2=\"741\"]][xw:global/*]"

The XPath above check if there is any xw:gen that :上面的 XPath 检查是否有任何xw:gen

  • [xw:input[xw:element1=\\"0100\\" and xw:element2=\\"741\\"]] : have child element xw:input with the criteria you mentioned [xw:input[xw:element1=\\"0100\\" and xw:element2=\\"741\\"]] :具有您提到的条件的子元素xw:input
  • [xw:global/*] : and have child element xw:global containing, at least, one other element of any name [xw:global/*] : 并且有子元素xw:global至少包含一个其他任何名称的元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM