首页 > 解决方案 > 如何在wiremock的请求映射中使用主体模式

问题描述

我有两个如下的 xml 请求,只是想得到响应。Response1 如果请求正文包含节点 A、B、C 并且不包含 D。Response2 如果 A、B、C、D 都包含在请求中。

有人可以帮我解决这个问题。

如果有人可以帮助我使用正文模式获得关于请求匹配的好文章,或者正文模式有哪些不同的选项,那也会很有帮助。

提前致谢!!

我试过包含和优先级,但这没有帮助。

要求1:

<TestService>
    <A> test </A>
    <B> testB</B>
    <C> testC </C>
    <XYZ> test </XYZ>
</TestService>

要求2:

<TestService>
    <A> test </A>
    <B> testB</B>
    <C> testC </C>
    <XYZQ> test test</XYZQ>
   <D> testD</D>
</TestService>

标签: regexwiremock

解决方案


在 WireMock 中,它可能会以 2 条规则结束,每种情况一个:

标签存在时的规则:

{
"request": {
 "method": "ANY",
 "url": "/xmltag",
 "bodyPatterns" : [ {
   "matchesXPath" : "/TestService[count(//D) = 1]"
 } ]  
},
"response": {
 "status": 200,
 "body": "<root><tag>D</tag></root>"
}
}

然后是没有 D 标签时的另一个:

{
"request": {
 "method": "ANY",
 "url": "/xmltag",
 "bodyPatterns" : [ {
   "matchesXPath" : "/TestService[count(//D) = 0]"
 } ]  
},
"response": {
 "status": 200,
 "body": "<root><tag>No D found</tag></root>"
}
}

推荐阅读