尝试将 Wiremock 用作虚拟化 SOAP 服务的工具。
请求映射条件如下所示:-
映射标准:
{
"request":{
"method":"POST",
"urlPattern":"/myServices/mycontent.asgx",
"headers":{
"SOAPAction":{
"contains":"#SearchMyContent"
}
},
"bodyPatterns":[{
**"matchesXPath":"//data:MyContentItemCode[contains(text(), 'SD_12345')] and //MyContentItemCode[contains(text(), 'SD_22222')]",**
"xPathNamespaces":{
"SOAP-ENV": "http://schemas.xmlsoap.org/soap/envelope/",
"data":"http://www.ins.com/insi/1.0/insi-data",
"msg":"http://www.ins.com/insi/1.0/insi-messaging",
"nc":"http://www.ins.com/insi/1.0/insi-non-compliant",
"soapenv":"http://schemas.xmlsoap.org/soap/envelope/",
"srvc":"http://www.ins.com/insi/1.0/insi-services"
}
}]
},
"response":{
"status":200,
"headers":{
"Content-Type":"text/xml;charset=utf-8"
},
"body":"encoded_XML_body"
}
}
出于安全原因,我无法在此处发布整个 SOAP 服务请求,但下面是来自 SOAP 服务的一小部分,必须与映射条件中的 xpath 匹配
<srvc:MyContentItemCodeList>
<data:MyContentItemCode>SD_12345</data:MyContentItemCode>
<data:MyContentItemCode>SD_22222</data:MyContentItemCode>
</srvc:MyContentItemCodeList>
如您所见,我正在尝试匹配映射条件中的两个">data:MyContentItemCode"标签。但是,wiremock 不识别/支持这一点。这可能是因为 xpath 返回了一个布尔值。我的问题是 - 有没有办法匹配 Wiremock 中的布尔值。
我没有在这里的 Wiremock 文档中找到示例:- http://wiremock.org/docs/request-matching/
当我将映射发布到 wiremock 服务器时,它确实成功发布,但是当我尝试命中 wiremock 服务器时,我没有返回我的虚拟化响应(即不考虑请求匹配(
任何帮助/指示将不胜感激。
您面临的问题是您需要将元素/标签返回给匹配器。这可以通过使用根标记来完成。在这个例子中,我使用了你的例子暗示存在的肥皂信封标签。
仅返回根元素的机制是计算与条件匹配的元素数。如果两者都为 true,则还返回根元素。下面的示例就是这样做的。
映射.json
{
"request":{
"method":"POST",
"urlPattern":"/dtag",
"bodyPatterns":[{
"matchesXPath":"/SOAP-ENV:Envelope[count(//data:MyContentItemCode[contains(text(), 'SD_12345')])=1 and count(//data:MyContentItemCode[contains(text(), 'SD_22222')] )=1]",
"xPathNamespaces":{
"SOAP-ENV": "http://schemas.xmlsoap.org/soap/envelope/",
"data":"http://www.ins.com/insi/1.0/insi-data",
"srvc":"http://www.ins.com/insi/1.0/insi-services"
}
}]
},
"response":{
"status":200,
"headers":{
"Content-Type":"text/xml;charset=utf-8"
},
"body":"encoded_XML_body"
}
}
以下 XML 通过 POST 请求发送到以下 URL。由于 WireMock 对命名空间非常挑剔,请确保您具有与请求中显示的标签关联的正确命名空间。
请求 http://localhost/dtag
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"
xmlns:data="http://www.ins.com/insi/1.0/insi-data"
xmlns:srvc="http://www.ins.com/insi/1.0/insi-services">
<srvc:MyContentItemCodeList >
<data:MyContentItemCode>SD_12345</data:MyContentItemCode>
<data:MyContentItemCode>SD_22222</data:MyContentItemCode>
</srvc:MyContentItemCodeList>
</soap:Envelope>