diff --git a/CHANGELOG.md b/CHANGELOG.md index e1c349c1..e46613d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ #### Bug Fixed * 接口path中写入 ?name=xxx bug +* 高级mock 匹配 data: [{item: XXX}] 时匹配不成功 ### v1.3.22 diff --git a/common/lib.js b/common/lib.js index a65befba..7bc08553 100644 --- a/common/lib.js +++ b/common/lib.js @@ -18,6 +18,9 @@ function getLength(object) { function Compare(objA, objB) { if (!isObj(objA) && !isObj(objB)) { + if (isArray(objA) && isArray(objB)) { + return CompareArray(objA, objB, true); + } return objA == objB; } if (!isObj(objA) || !isObj(objB)) return false; @@ -25,6 +28,18 @@ function Compare(objA, objB) { return CompareObj(objA, objB, true); } +function CompareArray(objA, objB, flag) { + if (objA.length != objB.length) return false; + for (let i in objB) { + if (!Compare(objA[i], objB[i])) { + flag = false; + break; + } + } + + return flag; +} + function CompareObj(objA, objB, flag) { for (var key in objA) { if (!flag) break; @@ -60,6 +75,7 @@ function CompareObj(objA, objB, flag) { exports.jsonEqual = Compare; exports.isDeepMatch = function(obj, properties) { + if (!properties || typeof properties !== 'object' || Object.keys(properties).length === 0) { return true; } diff --git a/docs/documents/plugin-hooks.md b/docs/documents/plugin-hooks.md index 313ff7c3..d181d7aa 100644 --- a/docs/documents/plugin-hooks.md +++ b/docs/documents/plugin-hooks.md @@ -26,7 +26,7 @@ var hooks = { }, /** * 客户端删除接口成功后触发 - * @param data 删除接口的详细信息 + * @param data 接口id */ interface_del: { type: 'multi', diff --git a/server/controllers/interface.js b/server/controllers/interface.js index 26817c7b..22f32340 100755 --- a/server/controllers/interface.js +++ b/server/controllers/interface.js @@ -721,7 +721,7 @@ class interfaceController extends baseController { // let inter = await this.Model.get(id); let result = await this.Model.del(id); - yapi.emitHook('interface_del', data).then(); + yapi.emitHook('interface_del', id).then(); await this.caseModel.delByInterfaceId(id); let username = this.getUsername(); this.catModel.get(data.catid).then(cate => { @@ -890,7 +890,7 @@ class interfaceController extends baseController { interfaceData.forEach(async item => { try { - yapi.emitHook('interface_del', item).then(); + yapi.emitHook('interface_del', item._id).then(); await this.caseModel.delByInterfaceId(item._id); } catch (e) { yapi.commons.log(e.message, 'error'); diff --git a/server/controllers/test.js b/server/controllers/test.js index ff6e893f..97d61f58 100644 --- a/server/controllers/test.js +++ b/server/controllers/test.js @@ -148,7 +148,8 @@ class interfaceColController extends baseController { async testDelete(ctx) { try { let params = ctx.request.query; - ctx.body = yapi.commons.resReturn(params); + let body = ctx.request.body; + ctx.body = yapi.commons.resReturn(body); } catch (e) { ctx.body = yapi.commons.resReturn(null, 402, e.message); } diff --git a/server/plugin.js b/server/plugin.js index ca36f229..38b5bb6f 100755 --- a/server/plugin.js +++ b/server/plugin.js @@ -27,7 +27,7 @@ var hooks = { }, /** * 客户端删除接口成功后触发 - * @param data 删除接口的详细信息 + * @param data 接口id */ interface_del: { type: 'multi', diff --git a/test/lib.test.js b/test/lib.test.js index c62a5891..31b38834 100644 --- a/test/lib.test.js +++ b/test/lib.test.js @@ -165,4 +165,16 @@ test('isDeepMatch', t=>{ params: { a: 'x', b: 'y' }, res_body: '111', code: 1 }, {t:'1'})) -}) \ No newline at end of file +}) + +test('isDeepMatch', t=>{ + t.true(lib.isDeepMatch({ t:[{a: 1}]}, { t:[{a: 1}]})) + }) + + test('isDeepMatch', t=>{ + t.false(lib.isDeepMatch({ t:[{a: 1, b: 12}]}, { t:[{a: 1}]})) + }) + + test('isDeepMatch', t=>{ + t.true(lib.isDeepMatch([{a: 1}], [{a: 1}])) + }) \ No newline at end of file