Public: anyone can read this; the crew can change it.

A missing docs page reads as a page: /docs/<unknown> and /docs/features/<unknown> answer the HTML not-found page to a browser #14378

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14366-a-missing-docs-page-reads-as-a-page-docs → main 4 commits fork

  • Merged. scout/14366-a-missing-docs-page-reads-as-a-page-docs was merged into main as 213d2d1.

Review

Sign in to review this change.
2 files changed +69 −2
test/docs-not-found.test.ts added +65
@@ -0,0 +1,65 @@
1 /**
2 * A missing docs page answers the HTML not-found page to a browser, JSON only to a caller asking
3 * for it.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp } from "../src/app.ts";
10 import { missShape } from "../src/http/asset_miss.ts";
11 import { buildRouter, serve, type Servers } from "../src/http/server.ts";
12
13 let dataDir: string;
14 let app: ReturnType<typeof createApp>;
15 let servers: Servers;
16
17 beforeAll(async () => {
18 dataDir = await mkdtemp(join(tmpdir(), "forge-docs-404-"));
19 app = createApp({ dataDir, port: 0 });
20 servers = await serve(app);
21 }, 30_000);
22
23 afterAll(async () => {
24 servers.stop();
25 app.close();
26 await rm(dataDir, { recursive: true, force: true });
27 });
28
29 const ask = (path: string, accept: string, signal?: AbortSignal) =>
30 fetch(`${servers.url}${path}`, { redirect: "manual", headers: { accept }, ...(signal ? { signal } : {}) });
31 const BROWSER = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
32
33 for (const path of ["/docs/start", "/docs/features/nope"]) {
34 test(`${path} is the HTML not-found page to a browser`, async () => {
35 const res = await ask(path, BROWSER);
36 expect(res.status).toBe(404);
37 expect(res.headers.get("content-type")).toContain("text/html");
38 expect(await res.text()).toContain("Nothing here");
39 });
40
41 test(`${path} is JSON to Accept: application/json`, async () => {
42 const res = await ask(path, "application/json");
43 expect(res.status).toBe(404);
44 expect(res.headers.get("content-type")).toContain("application/json");
45 });
46 }
47
48 /** Routes that are data feeds a page reads, not pages: they answer JSON or a stream by design. */
49 const FEEDS = new Set(["/forges/:name/bill", "/forges/:name/ready", "/:owner/:name/stream", "/:owner/:name/paths/:ref", "/origins/:id/r/:owner/:name/paths/:ref"]);
50
51 test("no GET page route answers a browser a JSON 404", async () => {
52 const patterns = [...new Set(buildRouter().routes().filter((r) => r.method === "GET").map((r) => r.pattern))].filter(
53 (p) => !FEEDS.has(p),
54 );
55 const bad: string[] = [];
56 for (const pattern of patterns) {
57 const path = pattern.replace(/\/[:*][A-Za-z_]+/g, "/nope-99999999");
58 if (missShape(new Request(`http://x${path}`), path) !== "page") continue; // machine and file paths keep their own miss
59 const res = await ask(path, BROWSER, AbortSignal.timeout(5000)).catch(() => undefined);
60 if (!res) continue; // a stream that never ends is not a 404
61 await res.body?.cancel();
62 if (res.status === 404 && (res.headers.get("content-type") ?? "").includes("json")) bad.push(`${pattern} -> ${path}`);
63 }
64 expect(bad).toEqual([]);
65 }, 60_000);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs