From 7a88e9de370cf954cd2f4d46aac4ffc7741d67f4 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Fri, 20 Jul 2018 22:11:41 +0800 Subject: [PATCH] fix(h5test): support batch request to resolve #188 --- bin/tsw/util/h5-test/add.js | 83 ++++++--- bin/tsw/util/h5-test/del.js | 51 +++--- bin/tsw/util/h5-test/page/src/new.tmpl.html | 76 ++++---- bin/tsw/util/h5-test/page/tmpl.js | 4 +- test/bin/tsw/util/h5-test/api.test.js | 184 ++++++++++++++++++++ 5 files changed, 312 insertions(+), 86 deletions(-) create mode 100644 test/bin/tsw/util/h5-test/api.test.js diff --git a/bin/tsw/util/h5-test/add.js b/bin/tsw/util/h5-test/add.js index 89716195..02f5b79d 100644 --- a/bin/tsw/util/h5-test/add.js +++ b/bin/tsw/util/h5-test/add.js @@ -16,27 +16,31 @@ const OALogin = require('util/oa-login/index.js'); const gzipHttp = require('util/gzipHttp.js'); const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; -module.exports = function(request, response) { - OALogin.checkLoginForTSW(request, response, function() { +module.exports = (request, response) => { + OALogin.checkLoginForTSW(request, response, () => { module.exports.go(request, response); }); }; -module.exports.go = async function(request, response) { +module.exports.go = async (request) => { const uin = request.param('uin'); const val = request.param('val'); + const map = request.param('uinval'); + let data; - const data = await module.exports.addTestUser(uin, val).toES6Promise().catch(function() { - return null; - }); + if (map && typeof map === 'object') { + data = await module.exports.addTestUsers(map).toES6Promise().catch(() => null); + } else { + data = await module.exports.addTestUser(uin, val).toES6Promise().catch(() => null); + } const result = { code: 0, data: data }; returnJson(result); }; -const returnJson = function(json) { +const returnJson = json => { const gzip = gzipHttp.create({ contentType: 'application/json; charset=UTF-8', code: 200 @@ -46,11 +50,35 @@ const returnJson = function(json) { gzip.end(); }; -module.exports.addTestUser = function(uin, val) { - logger.debug('addTestUser:' + uin); - val = val || true; - const memcached = isTest.cmem(); +/** + * 设置测试环境 + * @param uin + * @param val + * @returns {void | * | Promise | Promise} + */ +module.exports.addTestUser = (uin, val) => { + if (!uin) return Deferred.create().reject(); + const users = {}; + users[uin] = val; + return module.exports.addTestUsers(users); +}; + +/** + * 批量设置测试环境 + * @param map 批量设置列表 key 是 uin,map[key] 是环境 + * @returns {void | * | Promise | Promise} + */ +module.exports.addTestUsers = (map) => { + const uins = Object.keys(map); + logger.debug('addTestUsers:' + uins); const defer = Deferred.create(); + + const memcached = isTest.cmem(); + + if (!memcached) { + return defer.reject('memcached not exists'); + } + const appid = context.appid || ''; const appkey = context.appkey; let keyText = isTest.keyBitmap(); @@ -60,24 +88,21 @@ module.exports.addTestUser = function(uin, val) { keyText = `${keyText}.${appid}`; } - if (!uin) { + if (!uins.length) { return defer.reject(); } - if (!canIuse.test(uin)) { - return defer.reject(); - } - - if (!val) { - return defer.reject(); - } + for (let i = 0; i < uins.length; i++) { + if (!canIuse.test(uins[i])) { + return defer.reject(); + } - if (!memcached) { - return defer.reject('memcached not exists'); + if (!map[uins[i]]) { + return defer.reject(); + } } - - memcached.get(keyText, function(err, data) { + memcached.get(keyText, (err, data) => { if (appid && typeof data === 'string') { // 解密 @@ -99,16 +124,20 @@ module.exports.addTestUser = function(uin, val) { text = {}; } - text[uin] = val; + for (let i = 0; i < uins.length; i++) { + const uin = uins[i]; + const val = map[uin]; + text[uin] = val; - logger.debug(`setKeyText: ${uin}; value: ${val}`); + logger.debug(`setKeyText: ${uin}; value: ${val}`); + } if (appid) { // 加密 text = post.encode(appid, appkey, text); } - memcached.set(keyText, text, expire, function(err, ret) { + memcached.set(keyText, text, expire, (err) => { if (err) { defer.reject('memcache set data error'); } else { @@ -116,8 +145,8 @@ module.exports.addTestUser = function(uin, val) { defer.resolve(); } }); - }); + return defer; }; diff --git a/bin/tsw/util/h5-test/del.js b/bin/tsw/util/h5-test/del.js index c529fd00..b08be8af 100644 --- a/bin/tsw/util/h5-test/del.js +++ b/bin/tsw/util/h5-test/del.js @@ -17,28 +17,26 @@ const gzipHttp = require('util/gzipHttp.js'); const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; -module.exports = function(request, response) { - OALogin.checkLoginForTSW(request, response, function() { +module.exports = (request, response) => { + OALogin.checkLoginForTSW(request, response, () => { module.exports.go(request, response); }); }; -module.exports.go = async function(request, response) { +module.exports.go = async request => { const uin = request.param('uin'); const data = await module.exports.deleteTestUser( uin - ).toES6Promise().catch(function() { - return null; - }); + ).toES6Promise().catch(() => null); const result = { code: 0, data: data }; returnJson(result); }; -const returnJson = function(json) { +const returnJson = json => { const gzip = gzipHttp.create({ contentType: 'application/json; charset=UTF-8', code: 200 @@ -48,20 +46,25 @@ const returnJson = function(json) { gzip.end(); }; +module.exports.deleteTestUser = uin => { + uin = Array.isArray(uin) ? uin : [uin]; + return module.exports.deleteTestUsers([uin]); +}; -module.exports.deleteTestUser = function(uin) { - logger.debug('deleteTestUser:' + uin); +/** + * 批量删除测试环境 + * @param uins + * @returns {void | * | Promise | Promise} + */ +module.exports.deleteTestUsers = uins => { + logger.debug('deleteTestUser:' + uins); const memcached = isTest.cmem(); const defer = Deferred.create(); const appid = context.appid || ''; const appkey = context.appkey; let keyText = isTest.keyBitmap(); - if (!uin) { - return defer.reject(); - } - - if (!canIuse.test(uin)) { + if (!uins || uins.length === 0) { return defer.reject(); } @@ -74,7 +77,13 @@ module.exports.deleteTestUser = function(uin) { return defer.reject('memcached not exists'); } - memcached.get(keyText, function(err, data) { + for (let i = 0; i < uins.length; i++) { + if (!uins[i] || !canIuse.test(uins[i])) { + return defer.reject(); + } + } + + memcached.get(keyText, (err, data) => { if (appid && typeof data === 'string') { // 解密 @@ -96,18 +105,20 @@ module.exports.deleteTestUser = function(uin) { return defer.resolve(); } - if (text[uin]) { - delete text[uin]; + for (let i = 0; i < uins.length; i++) { + const uin = uins[i]; + if (text[uin]) { + delete text[uin]; + } + logger.debug('deleteKeyText:' + uin); } - logger.debug('deleteKeyText:' + uin); - if (appid) { // 加密 text = post.encode(appid, appkey, text); } - memcached.set(keyText, text, expire, function(err, ret) { + memcached.set(keyText, text, expire, err => { if (err) { defer.reject('memcache del data error'); } else { diff --git a/bin/tsw/util/h5-test/page/src/new.tmpl.html b/bin/tsw/util/h5-test/page/src/new.tmpl.html index 3fcefc9d..fd83d748 100644 --- a/bin/tsw/util/h5-test/page/src/new.tmpl.html +++ b/bin/tsw/util/h5-test/page/src/new.tmpl.html @@ -57,6 +57,30 @@ return defer; }; + _api.addTestUsers = function(params){ + var defer =$.Deferred(); + $.ajax({ + url: '/api/h5test/add', + type: 'POST', + contentType : "application/json", + dataType : "json", + data: JSON.stringify($.extend({},commomData,{ + uinval: params + })), + success: function(data){ + if(data && data.code === 0 ) { + defer.resolve(data); + }else { + defer.reject( data && data.msg || "系统繁忙,请稍后再试"); + } + }, + fail: function(err){ + defer.reject( err && err.msg || "系统繁忙,请稍后再试"); + } + }); + + return defer; + }; /* * 删除白名单 @@ -122,57 +146,35 @@