fes.js/packages/fes-template/mock.js

144 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-03-15 20:20:32 +08:00
export default function ({ cgiMock, mockjs, utils }) {
const { Random } = mockjs;
2021-02-04 13:43:16 +08:00
// 测试 proxy 与 mock 用例集合
cgiMock('/movie/in_theaters_mock', (req, res) => {
2022-05-06 13:10:09 +08:00
res.send(
JSON.stringify({
code: '0',
msg: '',
result: {
text: 'movie: movie/in_theaters_mock ~~~~~',
},
}),
);
2021-02-04 13:43:16 +08:00
});
cgiMock('/movie/test_mock', (req, res) => {
2022-05-06 13:10:09 +08:00
res.send(
JSON.stringify({
code: '0',
msg: '',
result: {
text: 'mock: movie/test_mock',
},
}),
);
2021-02-04 13:43:16 +08:00
});
// 测试用例: mock.js change重现请求需要能拉最新的数据
cgiMock('/watchtest', (req, res) => {
2022-05-06 13:10:09 +08:00
res.send(
JSON.stringify({
code: '0',
msg: '',
result: {
text: '通过 register 测试 mock watch: 初始状态',
},
}),
);
2021-02-04 13:43:16 +08:00
});
// 返回一个数字
// cgiMock('/number', 666);
cgiMock('/number', 999);
// 返回一个json
cgiMock({
url: '/json',
result: {
2022-05-06 13:10:09 +08:00
code: '400101',
msg: "不合法的请求:Missing cookie 'wb_app_id' for method parameter of type String",
transactionTime: '20170309171146',
success: false,
},
2021-02-04 13:43:16 +08:00
});
// 利用 mock.js 产生随机文本
cgiMock('/text', Random.cparagraph());
// 返回一个字符串 利用 mock.js 产生随机字符
2022-05-06 13:10:09 +08:00
cgiMock(
'/random',
mockjs.mock({
'string|1-10': '★',
}),
);
2021-02-04 13:43:16 +08:00
// 正则匹配url, 返回一个字符串
cgiMock(/\/abc|\/xyz/, 'regexp test!');
// option.result 参数如果是一个函数, 可以实现自定义返回内容, 接收的参数是是经过 express 封装的 req 和 res 对象.
cgiMock(/\/function$/, (req, res) => {
res.send('function test');
});
// 返回文本 readFileSync
cgiMock('/file', utils.file('./package.json'));
2021-02-04 13:43:16 +08:00
// 更复杂的规则配置
cgiMock({
url: /\/who/,
method: 'GET',
result(req, res) {
if (req.query.name === 'kwan') {
res.json({ kwan: '孤独患者' });
} else {
res.send('Nooooooooooo');
}
},
headers: {
'Content-Type': 'text/plain',
'Content-Length': '123',
2022-05-06 13:10:09 +08:00
ETag: '12345',
2021-02-04 13:43:16 +08:00
},
cookies: [
{
2022-05-06 13:10:09 +08:00
name: 'myname',
value: 'kwan',
maxAge: 900000,
httpOnly: true,
},
],
2021-02-04 13:43:16 +08:00
});
// 携带参数的请求
cgiMock('/v2/audit/list', (req, res) => {
2022-05-06 13:10:09 +08:00
const { currentPage, pageSize, isAudited } = req.body;
2021-02-04 13:43:16 +08:00
res.send({
code: '0',
msg: '',
data: {
currentPage,
pageSize,
totalPage: 2,
totalCount: 12,
pageData: Array.from({ length: pageSize }, () => ({
title: Random.title(),
authorName: Random.cname(),
authorId: Random.name(),
createTime: Date.now(),
updateTime: Date.now(),
readCount: Random.integer(60, 1000),
favoriteCount: Random.integer(1, 50),
postId: '12323',
serviceTag: '业务类型',
productTag: '产品类型',
requestTag: '需求类型',
handleTag: '已采纳',
postType: 'voice',
postStatus: isAudited ? 'pass' : 'auditing',
2022-05-06 13:10:09 +08:00
auditStatus: 'audit1',
})),
},
2021-02-04 13:43:16 +08:00
});
});
// multipart/form-data 类型
cgiMock('/v2/upload', (req, res) => {
res.send({
code: '0',
2022-05-06 13:10:09 +08:00
msg: '文件上传成功',
2021-02-04 13:43:16 +08:00
});
});
2021-03-15 20:20:32 +08:00
}