|
| 1 | +import assert from "node:assert"; |
| 2 | +import { describe, it, beforeEach, afterEach } from "node:test"; |
| 3 | +import { Server } from "node:http"; |
| 4 | +import { resolve } from "node:path"; |
| 5 | +import { readFile } from "node:fs/promises"; |
| 6 | +import { AppServer } from "../../src/server/server"; |
| 7 | +import { testAppServerSetup, getTestHost } from "./config"; |
| 8 | + |
| 9 | +describe("staticHandler", () => { |
| 10 | + let app: AppServer; |
| 11 | + let server: Server | null = null; |
| 12 | + const publicRoot = resolve(process.cwd(), "test", "handlers", "public"); |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + app = new AppServer({ ...testAppServerSetup, config: { publicRoot, port: 0 } }); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(async () => { |
| 19 | + if (server) { |
| 20 | + await new Promise((resolve) => server!.close(resolve)); |
| 21 | + server = null; |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + it("should serve index.html for /", async () => { |
| 26 | + server = await app.start(); |
| 27 | + const host = getTestHost(server); |
| 28 | + |
| 29 | + const res = await fetch(`${host}/`); |
| 30 | + const text = await res.text(); |
| 31 | + const expected = await readFile(resolve(publicRoot, "index.html"), "utf8"); |
| 32 | + |
| 33 | + assert.strictEqual(res.status, 200); |
| 34 | + assert.strictEqual(res.headers.get("content-type"), "text/html"); |
| 35 | + assert.strictEqual(text.trim(), expected.trim()); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should serve a static text file", async () => { |
| 39 | + server = await app.start(); |
| 40 | + const host = getTestHost(server); |
| 41 | + |
| 42 | + const res = await fetch(`${host}/test/test.txt`); |
| 43 | + const text = await res.text(); |
| 44 | + const expected = await readFile(resolve(publicRoot, "test", "test.txt"), "utf8"); |
| 45 | + |
| 46 | + assert.strictEqual(res.status, 200); |
| 47 | + assert.strictEqual(res.headers.get("content-type"), "text/plain"); |
| 48 | + assert.strictEqual(text.trim(), expected.trim()); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should return 404 for missing file", async () => { |
| 52 | + server = await app.start(); |
| 53 | + const host = getTestHost(server); |
| 54 | + |
| 55 | + const res = await fetch(`${host}/no-such-file.xyz`); |
| 56 | + |
| 57 | + assert.strictEqual(res.status, 404); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should prevent path traversal attempts", async () => { |
| 61 | + server = await app.start(); |
| 62 | + const host = getTestHost(server); |
| 63 | + |
| 64 | + const res = await fetch(`${host}/../../.env`); |
| 65 | + |
| 66 | + assert.strictEqual(res.status, 404); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should return 404 when accessing not existing directory", async () => { |
| 70 | + server = await app.start(); |
| 71 | + const host = getTestHost(server); |
| 72 | + |
| 73 | + const res = await fetch(`${host}/folder`); |
| 74 | + |
| 75 | + assert.strictEqual(res.status, 404); |
| 76 | + }); |
| 77 | +}); |
0 commit comments