当发送字段与 wiremock 中的期望 JSON 不匹配时如何检查 JSON 字段?
How to check JSON field when send field not match with expect JSON in wiremock?
我有一些关于再次检查 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
关于这些的所有信息都可以 found on this page。
解决方案看起来像...
...
"bodyPatterns": [
{
"equalToJson": "{ \"petType\": \"${json-unit.any-number}\", \"wildLevel\": \"${json-unit.any-number}\", \"petStatus\": \"${json-unit.any-number}\" }",
"ignoreExtraElements": false
}
]
...
我们在这里检查请求正文是否包含所有这三个字段(petType、wildLevel 和 petStatus),并且只包含这些字段,并且所有这些字段的值都是数字。如果不满足这些条件,则这将不被视为匹配。我会将其设置为更高优先级 (1),并针对此映射不匹配的情况设置更通用的“不匹配”映射,您 return 错误。
我有一些关于再次检查 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
关于这些的所有信息都可以 found on this page。
解决方案看起来像...
...
"bodyPatterns": [
{
"equalToJson": "{ \"petType\": \"${json-unit.any-number}\", \"wildLevel\": \"${json-unit.any-number}\", \"petStatus\": \"${json-unit.any-number}\" }",
"ignoreExtraElements": false
}
]
...
我们在这里检查请求正文是否包含所有这三个字段(petType、wildLevel 和 petStatus),并且只包含这些字段,并且所有这些字段的值都是数字。如果不满足这些条件,则这将不被视为匹配。我会将其设置为更高优先级 (1),并针对此映射不匹配的情况设置更通用的“不匹配”映射,您 return 错误。