编写规则
- 以- []包裹着以- {}包起来的接口脚本
 
- description:接口简介
 
- request:使用- {}包含接口的请求信息
 - uri:接口的地址名称
- method:请求方法
- queries:get请求参数
- forms:post请求参数
- headers:请求头信息
 
- response:返回的数据,使用- {}
 - text:返回的文字数据
- cookies:返回的cookie信息
- status:返回的响应码
 
举个栗子demo
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | [{
 "description": "这是我们的第一个mock栗子",
 "request": {
 "uri": "/demo"
 },
 "response": {
 "text": "第一个mock响应demo"
 }
 }
 ]
 
 | 
get请求demo
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | [{
 "description": "接口的get请求",
 "request": {
 "uri": "/#/test/xml",
 "method": "get"
 },
 "response": {
 "text": "get请求接口"
 }
 }
 ]
 
 | 
使用moko编写Get和Post测试接口
上一节已经写了规则,这次直接来编写Get和Post请求
模拟一个没有参数的get请求
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | {"description": "模拟一个没有参数的get请求",
 "request": {
 "uri": "/getdemo",
 "method": "get"
 },
 "response": {
 "text": "这是一个没有参数的get请求"
 }
 }
 
 | 
模拟一个带参数的请求
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | {"description": "模拟一个带参数的请求",
 "request": {
 "uri": "getwithparam",
 "method": "get",
 "queries": {
 "name": "胡汉三",
 "age": "18"
 }
 },
 "response": {
 "text": "我胡汉三又回来了!!!!!"
 }
 }
 
 | 
模拟一个Post请求
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | {"description": "模拟一个Post请求",
 "request": {
 "uri": "/postdemo",
 "method": "post"
 },
 "response": {
 "text": "这是我的第一个mosk的post请求"
 }
 }
 
 | 
这是一个带参数的post请求
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | {"description": "这是一个带参数的post请求",
 "request": {
 "uri": "/postwithparam",
 "method": "post",
 "forms": {
 "name": "胡汉三",
 "sex": "男人"
 }
 },
 "response": {
 "text": "我胡汉三带着参数来了!!!!"
 }
 }
 
 | 
使用moko编写包含cookie信息的测试接口
这是一个会返回cookie的get请求
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | {"description": "这是一个会返回cookie的get请求",
 "request": {
 "uri": "/getCookies",
 "method": "get"
 },
 "response": {
 "cookies": {
 "login": "true"
 },
 "text": "恭喜你获得cookies信息成功",
 "status": 200,
 "msg": "成功"
 }
 }
 
 | 
这是一个携带cookies信息的get请求
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | {"description": "这是一个带cookies信息的get请求",
 "request": {
 "uri": "/get/with/cookies",
 "method": "get",
 "cookies": {
 "login": "true"
 }
 },
 "response": {
 "text": "这是一个需要携带cookies信息才能访问的get请求"
 }
 }
 
 | 
这是一个携带cookies信息的post请求
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | {"description": "这是一个带cookies信息的post请求",
 "request": {
 "uri": "/post/with/cookies",
 "method": "post",
 "cookies": {
 "login": "true"
 },
 "json": {
 "name": "huhansan",
 "age": "18"
 }
 },
 "response": {
 "status": 200,
 "json": {
 "huhansan": "success",
 "status": "1"
 }
 }
 }
 
 | 
重定向的测试接口
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | [{
 "description": "重定向到百度",
 "request": {
 "uri": "/redirect"
 },
 "redirectTo": "http://www.baidu.com"
 },
 {
 "description": "重定向到自己的网页上",
 "request": {
 "uri": "/redirect/topath"
 },
 "redirectTo": "/redirect/new"
 },
 {
 "description": "这是被重定到的请求",
 "request": {
 "uri": "/redirect/new"
 },
 "response": {
 "text": "重定向成功了"
 }
 }
 ]
 
 | 
下载jar包
点击moke选择版本进行下载,我下载的的是moco-runner-0.11.0-standalone.jar
使用命令启动测试脚本
| 1
 | java -jar jar包的存放路径 http -p 端口 -c json文件路径
 |