json - 使用wiremock匹配JsonPath中的数据

标签 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"
    }
}

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

我的问题是,给定 POST JSON:
{
    "method": "login",
    "params": {
        "clientVersion": "1",
        "login": "test@test.com",
        "password": "681819535da188b6ef2"
    }
}

我收到 404。
然而,当我改变
{"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},

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

一切正常。

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

最佳答案

它在我的情况下工作:

线模:

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

关于json - 使用wiremock匹配JsonPath中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28671753/

相关文章:

java - 尝试调用 Wiremock Stub 时连接被拒绝

java - 使用 Google 的 Gson 进行严格的 JSON 解析?

java - 检索数据只给我数据库的第一行,我只想将最后一行插入数据库

amazon-web-services - AWS 步骤函数和可选参数

docker - 为什么docker运行时说Unknown操作数而同一shell脚本在ubuntu机器中完美运行

java - JsonPath DocumentContext 更新而不更新原始对象

spring-boot - 带 Spring Boot JUnit 5 测试的 Wiremock : Address in Use after test run

json - 如何迭代此Go界面

Python 突然不在 MAC OS X 上读取 json 文件

java - Wiremock 模拟代理服务器运行