1、mock 介绍
2、搭建 moco 环境
[
{
"response" :
{
"text" : "Hello World!" #text 在浏览器页面抛出 response 文本内容 "Hello World!"
}
}
]
3、moco 进阶操作
[{
"request": {
"uri": "/test" #在 request 中添加了 uri
},
"response": {
"text": "Hello World!"
}
}]
[{
"request": {
"uri": "/test",
"queries":{ #定义地址栏拼接的参数
"param":"value"
}
},
"response": {
"text": "Hello World!"
}
}]
[{
"request": {
"uri": "/test",
"queries": {
"param": "value"
}
},
"response": {
"status": 222, #定义返回状态码
"text": "Hello World!"
}
}]
[{
"request": {
"uri": "/test"
},
"response": {
"json": { #定义返回的报文内容
"code": "0000",
"msg": "success",
"data": {
"userId": "111222",
"username": "17779828887",
"password": "123456",
"nickName": "Jack"
}
}
}
}]
[{
"request": {
"headers": {
"Content-Type": "application/x-www-form-urlencoded" # 传参类型为 form 表单
},
"method": "POST",
"forms": { # form 表单传参需要使用 forms 定义参数
"phone": "17779828887",
"password": "123456"
}
},
"response": {
"json": {
"uid": "123",
"nickName": "xiaozheng"
}
}
}]
[{
"request": {
"uri": "/test",
"method": "POST", # 定义 post 请求方式
"headers": {
"Content-Type": "application/json" # 传参类型为 json
},
"json": { # json 传参需要用到 json 定义参数
"username": "17779828887",
"password": "123456"
}
},
"response": {
"json": {
"code": "0000",
"msg": "post success",
"data": {
"userId": "111222",
"nickName": "Jack"
}
}
}
}]
[{
"request": {
"uri": "/test",
"method": "get", #定义 get 请求方式
"queries": { #参数采用拼接在地址栏的方式
"username": "17779828887",
"password": "123456"
}
},
"response": {
"json": {
"code": "0000",
"msg": "post success",
"data": {
"userId": "111222",
"nickName": "Jack"
}
}
}
}]
4、moco 常见问题
"response": {
"json": {
"code": "0000",
"msg": "成功",
"data": {
"status": 0
}
},
"headers": {
"Content-Type": "text/json;charset=UTF-8"
}
}
java -Dfile.encoding=UTF-8 -jar moco-runner-1.1.0-standalone.jar http -p 8080 -c userLogin.json
5、配置文件
// config.json
# 定义了 context,则在接口 URL 中需要加入 context 值
# http://xxx.com/path1/login
# http://xxx.com/path2/pay
[
{"context":"/path1", "include":"login.json"},
{"context":"/path2", "include":"pay.json"}
]
// 不使用 context 字段的 config.json。
# http://xxx.com/login
# http://xxx.com/pay
[
{"include":"login.json"},
{"include":"pay.json"}
]
#在 include 的值中带入文件路径
[
{"include":"login/login.json"},
{"include":"pay/pay.json"}
]
// login.json
[{
"request": {
"uri": "/login",
"method": "POST",
"json": {
"phone": "18688886666",
"password": "123456"
}
},
"response": {
"json": {
"state": "0",
"userId": "111222"
}
}
}]
// pay.json
[{
"request": {
"uri": "/pay",
"method": "POST",
"json": {
"amount": "100",
"price": "3",
"userId": "111222"
}
},
"response": {
"json": {
"state": "0",
"orderId": "9321231324221"
}
}
}]
java -jar moco-runner--standalone.jar http -p 12306 -g config.json