formats.exposed/test/index/fileRouter.test.ts

30 lines
977 B
TypeScript

import { assertEquals } from "assert";
import { routeFile } from "../../public/index/fileRouter.js";
Deno.test("routes files correctly", async () => {
const PNG_SIGNATURE = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
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],
// 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})`,
);
}
});