cloudflare/cloudflare-typescript

Public

mirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v7

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/base64.test.ts

80lines · modecode

1import { fromBase64, toBase64 } from 'cloudflare/internal/utils/base64';
2
3describe.each(['Buffer', 'atob'])('with %s', (mode) => {
4 let originalBuffer: BufferConstructor;
5 beforeAll(() => {
6 if (mode === 'atob') {
7 originalBuffer = globalThis.Buffer;
8 // @ts-expect-error Can't assign undefined to BufferConstructor
9 delete globalThis.Buffer;
10 }
11 });
12 afterAll(() => {
13 if (mode === 'atob') {
14 globalThis.Buffer = originalBuffer;
15 }
16 });
17 test('toBase64', () => {
18 const testCases = [
19 {
20 input: 'hello world',
21 expected: 'aGVsbG8gd29ybGQ=',
22 },
23 {
24 input: new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]),
25 expected: 'aGVsbG8gd29ybGQ=',
26 },
27 {
28 input: undefined,
29 expected: '',
30 },
31 {
32 input: new Uint8Array([
33 229, 102, 215, 230, 65, 22, 46, 87, 243, 176, 99, 99, 31, 174, 8, 242, 83, 142, 169, 64, 122, 123,
34 193, 71,
35 ]),
36 expected: '5WbX5kEWLlfzsGNjH64I8lOOqUB6e8FH',
37 },
38 {
39 input: '✓',
40 expected: '4pyT',
41 },
42 {
43 input: new Uint8Array([226, 156, 147]),
44 expected: '4pyT',
45 },
46 ];
47
48 testCases.forEach(({ input, expected }) => {
49 expect(toBase64(input)).toBe(expected);
50 });
51 });
52
53 test('fromBase64', () => {
54 const testCases = [
55 {
56 input: 'aGVsbG8gd29ybGQ=',
57 expected: new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]),
58 },
59 {
60 input: '',
61 expected: new Uint8Array([]),
62 },
63 {
64 input: '5WbX5kEWLlfzsGNjH64I8lOOqUB6e8FH',
65 expected: new Uint8Array([
66 229, 102, 215, 230, 65, 22, 46, 87, 243, 176, 99, 99, 31, 174, 8, 242, 83, 142, 169, 64, 122, 123,
67 193, 71,
68 ]),
69 },
70 {
71 input: '4pyT',
72 expected: new Uint8Array([226, 156, 147]),
73 },
74 ];
75
76 testCases.forEach(({ input, expected }) => {
77 expect(fromBase64(input)).toEqual(expected);
78 });
79 });
80});
81