我正在尝试将Wiremock配置为从文本文件返回相同的响应,如果请求正文包含此内容或类似内容,这是我的单个请求的工作文件:
{
"priority": 1,
"request": {
"method": "POST",
"urlPath": "/v1/retrieve-vehicle",
"bodyPatterns" : [ {
"contains" : "\"vehicleIdentifier\":\"EROR-103\""
} ]
},
"response": {
"status": 404,
"bodyFileName": "JsonFiles/NoVinFound-Error103.json",
"headers": {
"Content-Type": "application/json;charset=UTF-8"
}
}
}
我尝试了bodyPatterns属性的一些组合(受文档启发),但无济于事。
"bodyPatterns" : [ {
"contains" : "\"vehicleIdentifier\":\"EROR-103\"",
"contains" : "\"vehicleIdentifier\":\"EROR-103A\""
} ]
"bodyPatterns" : [ {
"contains" : "\"vehicleIdentifier\":\"EROR-103\""
}, {
"contains" : "\"vehicleIdentifier\":\"EROR-103A\""
} ]
"multipartPatterns" : [ {
"matchingType" : "ANY",
"bodyPatterns" : [ {
"contains" : "\"vehicleIdentifier\":\"EROR-103\"",
"contains" : "\"vehicleIdentifier\":\"EROR-103A\""
} ]
} ]
"multipartPatterns" : [ {
"matchingType" : "ANY",
"bodyPatterns" : [ {
"contains" : "\"vehicleIdentifier\":\"EROR-103\""
}, {
"contains" : "\"vehicleIdentifier\":\"EROR-103A\""
} ]
} ]
请告知我我做错了。谢谢!
我的一位同事找到了解决方案。我们需要一个“映射标签”,然后如下所示列出完整的JSON对象。
{
"mappings": [
{
"priority": 1,
"request": {
"method": "POST",
"urlPath": "/v1/retrieve-owner",
"bodyPatterns" : [ {
"matchesJsonPath": "$.[?($.vehicleIdentifier == 'VIN-A')]"
} ]
},
"response": {
"status": 200,
"bodyFileName": "JsonFiles/Exported-Vehicle.json",
"headers": {
"Content-Type": "application/json;charset=UTF-8"
}
}
},
{
"priority": 1,
"request": {
"method": "POST",
"urlPath": "/v1/retrieve-owner",
"bodyPatterns" : [ {
"matchesJsonPath": "$.[?($.vehicleIdentifier == 'VIN-B')]"
} ]
},
"response": {
"status": 200,
"bodyFileName": "JsonFiles/Exported-Vehicle.json",
"headers": {
"Content-Type": "application/json;charset=UTF-8"
}
}
}
]
}