yapi/exts/yapi-plugin-advanced-mock/server.js

197 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-09-17 13:36:51 +08:00
const controller = require('./controller');
2017-10-18 15:59:23 +08:00
const advModel = require('./advMockModel.js');
2017-10-19 10:31:57 +08:00
const caseModel = require('./caseModel.js');
2017-09-17 13:36:51 +08:00
const yapi = require('yapi.js');
2017-09-20 20:21:58 +08:00
const mongoose = require('mongoose');
2017-10-19 10:31:57 +08:00
const _ = require('underscore');
const path = require('path');
const lib = require(path.resolve(yapi.WEBROOT, 'common/lib.js' ));
const Mock = require('mockjs');
2017-09-17 13:36:51 +08:00
2017-10-19 10:31:57 +08:00
function arrToObj(arr){
let obj = {'Set-Cookie': []};
2017-10-19 10:31:57 +08:00
arr.forEach(item=>{
if(item.name === 'Set-Cookie'){
obj['Set-Cookie'].push(item.value);
}else obj[item.name] = item.value;
2017-10-19 10:31:57 +08:00
})
return obj;
}
2017-09-17 13:36:51 +08:00
module.exports = function(){
2017-09-20 20:21:58 +08:00
yapi.connect.then(function () {
2017-10-25 19:33:34 +08:00
let Col = mongoose.connection.db.collection('adv_mock');
2017-10-18 10:04:16 +08:00
Col.createIndex({
2017-09-20 20:21:58 +08:00
interface_id: 1
})
2017-10-18 10:04:16 +08:00
Col.createIndex({
2017-09-20 20:21:58 +08:00
project_id: 1
})
2017-10-18 15:59:23 +08:00
2017-10-25 19:33:34 +08:00
let caseCol = mongoose.connection.db.collection('adv_mock_case');
2017-10-18 15:59:23 +08:00
caseCol.createIndex({
interface_id: 1
})
caseCol.createIndex({
project_id: 1
})
2017-09-20 20:21:58 +08:00
})
2017-10-19 10:31:57 +08:00
async function checkCase(ctx, interfaceId){
let reqParams = Object.assign({}, ctx.query, ctx.request.body);
2017-10-19 10:31:57 +08:00
let caseInst = yapi.getInst(caseModel);
let ip = ctx.ip.match(/\d+.\d+.\d+.\d+/)[0];
2017-10-25 19:33:34 +08:00
// 数据库信息查询
2017-10-19 10:31:57 +08:00
let listWithIp =await caseInst.model.find({
interface_id: interfaceId,
ip_enable: true,
ip: ip
}).select('_id params');
let matchList = [];
listWithIp.forEach(item=>{
let params = item.params;
if(lib.isDeepMatch(reqParams, params)){
2017-10-19 10:31:57 +08:00
matchList.push(item);
}
2017-11-02 17:50:34 +08:00
})
2017-10-19 10:31:57 +08:00
if(matchList.length === 0){
let list =await caseInst.model.find({
interface_id: interfaceId,
ip_enable: false
}).select('_id params')
list.forEach(item=>{
let params = item.params;
if(lib.isDeepMatch(reqParams, item.params)){
2017-10-19 10:31:57 +08:00
matchList.push(item);
}
})
}
if(matchList.length > 0){
2017-11-02 17:50:34 +08:00
let maxItem = _.max(matchList, item=> (item.params && Object.keys(item.params).length || 0 ));
2017-10-19 10:31:57 +08:00
return maxItem;
}
return null;
}
async function handleByCase(caseData, context){
let caseInst = yapi.getInst(caseModel);
let result = await caseInst.get({
_id: caseData._id
})
return result;
}
2017-09-17 13:36:51 +08:00
this.bindHook('add_router', function(addRouter){
addRouter({
controller: controller,
method: 'get',
path: 'advmock/get',
action: 'getMock'
})
addRouter({
controller: controller,
method: 'post',
path: 'advmock/save',
action: 'upMock'
})
2017-10-18 15:59:23 +08:00
addRouter({
/**
* 保存期望
*/
controller: controller,
method: 'post',
path: 'advmock/case/save',
action: 'saveCase'
})
addRouter({
/**
2017-10-25 19:33:34 +08:00
*
2017-10-18 15:59:23 +08:00
*/
controller: controller,
method: 'get',
path: 'advmock/case/get',
action: 'getCase'
})
addRouter({
/**
* 获取期望列表
*/
controller: controller,
method: 'get',
path: 'advmock/case/list',
action: 'list'
})
addRouter({
/**
2017-10-25 19:33:34 +08:00
* 删除期望列表
2017-10-18 15:59:23 +08:00
*/
controller: controller,
method: 'post',
path: 'advmock/case/del',
action: 'delCase'
})
2017-09-17 13:36:51 +08:00
})
this.bindHook('interface_del', async function(id){
let inst = yapi.getInst(advModel);
await inst.delByInterfaceId(id);
})
this.bindHook('project_del', async function(id){
let inst = yapi.getInst(advModel);
await inst.delByProjectId(id);
})
/**
* let context = {
projectData: project,
interfaceData: interfaceData,
ctx: ctx,
mockJson: res
}
*/
this.bindHook('mock_after', async function(context){
let interfaceId = context.interfaceData._id;
2017-10-19 10:31:57 +08:00
let caseData = await checkCase(context.ctx, interfaceId);
if(caseData){
2017-10-19 10:31:57 +08:00
let data = await handleByCase(caseData, context);
context.mockJson = yapi.commons.json_parse(data.res_body);
2017-10-19 10:31:57 +08:00
context.resHeader = arrToObj(data.headers);
2017-10-19 12:09:16 +08:00
context.httpCode = data.code;
2017-10-19 10:31:57 +08:00
context.delay = data.delay;
return true;
}
2017-09-17 13:36:51 +08:00
let inst = yapi.getInst(advModel);
let data = await inst.get(interfaceId);
if(!data || !data.enable || !data.mock_script){
return context;
}
let script = data.mock_script;
let sandbox = {
2017-09-19 17:49:59 +08:00
header: context.ctx.header,
2017-09-17 13:36:51 +08:00
query: context.ctx.query,
body: context.ctx.request.body,
2017-10-18 20:34:01 +08:00
mockJson: context.mockJson,
params: Object.assign({}, context.ctx.query, context.ctx.request.body),
resHeader: context.resHeader,
httpCode: context.httpCode,
delay: context.httpCode,
Random: Mock.Random
2017-09-17 13:36:51 +08:00
}
2017-09-19 17:49:59 +08:00
sandbox.cookie = {};
context.ctx.header.cookie && context.ctx.header.cookie.split(';').forEach(function( Cookie ) {
var parts = Cookie.split('=');
sandbox.cookie[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim();
});
2017-09-17 13:36:51 +08:00
sandbox = yapi.commons.sandbox(sandbox, script);
2017-10-18 20:34:01 +08:00
sandbox.delay = isNaN(sandbox.delay) ? 0 : +sandbox.delay;
2017-10-19 10:31:57 +08:00
2017-09-17 13:36:51 +08:00
context.mockJson = sandbox.mockJson;
2017-10-18 20:34:01 +08:00
context.resHeader = sandbox.resHeader;
context.httpCode = sandbox.httpCode;
context.delay = sandbox.delay;
2017-09-17 13:36:51 +08:00
})
}