使用wiremock在JsonPath中匹配数据

a_d*_*zik 8 json jsonpath wiremock

我正在尝试为我的登录程序创建模拟.我使用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"
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我的问题是,用给定的POST JSON:

{
    "method": "login",
    "params": {
        "clientVersion": "1",
        "login": "test@test.com",
        "password": "681819535da188b6ef2"
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到了404.但是,当我改变时

{"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},
Run Code Online (Sandbox Code Playgroud)

正常

{"matchesJsonPath" : "$.params.clientVersion"},
Run Code Online (Sandbox Code Playgroud)

一切正常.

那么 - 如果给定的字段等于某个值,如何使用matchesJsonPath检查内部的wiremock?在我的情况下,如何将它应用于根域,就像方法一样?虽然我们在这里 - 我在检查值是否为空时遇到了类似的问题.我试图应用正则表达式等 - 没有运气.

小智 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"
Run Code Online (Sandbox Code Playgroud)

}

杰森:

{
    "nir": "123456789",
    "nomPatronyme": "aubert",
    "prenoms": "christian",
    "dateNaissance": "01/09/1952"
}
Run Code Online (Sandbox Code Playgroud)


Kra*_*agh 1

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

$..params[?(@.clientVersion == "1")]
Run Code Online (Sandbox Code Playgroud)