12

我正在尝试为我的登录过程创建模拟。我使用带有几个字段和登录对象(带有登录名、密码等)的 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内部?在我的情况下,如何将其应用于根字段之类的方法?当我们这样做时 - 我在检查值是否不为空时遇到了类似的问题。我试图应用正则表达式等 - 没有运气。

4

4 回答 4

8

它在我的情况下工作:

线模:

"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"

}

杰森:

{
    "nir": "123456789",
    "nomPatronyme": "aubert",
    "prenoms": "christian",
    "dateNaissance": "01/09/1952"
}
于 2018-08-14T08:48:09.237 回答
2

Update Wiremock. It should work with newer versions >= 2.0.0-beta. Its JsonPath dependency was very outdated (GitHub #261).

Using the double dots operator is semantically not the same, as the filter will also match for elements with the same name deeper down the tree.

于 2016-06-17T10:58:26.907 回答
2

以下为我工作。

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

杰森:

{
    "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"
    }
  }
}
于 2015-08-18T09:35:23.987 回答
0

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

$..params[?(@.clientVersion == "1")]
于 2015-02-26T16:08:05.743 回答