Files
ia-fit/tests/server/static.test.js
T

72 lines
3.4 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import zlib from "node:zlib";
import { CSP } from "../../server/http.js";
import { pickEncoding } from "../../server/static.js";
import { startServer } from "./helpers.js";
test("site : types MIME, en-têtes du lot 1, revalidation", async () => {
const s = await startServer();
try {
for (const [p, type] of [["/", "text/html; charset=utf-8"], ["/app.js", "text/javascript; charset=utf-8"], ["/ia.md", "text/markdown; charset=utf-8"], ["/police.woff2", "font/woff2"]]) {
const r = await s.fetch(p);
assert.equal(r.status, 200, p);
assert.equal(r.headers.get("content-type"), type, p);
assert.equal(r.headers.get("content-security-policy"), CSP);
assert.equal(r.headers.get("x-content-type-options"), "nosniff");
assert.equal(r.headers.get("referrer-policy"), "no-referrer");
assert.equal(r.headers.get("cache-control"), "no-cache");
}
const first = await s.fetch("/app.js", { headers: { "accept-encoding": "identity" } });
const etag = first.headers.get("etag") ?? "";
assert.equal((await s.fetch("/app.js", { headers: { "if-none-match": etag, "accept-encoding": "identity" } })).status, 304);
// Les API ne sont jamais mises en cache.
assert.equal((await s.fetch("/.well-known/oauth-authorization-server")).headers.get("cache-control"), "no-store");
} finally {
await s.close();
}
});
test("site : compression zstd ou gzip des textes, pas des polices", async () => {
const s = await startServer();
try {
const raw = fs.readFileSync(path.join(s.siteDir, "app.js"));
// fetch décompresse de lui-même : on lit donc les en-têtes, puis le contenu par node:http.
const http = await import("node:http");
/** @param {string} p @param {string} enc */
const get = (p, enc) => new Promise((resolve) => http.get(s.base + p, { headers: { "accept-encoding": enc } }, (res) => {
/** @type {Buffer[]} */ const chunks = [];
res.on("data", (c) => chunks.push(c));
res.on("end", () => resolve({ headers: res.headers, body: Buffer.concat(chunks) }));
}));
const z = /** @type {any} */ (await get("/app.js", "gzip, deflate, br, zstd"));
assert.equal(z.headers["content-encoding"], "zstd");
assert.deepEqual(zlib.zstdDecompressSync(z.body), raw);
const g = /** @type {any} */ (await get("/app.js", "gzip"));
assert.equal(g.headers["content-encoding"], "gzip");
assert.deepEqual(zlib.gunzipSync(g.body), raw);
const f = /** @type {any} */ (await get("/police.woff2", "gzip"));
assert.equal(f.headers["content-encoding"], undefined);
assert.equal(pickEncoding("gzip;q=0, zstd;q=0"), null);
assert.equal(pickEncoding("*"), "zstd");
assert.equal(pickEncoding(undefined), null);
} finally {
await s.close();
}
});
test("site : 404 pour l'absent, les fichiers cachés et les remontées ; méthodes limitées", async () => {
const s = await startServer();
try {
fs.writeFileSync(path.join(s.siteDir, ".cache"), "secret");
for (const p of ["/absent.html", "/.cache", "/%2e%2e/data/.server-secret", "/..%2fdata%2f.server-secret"])
assert.equal((await s.fetch(p)).status, 404, p);
assert.equal((await s.fetch("/", { method: "POST" })).status, 405);
assert.equal((await s.fetch("/app.js", { method: "HEAD" })).status, 200);
} finally {
await s.close();
}
});