Java 杂谈程序员

mountebank配置(二)

2018-03-11  本文已影响0人  菜鸟栖飞

上一篇中对stubs配置做了介绍,该篇对imposterstubs在上一篇中没有提及的配置进行简短的介绍,该篇内容并不多。

配置多个imposter

前篇中的示例都是在imposters.ejs文件中配置了一个impostermb在启动时是可以加载多个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请阅读官方文档,传送门:xpathjsonpath

stubs配置Response的Behaviors

这些behaviors我只尝试了几个简单的,这里整理一下。

上一篇 下一篇

猜你喜欢

热点阅读