我正在向Wiremock(独立版,2.21)发送带有正文的请求
{
"attribute1": "value1",
"attribute2": "value2",
...
"attributen": "valuen"
}
使用/test/test-url
到URL POST
,没有查询参数。
我希望它执行以下操作:
其他属性与答案无关紧要。 我只想通过使用.json文件来做到这一点。谢谢!
答案 0 :(得分:0)
在最新版本的WireMock(2.19+)中,支持HandleBars processing in the BodyFileName属性。然后,您可以将(部分)名称放入JSON请求正文中,然后将其值重新用于文件名引用。
<div id="container">
<input type="file" />
</div>
输入消息:
{
"request" : {
"urlPathPattern" : "/jpathFile",
"method" : "GET",
"headers": {
"Content-Type": {
"equalTo": "application/json"
}
}
},
"response" : {
"status" : 200,
"headers": {
"Content-Type": "application/json"
},
"bodyFileName" : "/{{jsonPath request.body '$.attribute2'}}",
"transformers": ["response-template"]
}
}
位于{
"attribute1": "value1",
"attribute2": "response.json",
"attributen": "valuen"
}
位置的response.json
:
/__files/response.json
答案 1 :(得分:0)
答案是要检查身体形态,并针对3种不同情况进行3种映射:
一种用于检测到text1的情况:
"request": {
"method": "POST",
"urlPattern":"/.*",
"bodyPatterns": [
{
"contains":"\"attribute1\": \"text1\""
}
]
},
"response": {
"status": 200,
"bodyFileName": "response_text1.json",
"headers": {
"Content-Type": "application/json"
}
}
一种检测到text2的情况:
"request": {
"method": "POST",
"urlPattern":"/.*",
"bodyPatterns": [
{
"contains":"\"attribute1\": \"text2\""
}
]
},
"response": {
"status": 200,
"bodyFileName": "response_text2.json",
"headers": {
"Content-Type": "application/json"
}
}
一种情况,但均未检测到。在这种情况下,将给出一个一般性答案。
"request": {
"method": "POST",
"urlPattern": "/.*"
},
"response": {
"status": 200,
"bodyFileName": "response_general.json",
"headers": {
"Content-Type": "application/json"
}
}
答案 2 :(得分:0)
我建议不要使用“ contains”或“ equalTo”,而建议使用“ matchesJsonPath”
"bodyPatterns": [
{
"matchesJsonPath": "$[?(@.attribute1 == 'value1')]"
}
]