1,安装

Linux安装包,不用安装Node.js

https://s3.amazonaws.com/mountebank/v1.10/mountebank-v1.10.0-linux-x64.tar.gz

2,启动

./mb

3,例子

By default, mountebank listens on port 2525, but that's not the port that your imposters (test doubles) will listen on. To show a couple different kinds of imposters, let's create both an http imposter and a tcp one. We'll use the curl command line tool to call mountebank's api. The following command creates the http imposter, listening on port 4545, by POSTing to http://localhost:2525/imposters with the given body. The predicates are optional - if you don't include any, the stub always matches, and the response is always sent.

The contracts page provides an easy to use exploration of the JSON structure.


curl -i -X POST -H 'Content-Type: application/json' http://localhost:2525/imposters --data '{
"port": 4545,
"protocol": "http",
"stubs": [{
"responses": [
{ "is": { "statusCode": 400 }}
],
"predicates": [{
"and": [
{
"equals": {
"path": "/test",
"method": "POST",
"headers": {
"Content-Type": "application/json"
}
}
},
{
"not": {
"contains": {
"body": "requiredField"
},
"caseSensitive": true
}
}
]
}]
}]
}'

Let's test it out:


curl -i -X POST -H 'Content-Type: application/json' http://localhost:4545/test --data '{"optionalField": true}'

HTTP/1.1 400 Bad Request
Connection: close
Date: Sat, 04 Jan 2014 02:48:16 GMT
Transfer-Encoding: chunked

Had we not tailored the request to match the predicates, we would have instead received the default response. For instance, let's send a request that leaves off the Content-Type:


curl -i -X POST http://localhost:4545/test --data '{"optionalField": true}'

HTTP/1.1 200 OK
Connection: close
Date: Sat, 04 Jan 2014 02:48:16 GMT
Transfer-Encoding: chunked

mountebank can stub binary tcp equally well, which is convenient when your application integrates with a downstream system using one of the myriad binary RPC protocols. Those protocols tend to rely on language-specific serialization to return an object graph. Your test can use the same serialization code to create a binary stream of the object you want the imposter to return during an RPC call, and encode it as a base64 string. That string is what you send to the imposter. In the example below, we're telling the imposter to respond with a base64-encoded string of "hello, world!" when a tcp request containing the string "sayHello" is sent to port 5555, which could correspond to the method name serialized in the RPC call:


curl -i -X POST -H 'Content-Type: application/json' http://localhost:2525/imposters --data '{
"port": 5555,
"protocol": "tcp",
"mode": "binary",
"stubs": [{
"responses": [
{ "is": { "data": "aGVsbG8sIHdvcmxkIQ==" }}
],
"predicates": [{ "contains": { "data": "c2F5SGVsbG8=" } }]
}]
}'

We'll use nc (netcat) to make the tcp request, which is like telnet but easier to script.


echo "Calling sayHello over binary protocol" | nc localhost 5555

hello, world!

Finally, we can shut down both imposters by issuing an HTTP DELETE to both imposters, which are identified by the port number on the URL:


curl -X DELETE http://localhost:2525/imposters/4545
curl -X DELETE http://localhost:2525/imposters/5555

Explore more in the links on the left. Don't hesitate to ask for help!

