Skip to content

Commit ee79498

Browse files
authored
Merge pull request #406 from qiniu:fix/entry-encode
fix entry encode not work with empty key string and add test cases
2 parents a4c329c + 561a499 commit ee79498

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## CHANGE LOG
2+
## NEXT VERSION
3+
- 对象存储,修复无法对 key 为空字符串的对象进行操作
4+
25
## 7.8.0
36
- 移除不推荐域名,并增加 亚太-首尔 和 华东-浙江2 固定区域
47
- RTC,优化请求失败的错误信息

qiniu/util.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ exports.formatDateUTC = function (date, layout) {
4242

4343
// Encoded Entry
4444
exports.encodedEntry = function (bucket, key) {
45-
return exports.urlsafeBase64Encode(bucket + (key ? ':' + key : ''));
45+
let strToEncode = bucket;
46+
if (key !== undefined) {
47+
strToEncode += ':' + key;
48+
}
49+
return exports.urlsafeBase64Encode(strToEncode);
4650
};
4751

4852
// Get accessKey from uptoken

test/util.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -328,5 +328,47 @@ describe('test util functions', function () {
328328
);
329329
}
330330
});
331+
332+
it('test encodedEntry', function () {
333+
const caseList = [
334+
{
335+
msg: 'normal',
336+
bucket: 'qiniuphotos',
337+
key: 'gogopher.jpg',
338+
expect: 'cWluaXVwaG90b3M6Z29nb3BoZXIuanBn'
339+
},
340+
{
341+
msg: 'key empty',
342+
bucket: 'qiniuphotos',
343+
key: '',
344+
expect: 'cWluaXVwaG90b3M6'
345+
},
346+
{
347+
msg: 'key undefined',
348+
bucket: 'qiniuphotos',
349+
key: undefined,
350+
expect: 'cWluaXVwaG90b3M='
351+
},
352+
{
353+
msg: 'key need replace plus symbol',
354+
bucket: 'qiniuphotos',
355+
key: '012ts>a',
356+
expect: 'cWluaXVwaG90b3M6MDEydHM-YQ=='
357+
},
358+
{
359+
msg: 'key need replace slash symbol',
360+
bucket: 'qiniuphotos',
361+
key: '012ts?a',
362+
expect: 'cWluaXVwaG90b3M6MDEydHM_YQ=='
363+
}
364+
];
365+
366+
for (let i = 0; i < caseList.length; i++) {
367+
const actual = qiniu.util.encodedEntry(caseList[i].bucket, caseList[i].key);
368+
const expect = caseList[i].expect;
369+
const msg = caseList[i].msg;
370+
should.equal(actual, expect, msg);
371+
}
372+
});
331373
});
332374
});

0 commit comments

Comments
 (0)