Java 杂谈程序员

mountebank配置(一)

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

上一篇中讲到了安装和启动(跳转到mountebank安装、启动),该篇中记录一下如何进行简单的配置和使用,mountebank的模拟请求配置主要集中在stubs中,这里对stubs配置进行一个集中的说明。

stubs中主要组成是predicates和response,接下来会对这两项进行说明。

predicates

predicate的类型和示例

这里会介绍predicates的断言类型,mountebank提供了多种断言类型对请求进行断言。

predicate的匹配项和匹配规则
匹配项 类型 规则
requestFrom string 这个参数没有尝试,看原文:The client socket, primarily used for logging and debugging.
path string 请求的路径,不要带有请求参数 /test是正确的,/test?q=1是不正确的。
query object 请求的参数,例如: predicates类型是equals,"query":{"q":1};predicates的类型是exists, "query":{"q":true, "w":false}
method string 请求方法。
headers object 请求头的headers信息。
body string POST请求的请求包体。
form object POST请求的form格式数据。例如:请求包体firstname=f&lastname=l,可以使用lastname来进行匹配

response

response类型
Response Type 描述
is 对匹配的predicate做出响应的内容。原文:Merges the specified response fields with the response defaults .
proxy 使用代理。 原文:Proxies the request to the specified destination and returns the response. The response is saved and can be replayed on subsequent calls.
inject 使用function进行动态的注入。原文:Allows you to inject a JavaScript function to create the response object.

本人对proxyinject没有尝试,这里先对is进行一些总结。

response配置项
配置项 类型 默认值 说明
statusCode int 200 http 响应的码值
headers object { "Connection": "close" } http响应header头
body string or object "" http响应包体

配置项的值可以使用 <%- stringify(filename, 'specifications/fields/fieldlist.json') %> 来引用其他文件,例如:

filedlist.json文件内容

{
    "status": 200,
    "message": "请求成功",
    "data": {
        "list": [{
            "name": "项目简介",
            "type": 0,
            "url": "",
            "value": "0.00%"
        }, {
            "name": "项目简介",
            "type": 0,
            "url": "",
            "value": {
                "rowTitle": ["", "", ""],
                "rowsCount": 2,
                "colsCount": 2,
                "content": [
                    ["", "", ""],
                    ["", "", ""],
                    ["", "", ""]
                ]
            }
        }]
    }
}

is 的配置

"is": {
                    "statusCode" : 200,
                    "body": "<%- stringify(filename, 'specifications/fields/fieldlist.json') %>"
                }
示例
{
  "port": 4551,
  "protocol": "http",
  "stubs": [
    {
      "responses": [
        { 
          "is": { 
            "statusCode" : 200,
            "body": "first response",
            "headers" : { 
              "Accept":"text/plain,text/html", 
              "Set-Cookie":"91token=1234",
              "Access-Control-Allow-Headers":"Content-Type" 
            }
          } 
        },
        {
          "is": {
            "statusCode" : 200,
            "body": { "status":"200", "data" : {"list":[]} },
            "headers" : { 
              "Accept":"text/plain,text/html",
              "Set-Cookie":"91token1=12341234",
              "Access-Control-Allow-Headers":"Content-Type" 
            }
          } 
        },
        { 
          "is": { 
            "statusCode" : 404,
            "headers" : { 
              "Accept":"text/plain,text/html", 
              "Set-Cookie":"91token2=12341234",
              "Access-Control-Allow-Headers":"Content-Type" 
            }
          } 
        }
      ],
      "predicates": [
        {
          "equals": {
            "path" : "/test"
          }
        }
      ]
    }
  ]
}

请求

curl -i -X GET "http://localhost:4551/test"

上述请求依次执行三次,三次响应结果如下:

HTTP/1.1 200 OK
Accept: text/plain,text/html
Set-Cookie: token=1234
Access-Control-Allow-Headers: Content-Type
Connection: close
Date: Fri, 02 Mar 2018 06:27:23 GMT
Transfer-Encoding: chunked

first response
HTTP/1.1 200 OK
Accept: text/plain,text/html
Set-Cookie: token1=12341234
Access-Control-Allow-Headers: Content-Type
Connection: close
Date: Fri, 02 Mar 2018 06:27:27 GMT
Transfer-Encoding: chunked

{
    "status": "200",
    "data": {
        "list": []
    }
}
HTTP/1.1 404 Not Found
Accept: text/plain,text/html
Set-Cookie: token2=12341234
Access-Control-Allow-Headers: Content-Type
Connection: close
Date: Fri, 02 Mar 2018 06:27:28 GMT
Transfer-Encoding: chunked
上一篇 下一篇

猜你喜欢

热点阅读