/
base64url_test.ts
73 lines (63 loc) · 1.71 KB
/
base64url_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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows } from "../assert/mod.ts";
import { decodeBase64Url, encodeBase64Url } from "./base64url.ts";
const testsetString = [
["", ""],
["ß", "w58"],
["f", "Zg"],
["fo", "Zm8"],
["foo", "Zm9v"],
["foob", "Zm9vYg"],
["fooba", "Zm9vYmE"],
["foobar", "Zm9vYmFy"],
[">?>d?ß", "Pj8-ZD_Dnw"],
];
const testsetBinary = testsetString.map(([str, b64]) => [
new TextEncoder().encode(str),
b64,
]) as Array<[Uint8Array, string]>;
const testsetInvalid = [
"Pj8/ZD+Dnw",
"PDw/Pz8+Pg",
"Pj8/ZD+Dnw==",
"PDw/Pz8+Pg==",
];
Deno.test("encodeBase64Url() encodes string", () => {
for (const [input, output] of testsetString) {
assertEquals(encodeBase64Url(input!), output);
}
});
Deno.test("encodeBase64Url() encodes binary", () => {
for (const [input, output] of testsetBinary) {
assertEquals(encodeBase64Url(input), output);
}
});
Deno.test("decodeBase64Url() decodes binary", () => {
for (const [input, output] of testsetBinary) {
assertEquals(decodeBase64Url(output), input);
}
});
Deno.test("decodeBase64Url() throws on invalid input", () => {
for (const invalidb64url of testsetInvalid) {
assertThrows(
() => decodeBase64Url(invalidb64url),
TypeError,
"invalid character",
);
}
});
Deno.test("decodeBase64Url() throws on illegal base64url string", () => {
const testsetIllegalBase64url = [
"w58De",
"Zm9vYmFyy",
"DPj8-ZD_DnwEg",
"SGVsbG8gV29ybGQ-_",
];
for (const illegalBase64url of testsetIllegalBase64url) {
assertThrows(
() => decodeBase64Url(illegalBase64url),
TypeError,
"Illegal base64url string!",
);
}
});