2023-08-01 02:16:15 +00:00
|
|
|
import { assertEquals } from "assert";
|
2023-08-11 19:42:32 +00:00
|
|
|
import routeFile from "../../public/index/routeFile.js";
|
2023-08-01 02:16:15 +00:00
|
|
|
|
|
|
|
Deno.test("routes files correctly", async () => {
|
2023-08-11 19:58:53 +00:00
|
|
|
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]);
|
2023-08-01 02:16:15 +00:00
|
|
|
|
|
|
|
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],
|
2023-08-11 19:58:53 +00:00
|
|
|
// GIF
|
2023-08-12 03:46:19 +00:00
|
|
|
[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"],
|
2023-08-01 02:16:15 +00:00
|
|
|
// PNG
|
2023-08-12 03:46:19 +00:00
|
|
|
[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"],
|
2023-08-01 02:16:15 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
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})`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|