mountebank配置(一)

mountebank的模拟请求配置主要集中在stubs中,这里对stubs配置进行一个集中的说明。

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

predicates
predicate的类型和示例

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

  • equals

    判断是否匹配。这个很简单,估计也将是使用率最高的断言。

    The request field matches the predicate

  • deepEquals

    深度匹配。当请求字段是对象的时候比较有用。

    Performs nested set equality on the request field, useful when the request field is an object (e.g. the query field in http)

  • contains

    请求包含内容。示例

    {
      "port": 4547,
      "protocol": "http",
      "stubs": [
        {
          "responses": [{ "is": { "body": "first" } }],
          "predicates": [
            { 
              "contains": { "body": "first" } 
            },
            {
              "equals" : {
                "path":"/test"
              }
            }
          ]
        },
        {
          "responses": [{ "is": { "body": "second" } }],
          "predicates": [
            {
              "and" : [
                { 
                  "contains": { "body": "second" } 
                },
                {
                  "equals" : {
                    "path":"/test"
                  }
                 }            
              ]
            }
          ]
        },
        {
          "responses": [{ "is": { "body": "third" } }],
          "predicates": [
            {
              "contains": { "body": "third" } 
            },
            {
              "equals" : {
                "path":"/test"
              }
            }
          ]
        }
      ]
    }

    请求

    curl -i -X POST "http://localhost:4547/test" --data "third"

    响应

    HTTP/1.1 200 OK
    Connection: close
    Date: Thu, 01 Mar 2018 02:31:07 GMT
    Transfer-Encoding: chunked
    
    third
  • startsWith & endsWith

    这两个predicates见名知意,以XX开始和以XX结束。

    {
      "port": 4547,
      "protocol": "http",
      "stubs": [
        {
          "responses": [{ "is": { "body": "startswith first" } }],
          "predicates": [
            { 
              "startsWith": { "query": {
        "first" : "first"} } 
            },
            {
              "equals" : {
                "path":"/test"
              }
            }
          ]
        },
        {
          "responses": [{ "is": { "body": "endswith first" } }],
          "predicates": [
            {
              "endsWith": { "query": {
        "first" : "first"} } 
            },
            {
              "equals" : {
                "path":"/test"
              }
            }
          ]
        }
      ]
    }

    请求

    curl -i -X GET "http://localhost:4547/test?first=firstxxxx"

    响应

    HTTP/1.1 200 OK
    Connection: close
    Date
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值