【问题标题】:WireMock. How to write proper JSONPath for array线模。如何为数组编写正确的 JSONPath
【发布时间】:2022-01-09 22:53:02
【问题描述】:

我有如下请求:

"RequestBody": {
        "numbers": ["20030013", "10010701", "10006402"]
    }

我正在尝试使用 Wiremock 来捕获上述请求,例如:

{
"request": {
    "urlPattern": "<my_url_com>",
    "method": "POST",
    "bodyPatterns": [
        {
            "matchesJsonPath": "$.RequestBody[?(@.numbers == ['10010701'])]"
        }
    ]
},
"response": {
    "status": 200,
    "jsonBody": {
        "ResponseBody": {
            ...
        }
    }
}

} 因为我只对包含“10010701”的请求感兴趣。

但它只有在数字只包含一个值时才有效。 那么,我怎样才能捕获这个具有多个值的请求呢?

【问题讨论】:

    标签: jsonpath wiremock


    【解决方案1】:

    WireMock 使用 Goessener JSONPath,因此我们应该能够在 ?() 过滤器中使用 JS。然后我们可以使用.indexOf() 函数来检查一个值是否存在于数组中。如果是,它将返回项目的索引,如果不是,它将返回 -1。通过检查该值不是 -1,我们可以假设该值存在于数组中。

    Info taken from here

    "matchesJsonPath": "$.RequestBody[?[@.numbers.indexOf('10010701') != -1)]"
    

    【讨论】:

    • 感谢胶水。我设法用$.RequestBody.numbers[?(@.indexOf('10010701') != -1)] 找到了'10010701',并用jsonpath.herokuapp.com 尝试了它,但WireMock 没有用这个表达式缓存请求。有什么想法吗?
    • jsonpath.herokuapp.com 使用不支持indexOfJayway JsonPath
    • @AkshayG at jsonpath.herokuapp.com 您可以在 Jayway、Gatling、Nebhale 和 Goessner 符号之间进行选择
    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    相关资源
    最近更新 更多