-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase64.test.ts
80 lines (76 loc) · 2.02 KB
/
base64.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { fromBase64, toBase64 } from '@gitpod/sdk/internal/utils/base64';
describe.each(['Buffer', 'atob'])('with %s', (mode) => {
let originalBuffer: BufferConstructor;
beforeAll(() => {
if (mode === 'atob') {
originalBuffer = globalThis.Buffer;
// @ts-expect-error Can't assign undefined to BufferConstructor
delete globalThis.Buffer;
}
});
afterAll(() => {
if (mode === 'atob') {
globalThis.Buffer = originalBuffer;
}
});
test('toBase64', () => {
const testCases = [
{
input: 'hello world',
expected: 'aGVsbG8gd29ybGQ=',
},
{
input: new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]),
expected: 'aGVsbG8gd29ybGQ=',
},
{
input: undefined,
expected: '',
},
{
input: new Uint8Array([
229, 102, 215, 230, 65, 22, 46, 87, 243, 176, 99, 99, 31, 174, 8, 242, 83, 142, 169, 64, 122, 123,
193, 71,
]),
expected: '5WbX5kEWLlfzsGNjH64I8lOOqUB6e8FH',
},
{
input: '✓',
expected: '4pyT',
},
{
input: new Uint8Array([226, 156, 147]),
expected: '4pyT',
},
];
testCases.forEach(({ input, expected }) => {
expect(toBase64(input)).toBe(expected);
});
});
test('fromBase64', () => {
const testCases = [
{
input: 'aGVsbG8gd29ybGQ=',
expected: new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]),
},
{
input: '',
expected: new Uint8Array([]),
},
{
input: '5WbX5kEWLlfzsGNjH64I8lOOqUB6e8FH',
expected: new Uint8Array([
229, 102, 215, 230, 65, 22, 46, 87, 243, 176, 99, 99, 31, 174, 8, 242, 83, 142, 169, 64, 122, 123,
193, 71,
]),
},
{
input: '4pyT',
expected: new Uint8Array([226, 156, 147]),
},
];
testCases.forEach(({ input, expected }) => {
expect(fromBase64(input)).toEqual(expected);
});
});
});