diff --git a/lib/server/mock.js b/lib/server/mock.js index 67b6d82..1f5eb98 100644 --- a/lib/server/mock.js +++ b/lib/server/mock.js @@ -35,7 +35,14 @@ const mockMiddleware = async (ctx, next) => { return next(); } - const { data, header, status, delay } = await getMockData(api, ctx); + const { data, header, status, delay, selfHandle } = await getMockData( + api, + ctx + ); + + if (selfHandle) { + return; + } const dataType = Object.prototype.toString.call(data); if (dataType === '[object Uint8Array]' || dataType === '[object String]') { @@ -131,16 +138,18 @@ function matchPathWithRegex(mocks, id, ctx) { */ async function getMockData(api, ctx) { let { data, header, status, delay } = api; - + let selfHandle = false; if (typeof data === 'function') { const dataRes = await data(ctx); data = dataRes.data; + selfHandle = !!dataRes.selfHandle; dataRes.header && (header = Object.assign({}, header, dataRes.header)); dataRes.status && (status = dataRes.status); dataRes.delay && (delay = dataRes.delay); } return { + selfHandle, data, header, status, diff --git a/package.json b/package.json index 5b96f5b..33d4827 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mock-server-local", - "version": "2.2.1", + "version": "2.3.0", "description": "Mock your apis with a node server", "main": "index.js", "bin": { diff --git a/test/test.request.js b/test/test.request.js index bab7e7e..a24ce17 100644 --- a/test/test.request.js +++ b/test/test.request.js @@ -295,3 +295,32 @@ test('mock with restful api 2', () => { server.close(); }); }); + +test('mock with self handle', () => { + const data = { code: 1 }; + getStatus.mockImplementationOnce(() => { + return { + mock: { + 'api.mock.com/api/login': [ + { + data: ctx => { + ctx.body = data; + return { selfHandle: true }; + }, + ...defaultConfig + } + ], + _set: {} + }, + mockChecked: { 'api.mock.com/api/login': 0 }, + setChecked: null + }; + }); + const { instance, server } = setEnv(8089); + + return instance.get('/api/login').then(res => { + expect(res.status).toBe(200); + expect(res.data).toEqual(data); + server.close(); + }); +});