目录
一、概要介绍
moco框架提供两种方式模拟接口。第一种是通过json配置文件,本地启动模拟的服务器;第二种是在单元测试中 使用代码启动模拟接口,以此方便测试。Github源码地址:Moco
二、通过json配置文件 启动模拟服务器
1.快速开始(含中文注释)
快速开始步骤如下:
- 下载jar包:Moco稳定版本列表
- 新建test.json文件,文件内容 参考下方代码块(必须删除中文注释),test.json与jar放在同级目录。
- cmd命令行中 cd到 jar包所在目录
- cmd命令行中执行命令:java -jar moco-runner-1.1.0-standalone.jar http -p 8090 -c test.json
- 在浏览器中打开:http://localhost:8090/demo1 ,界面显示:Hello,Moco1
- 在浏览器中打开:http://localhost:8090/demo2 ,界面显示:Hello,Moco2
- 在同局域网内,测试下机顶盒 能否通过链接 请求到相同返回值。如:http://192.168.50.133:8090/demo1
注:cmd命令详解 java -jar moco-runner-版本号-standalone.jar 协议类型 -p 端口号 -c 配置文件
上述场景正常后,就可以开始搞事情了
[
{
"description":"此处在请求时不会返回,只做注释用且不需要删除:这是我们的第一个moco例子",
"request"://设置请求匹配器
{
"uri":"/demo1"//设置请求匹配的url
},
"response"://设置请求返回的内容
{
"text":"Hello,Moco1"//设置请求返回的内容是text格式 且 内容是Hello,Moco1
}
},
{
"description":"这是我们的第二个mock例子",
"request":{
"uri":"/demo2"
},
"response":
{
"text":"Hello,Moco2"
}
}
]2.json配置文件详解
(1).模拟Get请求(含返回json示例)
- 新建testget.json文件,文件内容 参考下方代码块(必须删除中文注释),testget.json与jar放在同级目录。
- ......
- 在浏览器中打开:http://192.168.50.133:8090/withGetDemo ,界面显示:这是不带参数的get请求
- 在浏览器中打开:http://192.168.50.133:8090/wihtGetDemobyParam?p1=cnn&p2=good ,界面显示:这是带参数的get请求
[
{
"description": "不带参数的get请求",
"request": {
"uri": "/withGetDemo",
"method": "get"
},
"response": {
"text": "这是不带参数的get请求"
}
},
{
"description": "不带参数的get请求,返回json",
"request": {
"uri": "/withGetJsonDemo",
"method": "get"
},
"response": {
"json": {"address":{"city":"nj","country":"ch"}}
}
},
{
"description": "带参数的get请求,p1,p2是参数",
"request": {
"uri": "/wihtGetDemobyParam",
"method": "get",
"queries": {
"p1": "cnn",
"p2": "good"
}
},
"response": {
"text": "这是带参数的get请求"
}
}
](2).模拟Post请求(表单提交)
- 新建testpost.json文件,文件内容 参考下方代码块(必须删除中文注释),testpost.json与jar放在同级目录。
[
{
"description":"post 请求",
"request":{
"uri":"/postDemo",
"method":"post"
},
"response":{
"text":"这是无参的post请求"
}
},
{
"description":"带参数的post请求",
"request":{
"uri":"/postDemoWithParam",
"method":"post",
"forms":{
"param1":"one",
"param2":"two"
}
},
"response":{
"text":"这是有参的post请求"
}
}
](3).模拟Post请求(请求参数为json格式、请求带cookies)
[
{
"description":"这是一个带cookies的Post请求",
"request":{
"uri":"/postDemoWithCookies",
"cookies":{
"login":"true"
},
"json":{
"name":"hi",
"age":"3"
}
},
"response":{
"status":"200",
"json":{
"name":"success",
"status":"1"
}
}
}
](4).模拟Post请求(请求带header)
注:test.json文件内容本身的编码 必须与 charset配置相符,不然会乱码
[
{
"description":"带header请求",
"request": {
"uri": "/withHeader",
"method": "post",
"headers": {
"content-type": "application/json;charset=gbk"
},
"json": {
"name": "xiaoming",
"age": "18"
}
},
"response":{
"json":{
"message":"success",
"status":"1"
}
}
}
](5).模拟Post请求(请求重定向)
[
{
"description":"重定向到百度",
"request":{
"uri":"/redirect",
"method":"get"
},
"redirectTo":"http://www.baidu.com"
},
{
"description":"这是被重定向的请求",
"request":{
"uri":"/toRedirect"
},
"response":{
"text":"this is the redirect page"
}
},
{
"description":"重定向到自己的网页上",
"request":{
"uri":"/myStation"
},
"redirectTo":"/toRedirect"
}
]
参考链接:
https://blog.csdn.net/qq_32706349/article/details/80472445
https://www.cnblogs.com/bingoTest/p/11353575.html
http://www.imooc.com/article/267877?block_id=tuijian_wz
版权声明:本文为rebornSS原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。