Mock就是模拟接口, 这里分享Mock 的Moco框架
Moco的源码和jar 下载地址 https://github.com/dreamhead/moco
下载的Moco jar包 全名: moco-runner-0.12.0-standalone.jar, 0.12 是版本号
Moco的使用:
1. 在项目中创建一个package:moco,并将下载的jar包放在该package下
2.创建一个json文件startup1.json,格式如下:
[
{
"description":"mock实例",
"request":{
"uri":"/demo"
},
"response":{
"text":"这是响应!"
}
}
]
3. 进入命令行, mac下使用终端工具, Windows使用cmd 命令
Moco的运行非常的简单:
只需要一行命令即可:
java -jar <path-to-moco-runner> http -p <monitor-port> -c < configuration -file>
<path-to-moco-runner>
:moco-runner-0.12.0-standalone.jar包的路径
<monitor-port>
:http服务监听的端口
<configuration -file>
:配置文件路径
进入到package下 运行命令:java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c startup1.json 请求协议 -p 端口号 -c 配置的文件
10 Oct 2019 17:39:54 [main] INFO Server is started at 8888
10 Oct 2019 17:39:55 [main] INFO Shutdown port is 52901
打开浏览器输入 http://localhost:8888 在浏览器上就可以看到json文件中定义的数据。
a. 不带参数的get实现
[
{
"description":"This is Get request without paramter",
"request":{
"uri":"/demo",
"method":"get"
},
"response":{
"text":"This is response for Get request without paramter "
}
}
]
不带参数的请求浏览器访问地址: localhost:8888/demo
b. 带参数的get实现
{
"description":"This is Get request with paramter",
"request":{
"uri":"/getwithparam",
"method":"get",
"queries":{
"name":"zhangsan",
"age":"18"
}
},
"response":{
"text":"This is response for Get request with paramter "
}
}
带参数的浏览器请求地址:
在浏览器中访问:http://localhost:8888/getwithparam?name=zhangsan&age=18 就可以返回定义的json数据了.
1. 不带参数的post请求
[
{
"description":"This is Post request",
"request":{
"uri":"/postdemo",
"method":"post"
},
"response":{
"text":"This is Post response"
}
}
]
post请求不能通过浏览器直接请求, 可以通过postman jmeter等工具发起post操作.
2.带参数的post请求
{
"description":"This is Post request with paramter",
"request":{
"uri":"/postwithparam",
"method":"post",
"forms":{
"name":"zhangsan",
"age":"18"
}
},
"response":{
"text":"This is Post response with paramter"
}
}
1. 带cookie请求信息的get
{
"description":"This is Get request with cookies",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"This is get response with cookies"
}
}
2. 带cookie请求信息的post
{
"description":"This is Post request with cookies",
"request":{
"uri":"/post/with/cookies",
"method":"post",
"cookies":{
"login":"true"
}
"json":{
"name":"zhangsan",
"age":"18"
}
},
"response":{
"status":200,
"json":{
"zhangsan":"success",
"status":"1"
}
}
}
header请求头信息的get和post是一致的
{
"description":"This is Post request with header",
"request":{
"uri":"/postwithheader",
"method":"post",
"headers":{
"content-type":"application/json"
},
"json":{
"name":"zhangsan",
"age":"18"
}
},
"response":{
"text":"This is Post response with paramter"
}
}
1. 重定向baidu
{
"description":"redirect to www.baidu.com",
"request":{
"uri":"/redirect"
},
"redirectTo":"http://www.baidu.com"
}
2. 重定向到某个指定地址:
{
"description":"redirect to my path",
"request":{
"uri":"/redirect2"
},
"redirectTo":"redirected"
},
{
"description":"This is my path",
"request":{
"uri":"/redirected"
},
"response":{
"text":"redirect success!"
}
}
如果喜欢本文内容,请关注订阅,以便第一时间获取最新的学习内容.