【问题标题】:How to match JSON in wiremock which has values same for fields but may not be in same order如何在wiremock中匹配字段值相同但顺序可能不同的JSON
【发布时间】:2024-01-17 10:23:01
【问题描述】:

我有请求 JSON 正文 [ {"name" : "Ram"}, {"name" : "Sam"} ]

这是wiremock请求的输入 即使 JSON 具有相同的内容但值的顺序可能不同,我也需要匹配请求。例子, [ {"name" : "Sam"}, {"name" : "Ram"} ]

我使用的方法是.withRequestBody. I tried withequalToJson` 但不起作用。只检查 JSON 内容而不检查顺序的匹配器是什么?

【问题讨论】:

    标签: web-services request matching wiremock


    【解决方案1】:

    这可以使用 JsonPath 解决,它是 bodyPatterns 相等匹配功能的一部分。

    {
      "request" : {
        "urlPathPattern" : "/jpath/.*",
        "method" : "GET",
         "bodyPatterns" : [ {
          "matchesJsonPath" : "$[?(@.name == 'Sam')]"
        } ]   
      },
      "response" : {
        "status" : 200,
        "body" : "Works"
      }
    }
    

    使用JsonPath online evaluator 可以很容易地测试 JsonPath 表达式。有关可能的更多详细信息,请查看here

    【讨论】:

      最近更新 更多