在 wiremock 映射 json BodyPatterns 中只有最后一次比较发生

In wiremock mapping json BodyPatterns only the last comparison is happening

我正在使用 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个matchesJsonPath但是每次只比较最后一个matchesJsonPath($[2].quantity == 2784)。我是否更改了请求正文中的其余内容,例如前三个 matchesJsonPath 失败并通过 Postman 发送请求我仍然得到响应。有没有办法让 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"
        }    
    }
]