上一篇中对
stubs
配置做了介绍,该篇对imposter
和stubs
在上一篇中没有提及的配置进行简短的介绍,该篇内容并不多。
配置多个imposter
前篇中的示例都是在imposters.ejs
文件中配置了一个imposter
,mb
在启动时是可以加载多个imposter
的,并且可以加载外部文件,配置如下:
{
"imposters": [
<% include ./imposter1.json %>,
<% include ./imposter2.json %>,
<% include ./imposter3.json %>
]
}
imposter1.json imposter2.json imposter3.json
三个文件代表了三个imposter
的配置, 文件内容示例:
{
"port": 4545,
"protocol": "http",
"stubs": [{
"predicates": [{
"equals": {
"path": "/test",
"method": "POST",
"headers": { "Content-Type": "application/json" }
}
}],
"responses": [
{ "is": { "statusCode": 400 }}
]
}]
}
暂时发现一个问题,各个imposter的监听端口不可重复,否则会有一个imposter不会被加载
imposter的配置项
这里补充一下imposter中的配置项,前篇中只在示例中写到了
protocol
,port
,stubs
。
配置项 | 参数配置 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
protocol | http | Y | N/A | 可接收的协议,mb支持http , https , tcp , smtp ,但是该列表中的参数针对的是http , 所以参数配置值为http
|
port | 可使用的端口号 | N | 随机端口号,建议填写。mb会在POST返回真正的端口号(然而我不知道请求那个地址) | 监听端口 |
name | 任何字符串 | N | 空字符串 | 改名称会包含在日志中,当配置多个imposter时比较有用,可以进行区分 |
recordRequests | true or false | N | false | 暂时没研究,贴一下原文:Adds mock verification support by remembering the requests made to this imposter. Note that this represents a memory leak for any long running mb process, as requests are never forgotten. |
stubs | 可用的stubs配置对象 | N | [] | 模拟请求的配置数组 |
defaultResponse | 可用的response 对象 |
N | { "statusCode": 200, "headers": { "connection": "close" }, "body": ""} |
默认的响应对象, predicates 没有匹配时返回改响应对象 |
allowCORS | boolean | N | false | 暂时未研究,贴一下原文:If true, mountebank will allow all CORS preflight requests on the imposter. |
一下两节内容的配置,我并没有一一尝试
stubs配置Predicates的其他参数
参数配置 | 默认值 | 描述 |
---|---|---|
caseSensitive | false | 匹配项是否大小写敏感 |
except | "" | Defines a regular expression that is stripped out of the request field before matching. |
xpath | null | Defines an object containing a selector string and, optionally, an ns object field that defines a namespace map. The predicate's scope is limited to the selected value in the request field. |
jsonpath | null | Defines an object containing a selector string. The predicate's scope is limited to the selected value in the request field. |
示例:
{
"port": 4547,
"protocol": "http",
"stubs": [
{
"responses": [{ "is": { "body": "first" } }],
"predicates": [
{
"equals" : {
"path":"/test",
"body" : "first"
},
"caseSensitive":true,
"except" : "\\d+$"
}
]
}
]
}
这里的except
没有理解是怎么样的匹配规则,后续补充。
xpath和jsonpath请阅读官方文档,传送门:xpath、jsonpath
stubs配置Response的Behaviors
这些behaviors我只尝试了几个简单的,这里整理一下。
-
wait
请求等待一定时间后进行响应,应该可以用来模拟长时间请求。
该参数支持function操作,mb启动时添加--allowInjection
参数示例:
{ "port": 4545, "protocol": "http", "stubs": [ { "responses": [ { "is": { "body": "This took at least half a second to send" }, "_behaviors": { "wait": 500 } } ] } ] }
{ "port": 4545, "protocol": "http", "stubs": [ { "responses": [ { "is": { "body": "This took at least 100 to 1000 ms to send" }, "_behaviors": { "wait": "function() { return Math.floor(Math.random() * 901) + 100; }" } } ] } ] }
-
repeat
响应的重复次数。
{ "port": 7777, "protocol": "http", "stubs": [ { "responses": [ { "is": { "body": "This will repeat 2 times" }, "_behaviors": { "repeat": 2 } }, { "is": { "body": "Then this will return" } } ] } ] }
This will repeat 2 times
响应两次后,才会返回Then this will return
响应对象。 -
其他参数
copy
,lookup
,decorate
,shellTransform
请阅读官方文档, 传送门
网友评论