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

The test files outside api-* read no answer that disagrees with openapi.json #18001

opened this

Merged 12h 7m ago, live in production

mason/forge:mason/17987-the-test-files-outside-api-read-no-answe → main 0 commits fork

  • Merged. mason/17987-the-test-files-outside-api-read-no-answe was merged into main as d861d63.

Review

Sign in to review this change.
2 files changed +25 −4
test/helpers/contract-fetch.ts +11 −1
@@ -4,7 +4,10 @@
44 * answers 200 application/json is matched to its route (each `{param}` is one segment; on several
55 * matches the one with the most literal segments wins, so /issues/search beats /issues/{number})
66 * and its body is checked with `disagreements` against the route's declared 200 schema. No
7 * matching route, or none declared, passes silently.
7 * matching route, or none declared, passes silently. Only an answer this forge wrote is held: it
8 * left through `securityFloor`, so it carries the floor's headers. A test's canned origin (a fake
9 * Forgejo whose repo `id` is an integer, a stub pull for a CLI test) shares the `/api/v1/` prefix
10 * but answers its own shape, which openapi.json does not describe (#17987).
811 *
912 * Collect mode (the default) writes each disagreement to stderr as `CONTRACT GET <pathname>: …`
1013 * and fails nothing; `contractStrict(true)` throws instead, naming the field.
@@ -26,6 +29,12 @@ export function contractStrict(on: boolean): void {
2629 strict = on;
2730 }
2831
32 /** Headers only `securityFloor` sets: an answer without them was not written by this forge. */
33 const FLOOR_MARKS = ["permissions-policy", "cross-origin-opener-policy"] as const;
34
35 /** Whether `res` left through this forge's `securityFloor`, not a test's canned server. */
36 const fromThisForge = (res: Response): boolean => FLOOR_MARKS.every((h) => res.headers.has(h));
37
2938 const escape = (s: string): string => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3039
3140 /** The 200 application/json schema an operation declares, if any. */
@@ -75,6 +84,7 @@ export function installContractFetch(): void {
7584 const method = (input instanceof Request ? input.method : (init?.method ?? "GET")).toUpperCase();
7685 if (method !== "GET" || res.status !== 200) return res;
7786 if (!(res.headers.get("content-type") ?? "").startsWith("application/json")) return res;
87 if (!fromThisForge(res)) return res;
7888 const url = new URL(input instanceof Request ? input.url : String(input));
7989 const pathname = url.pathname;
8090 if (!pathname.startsWith("/api/v1/")) return res;
test/support/conforms.test.ts +14 −3
@@ -1,5 +1,6 @@
11 import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test";
22 import { openapi } from "../../src/contract/openapi.ts";
3 import { securityFloor } from "../../src/http/respond.ts";
34 import { contractStrict } from "../helpers/contract-fetch.ts";
45 import { declared, disagreements } from "./conforms.ts";
56
@@ -29,24 +30,34 @@ describe("the suite's fetch holds /api/v1 answers to openapi.json (#17961)", ()
2930 };
3031 const valid = sampleOf(declared(doc, "/api/v1/repos/{owner}/{name}/pulls/{number}", "get")) as S;
3132 let server: ReturnType<typeof Bun.serve>;
33 let canned: ReturnType<typeof Bun.serve>;
3234 beforeAll(() => {
3335 server = Bun.serve({
3436 port: 0,
3537 hostname: "127.0.0.1",
38 // Each answer leaves through the floor, as every answer the forge writes does.
3639 fetch: (req) => {
3740 const path = new URL(req.url).pathname;
38 if (path === "/api/v1/repos/a/b/pulls/1") return Response.json({ ...valid, approvedAt: { at: "x" } });
41 if (path === "/api/v1/repos/a/b/pulls/1") return securityFloor(Response.json({ ...valid, approvedAt: { at: "x" } }));
3942 if (path === "/api/v1/repos/a/b/pulls") {
4043 const number = new URL(req.url).searchParams.get("fields") === "number,title" ? 7 : "seven";
41 return Response.json({ items: [{ number, title: "t" }], next_cursor: null });
44 return securityFloor(Response.json({ items: [{ number, title: "t" }], next_cursor: null }));
4245 }
43 return Response.json(valid);
46 return securityFloor(Response.json(valid));
4447 },
4548 });
49 // A test's fake origin: the same /api/v1/ path, its own shape, no floor.
50 canned = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => Response.json({ id: 7, approvedAt: { at: "x" } }) });
4651 });
4752 afterAll(() => {
4853 contractStrict(false);
4954 server.stop(true);
55 canned.stop(true);
56 });
57
58 test("a canned origin's answer under /api/v1/ is not this forge's and is not held (#17987)", async () => {
59 contractStrict(true);
60 expect((await fetch(`http://127.0.0.1:${canned.port}/api/v1/repos/a/b/pulls/1`)).status).toBe(200);
5061 });
5162
5263 test("a field of the wrong type rejects under strict, naming it; a conforming answer passes", async () => {

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs