Public: anyone can read this; the crew can change it.
The test files outside api-* read no answer that disagrees with openapi.json #18001
Merged 12h 7m ago, live in production
mason/forge:mason/17987-the-test-files-outside-api-read-no-answe → main 0 commits fork
2 files
changed +25
−4
test/helpers/contract-fetch.ts +11 −1
| @@ -4,7 +4,10 @@ | ||
| 4 | 4 | * answers 200 application/json is matched to its route (each `{param}` is one segment; on several |
| 5 | 5 | * matches the one with the most literal segments wins, so /issues/search beats /issues/{number}) |
| 6 | 6 | * 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). | |
| 8 | 11 | * |
| 9 | 12 | * Collect mode (the default) writes each disagreement to stderr as `CONTRACT GET <pathname>: …` |
| 10 | 13 | * and fails nothing; `contractStrict(true)` throws instead, naming the field. |
| @@ -26,6 +29,12 @@ export function contractStrict(on: boolean): void { | ||
| 26 | 29 | strict = on; |
| 27 | 30 | } |
| 28 | 31 | |
| 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 | ||
| 29 | 38 | const escape = (s: string): string => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 30 | 39 | |
| 31 | 40 | /** The 200 application/json schema an operation declares, if any. */ |
| @@ -75,6 +84,7 @@ export function installContractFetch(): void { | ||
| 75 | 84 | const method = (input instanceof Request ? input.method : (init?.method ?? "GET")).toUpperCase(); |
| 76 | 85 | if (method !== "GET" || res.status !== 200) return res; |
| 77 | 86 | if (!(res.headers.get("content-type") ?? "").startsWith("application/json")) return res; |
| 87 | if (!fromThisForge(res)) return res; | |
| 78 | 88 | const url = new URL(input instanceof Request ? input.url : String(input)); |
| 79 | 89 | const pathname = url.pathname; |
| 80 | 90 | if (!pathname.startsWith("/api/v1/")) return res; |
test/support/conforms.test.ts +14 −3
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test"; |
| 2 | 2 | import { openapi } from "../../src/contract/openapi.ts"; |
| 3 | import { securityFloor } from "../../src/http/respond.ts"; | |
| 3 | 4 | import { contractStrict } from "../helpers/contract-fetch.ts"; |
| 4 | 5 | import { declared, disagreements } from "./conforms.ts"; |
| 5 | 6 | |
| @@ -29,24 +30,34 @@ describe("the suite's fetch holds /api/v1 answers to openapi.json (#17961)", () | ||
| 29 | 30 | }; |
| 30 | 31 | const valid = sampleOf(declared(doc, "/api/v1/repos/{owner}/{name}/pulls/{number}", "get")) as S; |
| 31 | 32 | let server: ReturnType<typeof Bun.serve>; |
| 33 | let canned: ReturnType<typeof Bun.serve>; | |
| 32 | 34 | beforeAll(() => { |
| 33 | 35 | server = Bun.serve({ |
| 34 | 36 | port: 0, |
| 35 | 37 | hostname: "127.0.0.1", |
| 38 | // Each answer leaves through the floor, as every answer the forge writes does. | |
| 36 | 39 | fetch: (req) => { |
| 37 | 40 | 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" } })); | |
| 39 | 42 | if (path === "/api/v1/repos/a/b/pulls") { |
| 40 | 43 | 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 })); | |
| 42 | 45 | } |
| 43 | return Response.json(valid); | |
| 46 | return securityFloor(Response.json(valid)); | |
| 44 | 47 | }, |
| 45 | 48 | }); |
| 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" } }) }); | |
| 46 | 51 | }); |
| 47 | 52 | afterAll(() => { |
| 48 | 53 | contractStrict(false); |
| 49 | 54 | 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); | |
| 50 | 61 | }); |
| 51 | 62 | |
| 52 | 63 | test("a field of the wrong type rejects under strict, naming it; a conforming answer passes", async () => { |
new above
offline