2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-10-13 01:12:10 +00:00
|
|
|
|
2024-01-31 09:10:15 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2023-09-21 09:29:13 +00:00
|
|
|
import { decodeBase64, encodeBase64 } from "./base64.ts";
|
2020-05-24 13:10:01 +00:00
|
|
|
|
|
|
|
const testsetString = [
|
|
|
|
["", ""],
|
2020-10-13 01:12:10 +00:00
|
|
|
["ß", "w58="],
|
2020-05-24 13:10:01 +00:00
|
|
|
["f", "Zg=="],
|
|
|
|
["fo", "Zm8="],
|
|
|
|
["foo", "Zm9v"],
|
|
|
|
["foob", "Zm9vYg=="],
|
|
|
|
["fooba", "Zm9vYmE="],
|
|
|
|
["foobar", "Zm9vYmFy"],
|
|
|
|
];
|
|
|
|
|
2020-10-13 01:12:10 +00:00
|
|
|
const testsetBinary = testsetString.map(([str, b64]) => [
|
|
|
|
new TextEncoder().encode(str),
|
|
|
|
b64,
|
|
|
|
]) as Array<[Uint8Array, string]>;
|
2020-05-24 13:10:01 +00:00
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("encodeBase64() encodes string", () => {
|
2020-05-24 13:10:01 +00:00
|
|
|
for (const [input, output] of testsetString) {
|
2023-09-21 09:29:13 +00:00
|
|
|
assertEquals(encodeBase64(input), output);
|
2020-05-24 13:10:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("encodeBase64() encodes binary", () => {
|
2020-10-13 01:12:10 +00:00
|
|
|
for (const [input, output] of testsetBinary) {
|
2023-09-21 09:29:13 +00:00
|
|
|
assertEquals(encodeBase64(input), output);
|
2020-05-24 13:10:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("encodeBase64() encodes binary buffer", () => {
|
2020-05-24 13:10:01 +00:00
|
|
|
for (const [input, output] of testsetBinary) {
|
2023-09-21 09:29:13 +00:00
|
|
|
assertEquals(encodeBase64(input.buffer), output);
|
2020-05-24 13:10:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("decodeBase64() decodes binary", () => {
|
2020-05-24 13:10:01 +00:00
|
|
|
for (const [input, output] of testsetBinary) {
|
2023-09-21 09:29:13 +00:00
|
|
|
const outputBinary = decodeBase64(output);
|
2020-10-13 01:12:10 +00:00
|
|
|
assertEquals(outputBinary, input);
|
2020-05-24 13:10:01 +00:00
|
|
|
}
|
|
|
|
});
|