我正在使用Wiremock进行测试,我面临以下问题。
有2种情况
<Body>
<ElementA>Element A</ElementA>
<ElementB>123<ElementB>
</Body>
这里我们只需要检查B元素的值是否为123。
<Body>
<ElementA flag="true">Element A</ElementA>
<ElementB>123<ElementB>
</Body>
这里我们需要检查元素B的值应该是123,标志的值也应该是true。
现在我想让我的 wiremock 在这两种情况下返回不同的响应,我的 wiremock 配置文件看起来像这样。
{
"request": {
"url": "/",
"method": "POST",
"bodyPatterns": [
{
"matchesXPath": {
"expression": "//ElementB/text()",
"contains": "123"
}
},{
"absent": {
"expression": "//ElementA[@flag=true]"
}
}
]
},
"response": {
"status": 200,
"bodyFileName": "someFile.xml",
"headers": {
"Content-Type": "text/xml; charset=utf-8"
}
}
}
但这里只把absent当作true,其他元素并没有出现。
"bodyPatterns" : [ {
"matchesXPath" : {
"expression" : "//ElementA/text()",
"contains" : "123"
}
}, {
"absent" : true
} ]
{
"request": {
"url": "/",
"method": "POST",
"bodyPatterns": [
{
"matchesXPath": {
"expression": "//ElementB[text()='123']"
},
"matchesXPath": {
"expression": "//ElementA[@flag]",
"contains": "true"
}
}
]
},
"response": {
"status": 200,
"bodyFileName": "someotherFile.xml",
"headers": {
"Content-Type": "text/xml; charset=utf-8"
}
}
}
但不知何故,它不能在情况1中返回响应,而在情况2中,它不关心第一个条件,它只检查标志的值是否为真。
如果有任何帮助,我将感激不尽。
我不相信你需要的是什么?absent
标志,我认为你可以使用优先级来代替,迫使WireMock在转移到更一般的响应之前检查更具体的请求。我认为你可以使用优先级来强制WireMock检查更具体的请求,然后再转到更一般的响应。
我也认为你在检查Case 2中的XPath的方式有错误。我会尝试一些像...
{
"priority": 1,
"request": {
"url": "/",
"method": "POST",
"bodyPatterns": [
{
"matchesXPath": {
"expression": "//ElementB[text()]",
"contains": "123"
},
"matchesXPath": {
"expression": "//ElementA[@flag]",
"contains": "true"
}
}
]
},
"response": {
"status": 200,
"bodyFileName": "someFile.xml",
"headers": {
"Content-Type": "text/xml; charset=utf-8"
}
}
}
看看添加的 "priority": 1
. 我也认为你可以改变 matchesXPath
对象,如果你不需要它们与 regex 模式相匹配,就可以简单地检查这些值。"matchesXPath": "//ElementB[text() = 123]"
{
"priority": 2,
"request": {
"url": "/",
"method": "POST",
"bodyPatterns": [
{
"matchesXPath": {
"expression": "//ElementB[text()]",
"contains": "123"
}
}
]
},
"response": {
"status": 200,
"bodyFileName": "someotherFile.xml",
"headers": {
"Content-Type": "text/xml; charset=utf-8"
}
}
}
因为优先级为1的映射会检查 flag=true
在优先级 2 映射上的匹配不包含具有 flag=true 属性的元素 A。所以,在WireMock检查了优先级1的映射后(需要在 flag=true
),它检查优先级2的映射(只要求ElementB的文本=123)。