formats.exposed/test/common/base64url.test.ts

48 lines
1.1 KiB
TypeScript

import { assertEquals } from "assert";
import * as base64url from "../../public/common/base64url.js";
/**
* A shorthand for `new Uint8Array()`.
*/
const b = (...bytes: number[]) => new Uint8Array(bytes);
Deno.test('encodes no bytes as ""', () => {
assertEquals(base64url.stringify(b()), "");
});
Deno.test("encodes in a URL-safe way", () => {
const input = b(105, 183, 62, 249, 215, 191, 254);
assertEquals(
base64url.stringify(input),
"abc--de__g",
);
});
Deno.test("decodes the empty string", () => {
assertEquals(base64url.parse(""), b());
});
Deno.test("decodes URL-safe strings", () => {
assertEquals(base64url.parse("_-o"), b(255, 234));
});
Deno.test("decodes URL-unsafe strings (as a bonus)", () => {
assertEquals(base64url.parse("/+o="), b(255, 234));
});
Deno.test("round-trips", () => {
const testCases = [
b(),
b(1),
b(1, 2, 3),
b(255, 234),
b(105, 183, 62, 249, 215, 191, 254),
];
for (const original of testCases) {
const string = base64url.stringify(original);
const parsed = base64url.parse(string);
assertEquals(parsed, original, `Round-trip failed for ${original}`);
}
});