微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Wiremock映射json BodyPatterns中,仅发生最后的比较

如何解决在Wiremock映射json BodyPatterns中,仅发生最后的比较

我正在使用wiremock-jre8-standalone-2.27.0 jar模拟API。我的映射json如下:

 {
  "request": {
    "url": "/sampleUrl","method": "POST","bodyPatterns": [
      {
        "matchesJsonPath" : {
          "expression": "$[0].fruit","contains": "apple"
        },"matchesJsonPath" : {
          "expression": "$[0].quantity","contains": "1221"
        },"matchesJsonPath" : {
          "expression": "$[1].fruit","contains": "banana"
        },"matchesJsonPath" : {
          "expression": "$[2].quantity","contains": "2784"
        }
      }
    ]
  },"response": {
    "status": 200,"headers": {
      "Content-Type": "application/json; charset=utf-8"
    },"bodyFileName": "prices.json","delaydistribution": {
      "type": "uniform","lower": 200000,"upper": 500000
    }

可以看出,bodyPatterns内有4个matchsJsonPath,但是每次都只比较最后一个matchsJsonPath($[2].quantity == 2784)。我是否更改了请求正文中的其余内容,例如它使前三个matchesJsonPath失败,并通过邮递员发送了请求,我仍然得到响应。有没有办法让wiremock检查所有条件?

解决方法

问题出在您的bodyPatterns数组上。每个匹配项都必须是数组中自己的JSON对象。您目前将匹配器全部放在一个对象中。

"bodyPatterns": [
    {
        "matchesJsonPath" : {
          "expression": "$[0].fruit","contains": "apple"
        }
    },{
        "matchesJsonPath" : {
          "expression": "$[0].quantity","contains": "1221"
        }    
    },{
        "matchesJsonPath" : {
          "expression": "$[1].fruit","contains": "banana"
        }    
    },{
        "matchesJsonPath" : {
          "expression": "$[2].quantity","contains": "2784"
        }    
    }
]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。