Skip to content

Commit 8a6338e

Browse files
committed
Add _encodeURIComponent unit tests
Signed-off-by: Elijah Zupancic <[email protected]>
1 parent ecbdd11 commit 8a6338e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

test/unit/s3gateway_test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,50 @@ fakeRequest.log = function(msg) {
4848
console.log(msg);
4949
}
5050

51+
function testEncodeURIComponent() {
52+
printHeader('testEncodeURIComponent');
53+
function testPureAsciiAlphaNum() {
54+
console.log(' ## testPureAsciiAlphaNum');
55+
let alphaNum = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
56+
let encoded = s3gateway._encodeURIComponent(alphaNum);
57+
if (alphaNum !== alphaNum) {
58+
throw 'Incorrect encoding applied to string.\n' +
59+
`Actual: [${encoded}]\n` +
60+
`Expected: [${expected}]`;
61+
}
62+
}
63+
function testUnicodeText() {
64+
console.log(' ## testUnicodeText');
65+
let unicode = 'これは This is ASCII системы חן ';
66+
let expected = '%E3%81%93%E3%82%8C%E3%81%AF%E3%80%80This%20is%20ASCII%20%D1%81%D0%B8%D1%81%D1%82%D0%B5%D0%BC%D1%8B%20%20%D7%97%D7%9F%20';
67+
let encoded = s3gateway._encodeURIComponent(unicode);
68+
if (expected !== encoded) {
69+
throw 'Incorrect encoding applied to string.\n' +
70+
`Actual: [${encoded}]\n` +
71+
`Expected: [${expected}]`;
72+
}
73+
}
74+
function testDiceyCharactersInText() {
75+
console.log(' ## testDiceyCharactersInText');
76+
77+
let diceyCharacters = '%@!*()=+$#^&|\\/';
78+
for (let i = 0; i < diceyCharacters.length; i++) {
79+
let char = diceyCharacters[i];
80+
let encoded = s3gateway._encodeURIComponent(char);
81+
let expected = `%${char.charCodeAt(0).toString(16).toUpperCase()}`;
82+
if (encoded !== expected) {
83+
throw 'Incorrect encoding applied to string.\n' +
84+
`Actual: [${encoded}]\n` +
85+
`Expected: [${expected}]`;
86+
}
87+
}
88+
}
89+
90+
testPureAsciiAlphaNum();
91+
testUnicodeText();
92+
testDiceyCharactersInText();
93+
}
94+
5195
function testPad() {
5296
printHeader('testPad');
5397
var padSingleDigit = s3gateway._padWithLeadingZeros(3, 2);
@@ -568,6 +612,7 @@ function printHeader(testName) {
568612
}
569613

570614
async function test() {
615+
testEncodeURIComponent();
571616
testPad();
572617
testEightDigitDate();
573618
testAmzDatetime();

0 commit comments

Comments
 (0)