-
-
Notifications
You must be signed in to change notification settings - Fork 652
/
Copy pathencode.test.ts
61 lines (55 loc) · 1.98 KB
/
encode.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { decodeBase64Url, encodeBase64Url } from './encode'
const toURLBase64 = (base64String: string): string =>
base64String.replace(/\+|\//g, (m) => ({ '+': '-', '/': '_' }[m] ?? m))
const str2UInt8Array = (s: string): Uint8Array => {
const buffer = new Uint8Array(new ArrayBuffer(s.length))
for (let i = 0, len = buffer.byteLength; i < len; i++) {
buffer[i] = s.charCodeAt(i)
}
return buffer
}
describe('base64', () => {
const utf8Encoder = new TextEncoder()
describe.each([
// basic
[utf8Encoder.encode('Hello, 世界'), 'SGVsbG8sIOS4lueVjA=='],
[utf8Encoder.encode('炎'), '54KO'],
[utf8Encoder.encode('🔥'), '8J+UpQ=='],
[
utf8Encoder.encode('http://github.com/honojs/hono'),
'aHR0cDovL2dpdGh1Yi5jb20vaG9ub2pzL2hvbm8=',
],
// RFC 3548 examples
[str2UInt8Array('\x14\xfb\x9c\x03\xd9\x7e'), 'FPucA9l+'],
[str2UInt8Array('\x14\xfb\x9c\x03\xd9'), 'FPucA9k='],
[str2UInt8Array('\x14\xfb\x9c\x03'), 'FPucAw=='],
// RFC 4648 examples
[str2UInt8Array(''), ''],
[str2UInt8Array('f'), 'Zg=='],
[str2UInt8Array('fo'), 'Zm8='],
[str2UInt8Array('foo'), 'Zm9v'],
[str2UInt8Array('foob'), 'Zm9vYg=='],
[str2UInt8Array('fooba'), 'Zm9vYmE='],
[str2UInt8Array('foobar'), 'Zm9vYmFy'],
// Wikipedia examples
[str2UInt8Array('sure.'), 'c3VyZS4='],
[str2UInt8Array('sure'), 'c3VyZQ=='],
[str2UInt8Array('sur'), 'c3Vy'],
[str2UInt8Array('su'), 'c3U='],
[str2UInt8Array('leasure.'), 'bGVhc3VyZS4='],
[str2UInt8Array('easure.'), 'ZWFzdXJlLg=='],
[str2UInt8Array('asure.'), 'YXN1cmUu'],
[str2UInt8Array('sure.'), 'c3VyZS4='],
])('%s, %s', (stdDecoded, stdEncoded) => {
it('encode', () => {
const got = encodeBase64Url(stdDecoded)
const want = toURLBase64(stdEncoded)
expect(got).toStrictEqual(want)
})
it('decode', () => {
const got = decodeBase64Url(toURLBase64(stdEncoded))
const want = stdDecoded
expect(got).toStrictEqual(want)
})
})
})