import { staticFiles } from "./static_files.ts"; import { serveMiddleware } from "../test_utils.ts"; import type { BuildCache, StaticFile } from "../build_cache.ts"; import { expect } from "@std/expect"; import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx"; import { BUILD_ID } from "../runtime/build_id.ts"; class MockBuildCache implements BuildCache { buildId = "MockId"; files = new Map(); hasSnapshot = true; constructor(files: Record) { const encoder = new TextEncoder(); for (const [pathname, info] of Object.entries(files)) { const text = encoder.encode(info.content); const normalized = pathname.startsWith("/") ? pathname : "/" + pathname; this.files.set(normalized, { hash: info.hash, size: text.byteLength, readable: text, }); } } // deno-lint-ignore require-await async readFile(pathname: string): Promise { return this.files.get(pathname) ?? null; } getIslandChunkName(_islandName: string): string | null { return null; } } Deno.test("static files - 200", async () => { const buildCache = new MockBuildCache({ "foo.css": { content: "body {}", hash: null }, }); const server = serveMiddleware( staticFiles(), { buildCache }, ); const res = await server.get("/foo.css"); const content = await res.text(); expect(res.status).toEqual(200); expect(res.headers.get("Content-Type")).toEqual("text/css; charset=UTF-8"); expect(res.headers.get("Content-Length")).toEqual("7"); expect(res.headers.get("Cache-Control")).toEqual( "no-cache, no-store, max-age=0, must-revalidate", ); expect(content).toEqual("body {}"); }); Deno.test("static files - HEAD 200", async () => { const buildCache = new MockBuildCache({ "foo.css": { content: "body {}", hash: null }, }); const server = serveMiddleware( staticFiles(), { buildCache }, ); const res = await server.head("/foo.css"); const content = await res.text(); expect(res.status).toEqual(200); expect(res.headers.get("Content-Type")).toEqual("text/css; charset=UTF-8"); expect(res.headers.get("Content-Length")).toEqual("7"); expect(content).toEqual(""); }); Deno.test("static files - etag", async () => { const buildCache = new MockBuildCache({ "foo.css": { content: "body {}", hash: "123" }, }); const server = serveMiddleware( staticFiles(), { buildCache }, ); const headers = new Headers(); headers.append("If-None-Match", "123"); const cacheUrl = `/foo.css?${ASSET_CACHE_BUST_KEY}=${BUILD_ID}`; const res = await server.get(cacheUrl, { headers }); await res.body?.cancel(); expect(res.status).toEqual(304); const headers2 = new Headers(); headers2.append("If-None-Match", "W/123"); const res2 = await server.get(cacheUrl, { headers }); await res2.body?.cancel(); expect(res2.status).toEqual(304); }); Deno.test("static files - 404 on missing favicon.ico", async () => { const buildCache = new MockBuildCache({}); const server = serveMiddleware( staticFiles(), { buildCache }, ); const res = await server.get("favicon.ico"); await res.body?.cancel(); expect(res.status).toEqual(404); }); Deno.test("static files - 405 on wrong HTTP method", async () => { const buildCache = new MockBuildCache({ "foo.css": { content: "body {}", hash: null }, }); const server = serveMiddleware( staticFiles(), { buildCache }, ); for (const method of ["post", "patch", "put", "delete"]) { // deno-lint-ignore no-explicit-any const res = await (server as any)[method]("/foo.css"); await res.body?.cancel(); expect(res.status).toEqual(405); } }); Deno.test("static files - disables caching in development", async () => { const buildCache = new MockBuildCache({ "foo.css": { content: "body {}", hash: null }, }); const server = serveMiddleware( staticFiles(), { buildCache, config: { basePath: "", build: { outDir: "", }, mode: "development", root: ".", staticDir: "", }, }, ); const res = await server.get("/foo.css"); await res.body?.cancel(); expect(res.status).toEqual(200); expect(res.headers.get("Cache-Control")).toEqual( "no-cache, no-store, max-age=0, must-revalidate", ); });