import http from "node:http"; import type { AddressInfo } from "node:net"; import type Koa from "koa"; // oxlint-disable-next-line no-restricted-imports import nodeFetch from "node-fetch"; // oxlint-disable-next-line no-restricted-imports import type { RequestInit } from "node-fetch"; type TestRequestOptions = Omit & { body?: unknown; headers?: Record; }; interface Authable { getSessionToken(): string; } const tokenCache = new WeakMap(); function getCachedSessionToken(user: Authable): string { let token = tokenCache.get(user); if (!token) { token = user.getSessionToken(); tokenCache.set(user, token); } return token; } function normalizeArgs( userOrOpts?: Authable | TestRequestOptions, maybeOpts?: TestRequestOptions ): { user?: Authable; opts: TestRequestOptions } { if ( userOrOpts && typeof (userOrOpts as Authable).getSessionToken === "function" ) { return { user: userOrOpts as Authable, opts: maybeOpts ?? {} }; } return { opts: (userOrOpts as TestRequestOptions) ?? {} }; } class TestServer { private server: http.Server; private listener?: Promise | null; constructor(app: Koa) { this.server = http.createServer(app.callback() as http.RequestListener); } get address(): string { const { port } = this.server.address() as AddressInfo; return `http://localhost:${port}`; } listen() { if (!this.listener) { this.listener = new Promise((resolve, reject) => { this.server .listen(0, () => resolve()) .on("error", (err) => reject(err)); }); } return this.listener; } fetch(path: string, opts?: TestRequestOptions): ReturnType; fetch( path: string, user: Authable, opts?: TestRequestOptions ): ReturnType; fetch( path: string, userOrOpts?: Authable | TestRequestOptions, maybeOpts?: TestRequestOptions ) { const { user, opts } = normalizeArgs(userOrOpts, maybeOpts); return this.listen().then(() => { const url = `${this.address}${path}`; const headers: Record = { ...opts.headers }; if (user && !headers.Authorization && !headers.authorization) { headers.Authorization = `Bearer ${getCachedSessionToken(user)}`; } let body = opts.body; const contentType = headers["Content-Type"] ?? headers["content-type"]; // automatic JSON encoding if (!contentType && typeof body === "object" && body !== null) { headers["Content-Type"] = "application/json"; body = JSON.stringify(body); } return nodeFetch(url, { ...opts, headers, body: body as string | undefined, }); }); } close() { this.listener = null; this.server.closeAllConnections(); this.server.close(); } delete(path: string, opts?: TestRequestOptions): ReturnType; delete( path: string, user: Authable, opts?: TestRequestOptions ): ReturnType; delete( path: string, userOrOpts?: Authable | TestRequestOptions, maybeOpts?: TestRequestOptions ) { const { user, opts } = normalizeArgs(userOrOpts, maybeOpts); return user ? this.fetch(path, user, { ...opts, method: "DELETE" }) : this.fetch(path, { ...opts, method: "DELETE" }); } get(path: string, opts?: TestRequestOptions): ReturnType; get( path: string, user: Authable, opts?: TestRequestOptions ): ReturnType; get( path: string, userOrOpts?: Authable | TestRequestOptions, maybeOpts?: TestRequestOptions ) { const { user, opts } = normalizeArgs(userOrOpts, maybeOpts); return user ? this.fetch(path, user, { ...opts, method: "GET" }) : this.fetch(path, { ...opts, method: "GET" }); } head(path: string, opts?: TestRequestOptions): ReturnType; head( path: string, user: Authable, opts?: TestRequestOptions ): ReturnType; head( path: string, userOrOpts?: Authable | TestRequestOptions, maybeOpts?: TestRequestOptions ) { const { user, opts } = normalizeArgs(userOrOpts, maybeOpts); return user ? this.fetch(path, user, { ...opts, method: "HEAD" }) : this.fetch(path, { ...opts, method: "HEAD" }); } options( path: string, opts?: TestRequestOptions ): ReturnType; options( path: string, user: Authable, opts?: TestRequestOptions ): ReturnType; options( path: string, userOrOpts?: Authable | TestRequestOptions, maybeOpts?: TestRequestOptions ) { const { user, opts } = normalizeArgs(userOrOpts, maybeOpts); return user ? this.fetch(path, user, { ...opts, method: "OPTIONS" }) : this.fetch(path, { ...opts, method: "OPTIONS" }); } patch(path: string, opts?: TestRequestOptions): ReturnType; patch( path: string, user: Authable, opts?: TestRequestOptions ): ReturnType; patch( path: string, userOrOpts?: Authable | TestRequestOptions, maybeOpts?: TestRequestOptions ) { const { user, opts } = normalizeArgs(userOrOpts, maybeOpts); return user ? this.fetch(path, user, { ...opts, method: "PATCH" }) : this.fetch(path, { ...opts, method: "PATCH" }); } post(path: string, opts?: TestRequestOptions): ReturnType; post( path: string, user: Authable, opts?: TestRequestOptions ): ReturnType; post( path: string, userOrOpts?: Authable | TestRequestOptions, maybeOpts?: TestRequestOptions ) { const { user, opts } = normalizeArgs(userOrOpts, maybeOpts); return user ? this.fetch(path, user, { ...opts, method: "POST" }) : this.fetch(path, { ...opts, method: "POST" }); } put(path: string, opts?: TestRequestOptions): ReturnType; put( path: string, user: Authable, opts?: TestRequestOptions ): ReturnType; put( path: string, userOrOpts?: Authable | TestRequestOptions, maybeOpts?: TestRequestOptions ) { const { user, opts } = normalizeArgs(userOrOpts, maybeOpts); return user ? this.fetch(path, user, { ...opts, method: "PUT" }) : this.fetch(path, { ...opts, method: "PUT" }); } } export default TestServer;