|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The one place this package builds an `IHttpRequest` for a test (#13377). |
| 5 | + * |
| 6 | + * Test layer only — nothing in `src/index.ts` reaches it, so tsup (entry: |
| 7 | + * `src/index.ts`) never emits it into `dist` and it is not published. Same |
| 8 | + * placement, and for the same reason, as `src/xlsx-test-loader.ts`. |
| 9 | + * |
| 10 | + * ## The defect, stated once |
| 11 | + * |
| 12 | + * `IHttpRequest` (`packages/spec/src/contracts/http-server.ts`) declares FIVE |
| 13 | + * required members — `params`, `query`, `headers`, `method`, `path` — and a |
| 14 | + * route handler's first parameter IS that interface. So a test that hands a |
| 15 | + * handler an object literal owes all five. Before this file the package did |
| 16 | + * neither of the two honest things: 151 of its 281 `.handler(` call sites cast |
| 17 | + * the literal with `as any`, and four more sat in `test-typecheck-debt.json` as |
| 18 | + * ledgered `TS2345`: |
| 19 | + * |
| 20 | + * ``` |
| 21 | + * src/rest.test.ts(2063,7): error TS2345: Argument of type |
| 22 | + * '{ params: { environmentId: string; object: string; }; query: {}; }' |
| 23 | + * is not assignable to parameter of type 'IHttpRequest'. |
| 24 | + * ``` |
| 25 | + * |
| 26 | + * ⚠️ No annotation repairs those four. Adding the missing members changes what |
| 27 | + * the handler RECEIVES — the fixture's data, not its typing — so inventing |
| 28 | + * values silently changes what each test measures. That is why this is one |
| 29 | + * builder and not four edits, and it is why the defaults below are argued |
| 30 | + * rather than picked. |
| 31 | + * |
| 32 | + * ## Why each default is the default |
| 33 | + * |
| 34 | + * **`method` and `path` are NOT defaulted — they are read off the route under |
| 35 | + * test.** A constant default here is precisely the hazard #13377 names: a |
| 36 | + * `path` that does not match the route under test makes a passing test measure |
| 37 | + * something other than what it names, and `req.path` is live rather than |
| 38 | + * decorative — `RestServer.enforceAuth` feeds it to `isAuthGateAllowlisted` |
| 39 | + * (`src/rest-server.ts:1254`), where a wrong value decides whether the ADR-0069 |
| 40 | + * gate fires at all. Every call site already locates its route |
| 41 | + * (`getRoutes().find(r => r.method === 'GET' && r.path === '…')`); handing that |
| 42 | + * same route object here means the request and the route it is sent to CANNOT |
| 43 | + * disagree, because one is derived from the other. |
| 44 | + * |
| 45 | + * **`path` is materialized from `params`, not copied from the pattern.** A |
| 46 | + * transport hands the handler a CONCRETE path; `/api/v1/data/:object` is a |
| 47 | + * pattern no request ever carries. So the builder substitutes the request's own |
| 48 | + * `params` into the pattern's `:segments` — which also means `path` and |
| 49 | + * `params` cannot drift apart, since one is computed from the other. A |
| 50 | + * `:segment` with no matching param is REFUSED loudly instead of emitted: a |
| 51 | + * path carrying a literal `:` is one no transport could produce, and quietly |
| 52 | + * shipping it is exactly the plausible-looking value this card exists to |
| 53 | + * prevent. A test that wants an unmatched path says so, by overriding `path`. |
| 54 | + * |
| 55 | + * **`headers: {}`** is the one required member with a genuinely neutral |
| 56 | + * default. It means "a request that carried no headers", which is what every |
| 57 | + * one of these fixtures intends, and every header read in this package is |
| 58 | + * either optional-chained or an index access — both yield `undefined` against |
| 59 | + * `{}`, identical to what the fixture meant by omitting it. It is also strictly |
| 60 | + * better-formed than what the ledgered literals produced: they left |
| 61 | + * `req.headers` `undefined`, so the unguarded `req.headers['if-none-match']` |
| 62 | + * (`src/rest-server.ts:5232`) would have THROWN had those tests reached it. |
| 63 | + * |
| 64 | + * **`params: {}` / `query: {}`** — empty is the honest reading of "the fixture |
| 65 | + * supplied none", and both are `Record`s every handler reads defensively. |
| 66 | + * |
| 67 | + * **`remoteAddress` is deliberately ABSENT, and has no default at all.** Its |
| 68 | + * contract note (#4910) is explicit that it is "the TRANSPORT's own peer |
| 69 | + * address … the unforgeable half of caller identification", which a client |
| 70 | + * cannot influence and which the inbound rate limiter keys anonymous traffic |
| 71 | + * off. A builder that supplied a plausible `'127.0.0.1'` would put a forged |
| 72 | + * value in the one member whose entire worth is that it cannot be forged. The |
| 73 | + * interface makes it optional because "not every runtime exposes it", so absent |
| 74 | + * is both legal and true; a test about it states it. |
| 75 | + * |
| 76 | + * **`body` / `rawBody`** are optional and stay absent unless stated: a GET has |
| 77 | + * no body, and the sites that have one pass it. |
| 78 | + */ |
| 79 | + |
| 80 | +import type { RouteHandler } from '@objectstack/core'; |
| 81 | + |
| 82 | +/** |
| 83 | + * The request type a handler is actually handed, read off the handler's own |
| 84 | + * signature instead of spelled by hand — the discipline `xlsx-test-loader.ts` |
| 85 | + * applies to its dependency, applied here to ours. It resolves to |
| 86 | + * `IHttpRequest`; deriving it means this builder tracks whatever a handler's |
| 87 | + * first parameter becomes, and a change to that contract fails HERE, loudly, in |
| 88 | + * one file, instead of leaving a helper that still compiles and lies. |
| 89 | + */ |
| 90 | +type HandlerRequest = Parameters<RouteHandler>[0]; |
| 91 | + |
| 92 | +/** |
| 93 | + * The half of a mounted route this builder reads. Structurally what |
| 94 | + * `RestServer.getRoutes()` returns, so a `find(…)` result is passed straight |
| 95 | + * in — but declared narrowly on purpose: building a request must not be able to |
| 96 | + * reach the handler it is building the request FOR. |
| 97 | + */ |
| 98 | +export interface RouteUnderTest { |
| 99 | + /** HTTP verb as registered. */ |
| 100 | + readonly method: string; |
| 101 | + /** Full wire path, with `:param` segments — the pattern, not a request. */ |
| 102 | + readonly path: string; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Whatever this call site's assertion is actually about. Every member is the |
| 107 | + * contract's, so a site can state any of them — including `method` and `path`, |
| 108 | + * which override the route's and are then a deliberate, reviewable divergence |
| 109 | + * from the route the request is sent to. |
| 110 | + */ |
| 111 | +export type HttpRequestOverrides = Partial<HandlerRequest>; |
| 112 | + |
| 113 | +/** |
| 114 | + * Build a complete `IHttpRequest` aimed at `route`, overriding only what this |
| 115 | + * test is about. |
| 116 | + * |
| 117 | + * @param route the mounted route the request is being sent to; supplies |
| 118 | + * `method`, and the pattern `path` is materialized from. |
| 119 | + * @param overrides the members this call site's assertion is about. |
| 120 | + * @throws if the route's pattern has a `:param` that `overrides.params` does |
| 121 | + * not supply and no explicit `path` was given. |
| 122 | + */ |
| 123 | +export function httpRequestForRoute( |
| 124 | + route: RouteUnderTest, |
| 125 | + overrides: HttpRequestOverrides = {}, |
| 126 | +): HandlerRequest { |
| 127 | + const { params = {}, query = {}, headers = {}, method, path, ...optional } = overrides; |
| 128 | + return { |
| 129 | + ...optional, |
| 130 | + params, |
| 131 | + query, |
| 132 | + headers, |
| 133 | + method: method ?? route.method, |
| 134 | + path: path ?? materializeRoutePath(route.path, params), |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Substitute `params` into a route pattern's `:segments`, yielding the concrete |
| 140 | + * path a transport would have produced for this request. |
| 141 | + */ |
| 142 | +function materializeRoutePath(pattern: string, params: Record<string, string>): string { |
| 143 | + return pattern |
| 144 | + .split('/') |
| 145 | + .map((segment) => { |
| 146 | + if (!segment.startsWith(':')) return segment; |
| 147 | + const name = segment.slice(1); |
| 148 | + if (!Object.prototype.hasOwnProperty.call(params, name)) { |
| 149 | + throw new Error( |
| 150 | + `httpRequestForRoute: route '${pattern}' has a ':${name}' segment but params supplied no '${name}'. ` + |
| 151 | + `A path holding a literal ':' is one no transport produces — supply params.${name}, or state ` + |
| 152 | + `the 'path' override deliberately if the mismatch is what the test is about.`, |
| 153 | + ); |
| 154 | + } |
| 155 | + return params[name]; |
| 156 | + }) |
| 157 | + .join('/'); |
| 158 | +} |
0 commit comments