Mountebank - 如何在 mountebank 中的 POST 请求中断言消息正文是 JSON 格式
Mountebank - How do I assert message body is JSON format in a POST request in mountebank
我想在 mountebank 的 POST 请求中存根并检查消息正文,
{
"port": "22001",
"protocol": "http",
"name": "login_user",
"stubs": [
{
"responses": [
{
"is": {
"statusCode": 201,
"headers": {
"Content-Type": "application/json"
},
"body": {}
},
"_behaviors": {
"wait": 100
}
}
],
"predicates": [
{
"equals": {
"path": "/login_user",
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": {
"name": "Tony",
"age": "20"
}
}
}
]
}
]
}
如果邮件正文采用 JSON 格式。预期响应状态代码 200.
例如
{
"body": {
"name": "Tony",
"age": "20"
}
}
如果邮件正文采用 JSON 格式 但 JSON 字符串 。预期响应状态代码 400.
例如
{
"body": "{\"name\": \"Tony\", \"age\": \"20\"}"
}
您可以使用 'matches' 谓词来实现此目的,其中包含一个(在本例中相当粗略)正则表达式以捕获任何字符串输入和 return 一个 400:
"stubs": [
{
"responses": [
{
"is": {
"statusCode": 400,
"headers": {
"Content-Type": "application/json"
},
"body": {}
},
"_behaviors": {
"wait": 100
}
}
],
"predicates": [
{
"matches": {
"path": "/login_user",
"method": "POST",
"body": ".*\\\\"name.*"
}
}
]
},
{
"responses": [
{
"is": {
"statusCode": 201,
"headers": {
"Content-Type": "application/json"
},
"body": {}
},
"_behaviors": {
"wait": 100
}
}
],
"predicates": [
{
"equals": {
"path": "/login_user",
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": {
"name": "Tony",
"age": "20"
}
}
}
]
}
]
或者,您还可以在冒名顶替者声明中指定默认响应代码 400,这样任何与特定谓词不匹配的内容都将 return 默认为 400 响应:
{
"port": "22001",
"protocol": "http",
"name": "login_user",
"defaultResponse": {
"statusCode": 400
},
"stubs": [
{
.... snip ....
我想在 mountebank 的 POST 请求中存根并检查消息正文,
{
"port": "22001",
"protocol": "http",
"name": "login_user",
"stubs": [
{
"responses": [
{
"is": {
"statusCode": 201,
"headers": {
"Content-Type": "application/json"
},
"body": {}
},
"_behaviors": {
"wait": 100
}
}
],
"predicates": [
{
"equals": {
"path": "/login_user",
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": {
"name": "Tony",
"age": "20"
}
}
}
]
}
]
}
如果邮件正文采用 JSON 格式。预期响应状态代码 200.
例如
{
"body": {
"name": "Tony",
"age": "20"
}
}
如果邮件正文采用 JSON 格式 但 JSON 字符串 。预期响应状态代码 400.
例如
{
"body": "{\"name\": \"Tony\", \"age\": \"20\"}"
}
您可以使用 'matches' 谓词来实现此目的,其中包含一个(在本例中相当粗略)正则表达式以捕获任何字符串输入和 return 一个 400:
"stubs": [
{
"responses": [
{
"is": {
"statusCode": 400,
"headers": {
"Content-Type": "application/json"
},
"body": {}
},
"_behaviors": {
"wait": 100
}
}
],
"predicates": [
{
"matches": {
"path": "/login_user",
"method": "POST",
"body": ".*\\\\"name.*"
}
}
]
},
{
"responses": [
{
"is": {
"statusCode": 201,
"headers": {
"Content-Type": "application/json"
},
"body": {}
},
"_behaviors": {
"wait": 100
}
}
],
"predicates": [
{
"equals": {
"path": "/login_user",
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": {
"name": "Tony",
"age": "20"
}
}
}
]
}
]
或者,您还可以在冒名顶替者声明中指定默认响应代码 400,这样任何与特定谓词不匹配的内容都将 return 默认为 400 响应:
{
"port": "22001",
"protocol": "http",
"name": "login_user",
"defaultResponse": {
"statusCode": 400
},
"stubs": [
{
.... snip ....