Repository Public
1import { describe, expect, it } from "vitest";2import { base64urlToBytes, bytesToBase64url } from "./base64url";34describe("base64url", () => {5 it("encodes without padding and with the URL alphabet", () => {6 expect(bytesToBase64url(new Uint8Array([0xfb, 0xff, 0xfe]))).toBe("-__-");7 expect(bytesToBase64url(new Uint8Array([1]))).toBe("AQ");8 expect(bytesToBase64url(new Uint8Array([1, 2]))).toBe("AQI");9 expect(bytesToBase64url(new Uint8Array([]))).toBe("");10 });11 it("round-trips every byte value and every length modulo 3", () => {12 for (const n of [0, 1, 2, 3, 31, 32, 33, 255, 256, 70000]) {13 const bytes = new Uint8Array(n).map((_, i) => (i * 37 + 11) & 0xff);14 expect([...base64urlToBytes(bytesToBase64url(bytes))]).toEqual([...bytes]);15 }16 });17 it("accepts ArrayBuffer and views with an offset", () => {18 const buf = new Uint8Array([9, 8, 7, 6, 5]).buffer;19 expect(bytesToBase64url(buf)).toBe("CQgHBgU");20 expect(bytesToBase64url(new Uint8Array(buf, 1, 3))).toBe("CAcG");21 expect(bytesToBase64url(new DataView(buf, 3))).toBe("BgU");22 });23 it("decodes padded and standard-alphabet input, returns a real ArrayBuffer", () => {24 expect([...base64urlToBytes("AQI=")]).toEqual([1, 2]);25 expect([...base64urlToBytes("+/8=")]).toEqual([0xfb, 0xff]);26 expect(base64urlToBytes("AQ").buffer).toBeInstanceOf(ArrayBuffer);27 });28 it("rejects malformed input", () => {29 expect(() => base64urlToBytes("a b")).toThrow(TypeError);30 expect(() => base64urlToBytes("A")).toThrow(TypeError);31 expect(() => base64urlToBytes("<script>")).toThrow(TypeError);32 });33});34