2017-10-13 75 views
1

我正在使用Wiremocks创建案例,并且正在生成响应模拟。Xpath验证wiremock matchesXPath表达式

我有一个这样的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> 

我只需要验证XW:输入/ XW:元素1 = 0100XW:输入/ XW:在element2 = 741和我需要那个节点有什么东西。 xw:global的唯一条件不是空的。 (该节点可以是<xw:global></xw:global>)。

这是我在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>" 
    } 
} 

的问题是:我怎么能验证该节点XW:全球不为空或不为空?

我试着用这个matchesXPath,但我没有运气:

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

谢谢。

+0

您是否在存根定义中指定了命名空间?如果不是,那么你不能在XPath表达式中使用它们。 – Tom

+0

是的。我指定了命名空间。谢谢。 – dani77

回答

1

我不熟悉wiremock,但你可能想尝试以下XPath:

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

上述检查中的XPath是否有任何xw:gen说:

  • [xw:input[xw:element1=\"0100\" and xw:element2=\"741\"]]:有孩子元素xw:input与您提到的标准
  • [xw:global/*]:并且子元素xw:global包含至少一个其他元素o f任何名称
+0

Hi @ har07。在我的情况下,节点xw:global可以是,我使用了这种验证:'[count(xw:globalObj)> 0]'。但你的回应非常有用。谢谢!! – dani77