formats.exposed/test/index/routeFile.test.ts

47 lines
1.4 KiB
TypeScript

import { assertEquals } from "assert";
import routeFile from "../../public/index/routeFile.js";
Deno.test("routes files correctly", async () => {
const PNG_SIGNATURE = new Uint8Array([
0x89,
0x50,
0x4E,
0x47,
0x0D,
0x0A,
0x1A,
0x0A,
]);
const GIF87_SIGNATURE = new Uint8Array([0x47, 0x49, 0x46, 0x38, 0x37, 0x61]);
const GIF89_SIGNATURE = new Uint8Array([0x47, 0x49, 0x46, 0x38, 0x39, 0x61]);
const testCases = new Map<File, null | string>([
// No matching type
[new File([], "foo"), null],
[new File([], "foo.txt"), null],
[new File([], "foo.txt", { type: "text/plain" }), null],
// GIF
[new File([], "foo", { type: "image/gif" }), "/gif"],
[new File([], "foo.gif"), "/gif"],
[new File([], "foo.gif", { type: "text/plain" }), "/gif"],
[new File([GIF87_SIGNATURE], "foo"), "/gif"],
[new File([GIF89_SIGNATURE], "foo"), "/gif"],
// PNG
[new File([], "foo", { type: "image/png" }), "/png"],
[new File([], "foo.png"), "/png"],
[new File([], "foo.png", { type: "text/plain" }), "/png"],
[new File([PNG_SIGNATURE], "foo"), "/png"],
]);
for (const [file, expected] of testCases) {
const actual = await routeFile(file);
assertEquals(
actual,
expected,
`Expected file (${JSON.stringify(file.name)}, type ${
JSON.stringify(file.type)
}, sized ${file.size}b) to route to ${expected} (got ${actual})`,
);
}
});