微服务测试打桩/mock工具mountebank的更多相关文章

  1. CODING DevOps 系列第五课:微服务测试——微服务下展开体系化的微服务测试

    微服务测试的痛点与挑战 这张图可以形象地展示单体服务和微服务的对比,单体应用就像左边巨大的集装箱,软件模块和应用都包括其中:而微服务就像是由一个小集装箱组成,微小的服务组成一个庞大.完整的系统.单体服 ...

  2. 二、springcloud微服务测试环境搭建

    版本说明: springcloud:Greenwich.SR3 springboot:2.1.8 1.构建步骤 1.1.microservicecloud整体父工程Project 新建父工程micro ...

  3. 基于node.js构建微服务中的mock服务

    缘起 由于现在微服务越来越火了,越来越多的微服务融入到了日常开发当中.在开发微服务的时候,经常会遇到一个问题由于依赖于其他服务,导致你的进度受到阻碍.使你不得不先mock出你期望调用依赖服务的输出,来 ...

  4. Wcf服务测试自带工具

    Visual Studio 安装包文件夹 \Common7\IDE\WcfTestClient.exe

  5. 12个强大的Web服务测试工具

    在过去的几年中,web服务或API的普及和使用有所增加. web服务或API是程序或软件组件的集合,可以帮助应用程序进行交互或通过形成其他应用程序或服务器之间的连接执行一些进程/事务处理.基本上有两种 ...

  6. dubbo系列一、dubbo背景介绍、微服务拆分

    一.背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 二.传统应用到分布式应用的演进过程 ...

  7. 85道Java微服务面试题整理(助力2020面试)

    微服务 面试题 1.您对微服务有何了解? 2.微服务架构有哪些优势? 3.微服务有哪些特点? 4.设计微服务的最佳实践是什么? 5.微服务架构如何运作? 6.微服务架构的优缺点是什么? 7.单片,SO ...

  8. [dotnet core]落地微服务特色的DevOps管道,持续集成/部署到kubernetes。

    目录 前言 目标 工具 - 最小的学习成本 方案 - 愿景 1. 持续集成 - CI 2. 持续部署 - CD 部署环境 1. 部署gitlab-runner 2. 注册gitlab-runner 搭 ...

  9. 2019年微服务5大趋势,你pick哪个?

    2018年对于微服务来说是非常重要的一年,这一年Service Mesh开始崭露头角,解决服务间复杂的通信问题,这一年很多国内互联网公司已经有了较为成熟的微服务实践案例,网易云主办的微服务实践沙龙中也 ...

随机推荐

  1. tomcat配置https–采用JDK自带的keytool工具生成证书

    转自:http://blog.csdn.net/huangxinyu_it/article/details/41693633 有关http与https的区别请看<浅谈http与https的区别( ...

  2. 回收机制GC

    .NET 之 垃圾回收机制GC 一.GC的必要性 1.应用程序对资源操作,通常简单分为以下几个步骤:为对应的资源分配内存 → 初始化内存 → 使用资源 → 清理资源 → 释放内存. 2.应用程序对资源 ...

  3. EasyUI datagrid 查询、设置、提交 三

    查询 $(“#grid”).datagrid(“load”,{  a: $('#id').val(),b :$('#text').val() });   {} 里面可以 是序列化参数 $(“#grid ...

  4. php基础-3

    php的数据类型 字符串 字符串的声明:$str = "aaa"; 字符串的方法 strpos(str, find_str):该方法在一个字符串中查找需要查找的字符串,并回来该字符 ...

  5. python基础-函数基本特性和用法

    函数: 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因变量,y是x的函数.自变量x的取值 ...

  6. 新一代构建工具gradle学习

    简介:Gradle的出现,是技术发展的必然,站在了Ant.maven等构建工具的肩膀上,使用了一种强大且具有表达性的基于Groovy的领域特定语言(DSL),使其拥有易用且灵活的方式去实现定制逻辑.方 ...

  7. gearman 简单试用

    服务启动 使用yum 的安装包 安装server yum install  -y gearmand 启动 systemctl restart  gearmand   启动worker gearman ...

  8. laravel使用过程中一些总结

    推荐连接: laravel辅助函数总结:https://laravel-china.org/docs/laravel/5.5/helpers 基于 Laravel 集成的 Monolog 库对日志进行 ...

  9. Web 单点登录(SSO) 实现模型

    有网友问起, 前后端分离 架构下的  Web 单点验证 怎么做, 我画了个图 : Temp Token  就 相当于 短信验证码 . Web 单点登录 都可以用这个 模型, 不仅仅是 前后端分离 .

  10. Lambda 表达式 是 个 好东东

    Lambda 表达式 是 个 好东东 首先,通过 Lambda 表达式 + 动态语言特性 dynamic , C# 已经 可以 实现 函数式 编程 了 其次, 利用 Lambda, 可以 实现 AOP ...