From 561a4994e7cfaaf2acd0de8967268d89c1e8c620 Mon Sep 17 00:00:00 2001 From: LiHS Date: Tue, 17 Jan 2023 16:23:05 +0800 Subject: [PATCH] fix entry encode not work with empty key string and add test cases --- CHANGELOG.md | 3 +++ qiniu/util.js | 6 +++++- test/util.test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef6c61d..3eefaa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ ## CHANGE LOG +## NEXT VERSION +- 对象存储,修复无法对 key 为空字符串的对象进行操作 + ## 7.8.0 - 移除不推荐域名,并增加 亚太-首尔 和 华东-浙江2 固定区域 - RTC,优化请求失败的错误信息 diff --git a/qiniu/util.js b/qiniu/util.js index b4cb6be..42425ae 100644 --- a/qiniu/util.js +++ b/qiniu/util.js @@ -42,7 +42,11 @@ exports.formatDateUTC = function (date, layout) { // Encoded Entry exports.encodedEntry = function (bucket, key) { - return exports.urlsafeBase64Encode(bucket + (key ? ':' + key : '')); + let strToEncode = bucket; + if (key !== undefined) { + strToEncode += ':' + key; + } + return exports.urlsafeBase64Encode(strToEncode); }; // Get accessKey from uptoken diff --git a/test/util.test.js b/test/util.test.js index 401850a..ef7b4fa 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -328,5 +328,47 @@ describe('test util functions', function () { ); } }); + + it('test encodedEntry', function () { + const caseList = [ + { + msg: 'normal', + bucket: 'qiniuphotos', + key: 'gogopher.jpg', + expect: 'cWluaXVwaG90b3M6Z29nb3BoZXIuanBn' + }, + { + msg: 'key empty', + bucket: 'qiniuphotos', + key: '', + expect: 'cWluaXVwaG90b3M6' + }, + { + msg: 'key undefined', + bucket: 'qiniuphotos', + key: undefined, + expect: 'cWluaXVwaG90b3M=' + }, + { + msg: 'key need replace plus symbol', + bucket: 'qiniuphotos', + key: '012ts>a', + expect: 'cWluaXVwaG90b3M6MDEydHM-YQ==' + }, + { + msg: 'key need replace slash symbol', + bucket: 'qiniuphotos', + key: '012ts?a', + expect: 'cWluaXVwaG90b3M6MDEydHM_YQ==' + } + ]; + + for (let i = 0; i < caseList.length; i++) { + const actual = qiniu.util.encodedEntry(caseList[i].bucket, caseList[i].key); + const expect = caseList[i].expect; + const msg = caseList[i].msg; + should.equal(actual, expect, msg); + } + }); }); });