40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { serviceRoutes } from "../src/routes/services.js";
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const requests: Request[] = [];
|
|
|
|
globalThis.fetch = (async (input, init) => {
|
|
const request = new Request(input, init);
|
|
requests.push(request);
|
|
return new Response(JSON.stringify({ entries: [] }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}) as typeof fetch;
|
|
|
|
const app = serviceRoutes({ skipAuth: true });
|
|
try {
|
|
const leaderboard = await app.request("http://backend.test/interview/leaderboard?org_id=org-X&limit=100");
|
|
assert.equal(leaderboard.status, 200);
|
|
assert.deepEqual(await leaderboard.json(), { entries: [] });
|
|
|
|
const upstream = new URL(requests[0]!.url);
|
|
assert.equal(upstream.pathname, "/api/v1/leaderboard");
|
|
assert.equal(upstream.searchParams.get("limit"), "100");
|
|
assert.equal(upstream.searchParams.get("org_id"), "org-X");
|
|
assert.match(requests[0]!.headers.get("authorization") ?? "", /^Bearer /);
|
|
|
|
const clamped = await app.request("http://backend.test/interview/leaderboard?limit=101");
|
|
assert.equal(clamped.status, 200);
|
|
assert.equal(new URL(requests[1]!.url).searchParams.get("limit"), "100");
|
|
|
|
const defaulted = await app.request("http://backend.test/interview/leaderboard?limit=not-a-number");
|
|
assert.equal(defaulted.status, 200);
|
|
assert.equal(new URL(requests[2]!.url).searchParams.get("limit"), "10");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
|
|
console.log("interview leaderboard gateway: all assertions passed");
|