我对再次检查Wiremock有一些疑问
我想检查JSON所有字段
{"petType": 1, "wildLevel": 40,"petStatus": 1}
如果我发送无效字段,例如
{"petType": 1, "wildLevel": 40,"patStatus": 1}
{"catType": 1, "wildLevel": 40,"patStatus": 1}
{"petType": 1, "wildLevelxx": 40,"patStatus": 1}
,它应该显示无效请求。
这个映射JSON我可以写
{
"mappings":[
{
"priority":1,
"request":{
"method":"PATCH",
"urlPath":"/pet/status",
"bodyPatterns":[
{
"or":[
]
}
]
},
"response":{
"status":400,
"transformers":[
"response-template"
],
"bodyFileName":"invalid-request.json",
"headers":{
"Content-Type":"application/json"
}
}
},
{
"priority":2,
"request":{
"method":"PATCH",
"urlPath":"/pet/status",
"bodyPatterns":[
{
"and":[
{
"matchesJsonPath":"$[?(@.petType == '404040')]"
},
{
"matchesJsonPath":"$.wildLevel "
},
{
"matchesJsonPath":"$.petStatus "
}
]
}
]
},
"response":{
"status":200,
"transformers":[
"response-template"
],
"bodyFileName":"find-success.json",
"headers":{
"Content-Type":"application/json"
}
}
},
{
"priority":3,
"request":{
"method":"PATCH",
"urlPath":"/pet/status",
"bodyPatterns":[
{
"and":[
{
"matchesJsonPath":"$.petType "
},
{
"matchesJsonPath":"$.wildLevel "
},
{
"matchesJsonPath":"$.petStatus "
}
]
}
]
},
"response":{
"status":403,
"transformers":[
"response-template"
],
"bodyFileName":"data-not-found.json",
"headers":{
"Content-Type":"application/json"
}
}
}
]
}
现在我不知道怎么解决,请帮帮我
如果您的请求正文只包含这三个字段,您可以使用以下组合:
equalToJson
ignoreExtraElements = false
json-Unit Placeholder
所有这些信息都可以在本页找到。
解决方案看起来像…
...
"bodyPatterns": [
{
"equalToJson": "{ "petType": "${json-unit.any-number}", "wildLevel": "${json-unit.any-number}", "petStatus": "${json-unit.any-number}" }",
"ignoreExtraElements": false
}
]
...
我们在这里检查请求体是否包含所有三个字段(petType, wildLevel和petStatus),并且只包含这些字段,并且所有这些字段的值都是数字。如果不满足这些条件,则不会将其视为匹配。我将这作为高优先级(1),和有一个更一般的"不matched"对于不匹配此映射的情况的映射,您将返回错误。