在上文中,我们介绍 Mock 的基本概念,本文我们将详细介绍其中一个快速简单Mock Server Moco。
简单来说 Moco 就是类似一个 Mock 的工具框架,一个简单搭建模拟服务器的程序库 / 工具,下载就是一个JAR包。
在 Moco 的 github 上面有这段话。
Integration, especially based on HTTP protocol, e.g. web service, REST etc, is wildly used in most of our development.
In the old days, we just deployed another WAR to an application server, e.g. Jetty or Tomcat etc. As we all know, it’s so boring to develop a WAR and deploy it to any application server, even if we use an embeded server. And the WAR needs to be reassembled even if we just want to change a little bit.
翻译过来:
集成,特别是基于 HTTP 协议的集成,例如 web 服务、REST 等,在我们的大多数开发中都被广泛使用。
在过去,我们只是将另一场 WAR 包部署到应用服务器上,例如 Jetty 或Tomcat 等。众所周知,开发一个 WAR 包并将其部署到任何应用服务器上是非常枯燥的,即使我们使用的是嵌入式服务器。war包也需要被重新打包即使我们只是想稍微改变一下。
简单来说,Moco 就是解决了开发前端时没有后端支持,开发接口时依赖没有到位的尴尬场景。当然 Moco 的灵活性,让其有越来越多的应用场景,比如我们在开发接口测试的时候。
特点:
Moco 本身支持 API 和独立运行两种方式。通过 API ,开发人员可以在Junit、TestNg 等测试框架里使用 Moco,这样极大地降低了接口测试的复杂度。
Moco 根据一些配置,启动一个真正的 HTTP 服务(监听本地指定端口)。当发起的请求满足一个条件时,就会收到一个 response 。Moco 底层并没有依赖于像 Servlet 这样的重型框架,而是基于 Netty 的网络应用框架编写的,这样就绕过了复杂的应用服务器,所以它的速度是极快的。
加载配置启动 Moco HTTP Server
java -jar <moco-runner-path> http -p <port> -c <configfile-path>
启动命令参数含义:
moco-runner-0.11.0-standalone.jar
包路径。下面介绍不同的 HTTP 服务,以及如何设置 JSON 文件的参数
在本地启动一个 http 服务器,其中监听端口是 12306,配置文件是 JSON 文件。只需要本机发起一个request,如:http://localhost:12306
JSON 脚本
[
{
"description":"这是一个请求URI",
"request":{
"uri":"/7d"
},
"response":{
"text":"success!"
}
}
]
启动命令
java -jar moco-runner-0.11.0-standalone.jar http -p 12306 -c ./src/main/resources/startupURI.json
通过 Postman 验证服务,测试 Get 请求
Moco 服务日志
09 十二月 2018 11:06:50 [nioEventLoopGroup-3-2] INFO Request received:
GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: 7b5a1a47-a287-4674-b94e-c455fc5c645a
X-Lantern-Version: 5.1.0
Content-Length: 0
09 十二月 2018 11:06:50 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
JSON 脚本
[
{
"description":"这是一个请求queries",
"request":{
"uri":"/7d",
"queries":{
"name":"zuozewei"
}
},
"response":{
"text":"success!"
}
}
]
通过 Postman 验证服务,测试 Get 请求
Moco 服务日志
09 十二月 2018 11:21:04 [nioEventLoopGroup-3-2] INFO Request received:
GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: 2d36e386-e022-4478-8acd-258eff4ff684
X-Lantern-Version: 5.1.0
Content-Length: 0
09 十二月 2018 11:21:04 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
JSON 脚本
[
{
"description":"这是一个get请求",
"request":{
"uri":"/7d",
"method":"get"
},
"response":{
"text":"success!"
}
}
]
通过 Postman 验证服务,测试 Get 请求
Moco 服务日志
09 十二月 2018 11:26:42 [nioEventLoopGroup-3-2] INFO Request received:
GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: ae3250b6-0ec0-4875-8970-d37e5b840820
X-Lantern-Version: 5.1.0
Content-Length: 0
09 十二月 2018 11:26:42 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
JSON 脚本
[
{
"description":"这是一个post请求",
"request":{
"uri":"/7d",
"method":"post"
},
"response":{
"text":"success!"
}
}
]
通过 Postman 验证服务,测试 Post 请求
Moco 服务日志
09 十二月 2018 11:29:30 [nioEventLoopGroup-3-2] INFO Request received:
POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Postman-Token: 73f38af1-4efb-473a-b9d2-de0392c65bbe
X-Lantern-Version: 5.1.0
09 十二月 2018 11:29:30 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
JSON 脚本
[
{
"description":"这是一个带headers的post请求",
"request":{
"uri":"/7d",
"method":"post",
"headers":{
"content-type":"application/json"
}
},
"response":{
"text":"success!"
}
}
]
通过 Postman 验证服务,测试 Post 请求
Moco 服务日志
09 十二月 2018 11:34:43 [nioEventLoopGroup-3-2] INFO Request received:
POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: application/json
Postman-Token: 0a82d74b-303f-42a3-9da0-32fd6c604166
X-Lantern-Version: 5.1.0
09 十二月 2018 11:34:43 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
JSON 脚本
[
{
"description":"这是一个带cookies的post请求",
"request":{
"uri":"/7d",
"method":"post",
"cookies":{
"login":"7dgroup"
}
},
"response":{
"text":"success!"
}
}
]
通过 Postman 验证服务,测试 Post 请求
Moco 服务日志
09 十二月 2018 12:26:46 [nioEventLoopGroup-3-3] INFO Request received:
POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Cookie: login=7dgroup
Postman-Token: 36a12412-6eb1-44a4-a2d8-ea222eba8968
X-Lantern-Version: 5.1.0
09 十二月 2018 12:26:46 [nioEventLoopGroup-3-3] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
JSON 脚本
[
{
"description":"这是一个带forms参数的post请求",
"request":{
"uri":"/7d",
"method":"post",
"forms":{
"name":"zuozewei"
}
},
"response":{
"text":"success!"
}
}
]
通过 Postman 验证服务,测试 Post 请求
Moco 服务日志
09 十二月 2018 12:50:47 [nioEventLoopGroup-3-3] INFO Request received:
POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 167
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------977669308391204172275520
Postman-Token: 308d06bf-c110-4736-9ac4-ee2fe8a4a036
X-Lantern-Version: 5.1.0
<content is binary>
09 十二月 2018 12:50:47 [nioEventLoopGroup-3-3] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
对于 Restful 风格的 url ,支持正则匹配。
JSON 脚本
[
{
"description":"这是一个请求Match URI",
"request":{
"uri":
{
"match":"/\\w*/7d"
}
},
"response":{
"text":"success!"
}
}
]
通过 Postman 验证服务,测试 Post 请求
Moco 服务日志
09 十二月 2018 13:05:48 [nioEventLoopGroup-7-2] INFO Request received:
POST /wzuozewei/7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------767805110351846142172059
Postman-Token: 5d7b5c65-1f8b-46ae-8868-62def1a5de31
X-Lantern-Version: 5.1.0
09 十二月 2018 13:05:48 [nioEventLoopGroup-7-2] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
JSON 脚本
[
{
"description":"这是一个请求StartsWith URI",
"request":{
"uri":
{
"startsWith":"/7d"
}
},
"response":{
"text":"success!"
}
}
]
通过 Postman 验证服务,测试 Get 请求
Moco 服务日志
09 十二月 2018 13:12:43 [nioEventLoopGroup-3-2] INFO Request received:
GET /7d/zuozewei HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------445269276531904972620891
Postman-Token: f9deca3a-9b59-426c-ad48-00ebb4800321
X-Lantern-Version: 5.1.0
Content-Length: 0
09 十二月 2018 13:12:43 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
JSON 脚本
[
{
"description":"这是一个请求endsWith URI",
"request":{
"uri":
{
"endsWith":"/7d"
}
},
"response":{
"text":"success!"
}
}
]
通过 Postman 验证服务,测试 Get 请求
Moco 服务日志
09 十二月 2018 13:16:48 [nioEventLoopGroup-3-2] INFO Request received:
GET /zuozewei/7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------516453569550782372688423
Postman-Token: 774378a6-5e57-4cc2-a015-f4b3bd2cb84d
X-Lantern-Version: 5.1.0
Content-Length: 0
09 十二月 2018 13:16:48 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
JSON 脚本
[
{
"description":"这是一个请求Contain URI",
"request":{
"uri":
{
"contain":"/7d"
}
},
"response":{
"text":"success!"
}
}
]
通过 Postman 验证服务,测试 Get 请求
Moco 服务日志
09 十二月 2018 13:20:28 [nioEventLoopGroup-3-2] INFO Request received:
GET /zuozewei/7d/12345 HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------030965700716204296542028
Postman-Token: 7615db1b-77e1-40f7-bdc3-e464c4e7269a
X-Lantern-Version: 5.1.0
Content-Length: 0
09 十二月 2018 13:20:28 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
Content-Length: 10
Content-Type: text/plain; charset=utf-8
success!
JSON 脚本
[
{
"description":"这是一个指定Json响应的post请求",
"request":{
"uri":"/7d",
"method":"post"
},
"response":{
"json":{
"name":"success",
"code":"1"
}
}
}
]
通过 Postman 验证服务,测试 Post 请求
Moco 服务日志
09 十二月 2018 13:25:19 [nioEventLoopGroup-3-2] INFO Request received:
POST /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Content-Length: 0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------703341725381001692596870
Postman-Token: e5686919-85b9-44d0-8a73-61bf804b6377
X-Lantern-Version: 5.1.0
09 十二月 2018 13:25:19 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
Content-Length: 29
Content-Type: application/json; charset=utf-8
{"name":"success","code":"1"}
JSON 脚本
[
{
"description":"这是指定响应status的get请求",
"request":{
"uri":"/7d",
"method":"get"
},
"response":{
"status":200
}
}
]
通过 Postman 验证服务,测试 Get 请求
Moco 服务日志
09 十二月 2018 13:29:07 [nioEventLoopGroup-3-2] INFO Request received:
GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------465777039297587100709267
Postman-Token: 791fa21c-386f-4389-aaa9-ba06d9e53aff
X-Lantern-Version: 5.1.0
Content-Length: 0
09 十二月 2018 13:29:07 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
JSON 脚本
[
{
"description":"这是一个get请求",
"request":{
"uri":"/7d",
"method":"get"
},
"response":{
"headers":{
"content-type":"application/json"
}
}
}
]
通过 Postman 验证服务,测试 Get 请求
Moco 服务日志
09 十二月 2018 13:34:22 [nioEventLoopGroup-3-2] INFO Request received:
GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------774041889819140984857561
Postman-Token: 0a51f958-0338-4afa-8ff6-af45d61e12a7
X-Lantern-Version: 5.1.0
Content-Length: 0
09 十二月 2018 13:34:22 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
content-type: application/json
JSON 脚本
[
{
"description":"这是一个响应Cookies的get请求",
"request":{
"uri":"/7d",
"method":"get"
},
"response":{
"cookies":{
"login":"7dgroup"
}
}
}
]
通过 Postman 验证服务,测试 Get 请求
Moco 服务日志
09 十二月 2018 13:39:00 [nioEventLoopGroup-3-2] INFO Request received:
GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------315176206881627055625168
Postman-Token: f6d1ae6b-c1c2-474a-827c-f02ed3f23482
X-Lantern-Version: 5.1.0
Content-Length: 0
09 十二月 2018 13:39:00 [nioEventLoopGroup-3-2] INFO Response return:
HTTP/1.1 200
Set-Cookie: login=7dgroup; Path=/
JSON 脚本
[
{
"description":"这是一个重定向的get请求",
"request":{
"uri":"/7d",
"method":"get"
},
"redirectTo":"http://blog.csdn.net/zuozewei"
}
]
通过浏览器验证服务,测试 Get 请求
http://127.0.0.1:12306/7d
Moco 服务日志
09 十二月 2018 13:43:58 [nioEventLoopGroup-4-2] INFO Request received:
GET /7d HTTP/1.1
Host: 127.0.0.1:12306
User-Agent: PostmanRuntime/7.4.0
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=--------------------------167408129884853494096695
Cookie: login=7dgroup
Postman-Token: f83696d4-37ba-45b6-aff6-6f20982673ac
X-Lantern-Version: 5.1.0
Content-Length: 0
09 十二月 2018 13:43:58 [nioEventLoopGroup-4-2] INFO Response return:
HTTP/1.1 302
Location: http://blog.csdn.net/zuozewei
Moco 的使用很简单,配置也很方便,目前更是提供了 http、rest、socket 服务。但是也仅仅是能 stub 接口,模拟出简单的场景。如果接收到请求后需要做一些处理,如需查询数据库、进行运算、或者一些复杂的操作,就无能为力了。所以是否选用 Moco,就取决于测试人员是否只是需要一个简单的模拟服务器。
本文源码:https://github.com/zuozewei/moco-demo