feat: 增强 mockServer

This commit is contained in:
suxiaoxin 2017-10-18 20:34:01 +08:00
parent bc88d44350
commit 333be5069f
2 changed files with 19 additions and 2 deletions

View File

@ -103,7 +103,11 @@ module.exports = function(){
header: context.ctx.header,
query: context.ctx.query,
body: context.ctx.request.body,
mockJson: context.mockJson
mockJson: context.mockJson,
params: Object.assign({}, context.ctx.query, context.ctx.request.body),
resHeader: context.resHeader,
httpCode: context.httpCode,
delay: context.httpCode
}
sandbox.cookie = {};
@ -112,6 +116,16 @@ module.exports = function(){
sandbox.cookie[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim();
});
sandbox = yapi.commons.sandbox(sandbox, script);
sandbox.delay = isNaN(sandbox.delay) ? 0 : +sandbox.delay;
let handleMock = new Promise(resolve=>{
setTimeout(()=>{
resolve(true)
}, sandbox.delay)
})
await handleMock;
context.mockJson = sandbox.mockJson;
context.resHeader = sandbox.resHeader;
context.httpCode = sandbox.httpCode;
context.delay = sandbox.delay;
})
}

View File

@ -143,7 +143,10 @@ module.exports = async (ctx, next) => {
projectData: project,
interfaceData: interfaceData,
ctx: ctx,
mockJson: res
mockJson: res,
resHeader: {},
httpCode: 200,
delay: 0
}
await yapi.emitHook('mock_after', context);
return ctx.body = context.mockJson;