Matching data in JsonPath with wiremock
I'm trying to create mocks for my login procedure. I use POST method with a couple of fields and login object (with login, password, etc.) For that I'm using JsonPath. Code below:
{
"request": {
"method": "POST",
"url": "/login",
"bodyPatterns" : [
{"matchesJsonPath" : "$.method"},
{"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},
{"matchesJsonPath" : "$.params.login"},
{"matchesJsonPath" : "$.params.password"}
]
},
"response": {
"status": 200,
"bodyFileName": "login.json"
}
}
I'm checking the clientVersion because it's similar to the examples.
My problem is, that with te given POST JSON:
{
"method": "login",
"params": {
"clientVersion": "1",
"login": "test@test.com",
"password": "681819535da188b6ef2"
}
}
I receive 404. However, when I change
{"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},
to normal
{"matchesJsonPath" : "$.params.clientVersion"},
everything works just fine.
So - how to check inside wiremock, using matchesJsonPath if given field equals some value? How to apply it to the root field like method in my case? And while we're at it - I had similar problems with checking if the value is not null. I tried to apply regular expressions and such - no luck.
Following worked for me.
"matchesJsonPath" : "$.rootItem.itemA[0].item..[?(@.fieldName=='file')]"
Json :
{
"rootItem" : {
"itemA" : [
{
"item" : {
"fieldName" : "file",
"name" : "test"
}
}
]
}
}
Wiremock
{
"request" : {
"urlPattern" : "/testjsonpath",
"method" : "POST",
"bodyPatterns" : [ {
"matchesJsonPath" : "$.rootItem.itemA[0].item..[?(@.fieldName=='file')]"
} ]
},
"response" : {
"status" : 200,
"body" : "{"result": "success"}",
"headers" : {
"Content-Type" : "application/json"
}
}
}
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.
尝试使用双点运算符(递归)$ .. params [?(@。clientVersion ==“1”)]
链接地址: http://www.djcxy.com/p/83874.html