【问题标题】:Matching data in JsonPath with wiremock使用wiremock匹配JsonPath中的数据
【发布时间】:2015-02-23 10:35:59
【问题描述】:

我正在尝试为我的登录过程创建模拟。我使用带有几个字段和登录对象(带有登录名、密码等)的 POST 方法 为此,我正在使用 JsonPath。代码如下:

{
"request": {
        "method": "POST",
        "url": "/login",
        "bodyPatterns" : [
                {"matchesJsonPath" : "$.method"},
                {"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},
                {"matchesJsonPath" : "$.params.login"},
                {"matchesJsonPath" : "$.params.password"}
         ]
    },
    "response": {
            "status": 200,
            "bodyFileName": "login.json"
    }
}

我正在检查 clientVersion,因为它与示例类似。

我的问题是,给定 POST JSON:

{
    "method": "login",
    "params": {
        "clientVersion": "1",
        "login": "test@test.com",
        "password": "681819535da188b6ef2"
    }
}

我收到 404。 但是,当我改变时

{"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},

正常

{"matchesJsonPath" : "$.params.clientVersion"},

一切正常。

那么-如果给定字段等于某个值,如何使用matchesJsonPath检查wiremock内部? 在我的情况下,如何将其应用于根字段之类的方法? 当我们这样做时 - 我在检查值是否不为空时遇到了类似的问题。我尝试应用正则表达式等 - 没有运气。

【问题讨论】:

    标签: json jsonpath wiremock


    【解决方案1】:

    它在我的情况下工作:

    有线模拟:

    "request": {
    "urlPathPattern": "/api/authins-portail-rs/authins/inscription/infosperso",
    "bodyPatterns" : [
      {"matchesJsonPath" : "$[?(@.nir == '123456789')]"},
      {"matchesJsonPath" : "$[?(@.nomPatronyme == 'aubert')]"},
      {"matchesJsonPath" : "$[?(@.prenoms == 'christian')]"},
      {"matchesJsonPath" : "$[?(@.dateNaissance == '01/09/1952')]"}
    
    ],
    "method": "POST"
    

    }

    json:

    {
        "nir": "123456789",
        "nomPatronyme": "aubert",
        "prenoms": "christian",
        "dateNaissance": "01/09/1952"
    }
    

    【讨论】:

      【解决方案2】:

      以下对我有用。

      "matchesJsonPath" : "$.rootItem.itemA[0].item..[?(@.fieldName=='file')]"

      json:

      {
          "rootItem" :  {
                "itemA" : [
                    {
                       "item" : {
                           "fieldName" : "file",
                           "name" : "test"
                       }
                    }
                ]
          }
      }
      

      线模

      {
        "request" : {
          "urlPattern" : "/testjsonpath",
          "method" : "POST",
          "bodyPatterns" : [ {
            "matchesJsonPath" : "$.rootItem.itemA[0].item..[?(@.fieldName=='file')]"
          } ]
        },
        "response" : {
          "status" : 200,
          "body" : "{\"result\": \"success\"}",
          "headers" : {
            "Content-Type" : "application/json"
          }
        }
      }
      

      【讨论】:

        【解决方案3】:

        更新 Wiremock。它应该适用于 >= 2.0.0-beta 的较新版本。它的JsonPath 依赖已经过时了(GitHub #261)。

        使用双点运算符在语义上是不一样的,因为过滤器也会匹配树更深处的同名元素。

        【讨论】:

          【解决方案4】:

          尝试使用双点运算符(递归)

          $..params[?(@.clientVersion == "1")]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-22
            • 1970-01-01
            • 1970-01-01
            • 2018-09-08
            • 1970-01-01
            相关资源
            最近更新 更多