-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
Copy pathencoding.test.ts
70 lines (55 loc) · 2.16 KB
/
encoding.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
/**
* @license
* Copyright 2018 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
import {describe, it} from 'node:test';
import expect from 'expect';
import {mergeUint8Arrays, stringToTypedArray} from './encoding.js';
describe('Typed Array helpers', function () {
describe('stringToTypedArray', function () {
it('should get body length from empty string', async () => {
const result = stringToTypedArray('');
expect(Buffer.from('').compare(Buffer.from(result))).toBe(0);
});
it('should get body length from latin string', async () => {
const body = 'Lorem ipsum dolor sit amet';
const result = stringToTypedArray(body);
expect(Buffer.from(body).compare(Buffer.from(result))).toBe(0);
});
it('should get body length from string with emoji', async () => {
const body = 'How Long is this string in bytes 📏?';
const result = stringToTypedArray(body);
expect(Buffer.from(body).compare(Buffer.from(result))).toBe(0);
});
it('should get body length from base64', async () => {
const body = btoa('Lorem ipsum dolor sit amet');
const result = stringToTypedArray(body, true);
expect(Buffer.from(body, 'base64').compare(Buffer.from(result))).toBe(0);
});
it('should get body length from base64 containing emoji', async () => {
// 'How Long is this string in bytes 📏?';
const base64 = 'SG93IExvbmcgaXMgdGhpcyBzdHJpbmcgaW4gYnl0ZXMg8J+Tjz8=';
const result = stringToTypedArray(base64, true);
expect(Buffer.from(base64, 'base64').compare(Buffer.from(result))).toBe(
0
);
});
});
describe('mergeUint8Arrays', () => {
it('should work', () => {
const one = new Uint8Array([1]);
const two = new Uint8Array([12]);
const three = new Uint8Array([123]);
expect(mergeUint8Arrays([one, two, three])).toEqual(
new Uint8Array([1, 12, 123])
);
});
it('should work with empty arrays', () => {
const one = new Uint8Array([1]);
const two = new Uint8Array([]);
const three = new Uint8Array([]);
expect(mergeUint8Arrays([one, two, three])).toEqual(new Uint8Array([1]));
});
});
});