From b64466953a4e8c13d7a78445845396e6e8c0b625 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Mar 2026 00:19:59 +0000 Subject: [PATCH] test: share plugin http auth helpers --- src/gateway/server.plugin-http-auth.test.ts | 82 ++++++++++----------- 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/src/gateway/server.plugin-http-auth.test.ts b/src/gateway/server.plugin-http-auth.test.ts index 6eb9399e23a..029719b01cc 100644 --- a/src/gateway/server.plugin-http-auth.test.ts +++ b/src/gateway/server.plugin-http-auth.test.ts @@ -32,6 +32,37 @@ function respondJsonRoute(res: ServerResponse, route: string): true { return true; } +function createHealthzPluginHandler() { + return vi.fn(async (req: IncomingMessage, res: ServerResponse) => { + const pathname = new URL(req.url ?? "/", "http://localhost").pathname; + if (pathname !== "/healthz") { + return false; + } + return respondJsonRoute(res, "plugin-health"); + }); +} + +async function expectHealthzPluginShadow(params: { + server: Parameters[0]; + handlePluginRequest: ReturnType; +}) { + const response = await sendRequest(params.server, { path: "/healthz" }); + expect(response.res.statusCode).toBe(200); + expect(response.getBody()).toBe(JSON.stringify({ ok: true, route: "plugin-health" })); + expect(params.handlePluginRequest).toHaveBeenCalledTimes(1); +} + +function createMattermostCallbackConfig(callbackPath: string) { + return { + gateway: { trustedProxies: [] }, + channels: { + mattermost: { + commands: { callbackPath }, + }, + }, + }; +} + function createRootMountedControlUiOverrides(handlePluginRequest: PluginRequestHandler) { return { controlUiEnabled: true, @@ -121,26 +152,14 @@ describe("gateway plugin HTTP auth boundary", () => { }); test("does not shadow plugin routes mounted on probe paths", async () => { - const handlePluginRequest = vi.fn(async (req: IncomingMessage, res: ServerResponse) => { - const pathname = new URL(req.url ?? "/", "http://localhost").pathname; - if (pathname === "/healthz") { - res.statusCode = 200; - res.setHeader("Content-Type", "application/json; charset=utf-8"); - res.end(JSON.stringify({ ok: true, route: "plugin-health" })); - return true; - } - return false; - }); + const handlePluginRequest = createHealthzPluginHandler(); await withGatewayServer({ prefix: "openclaw-plugin-http-probes-shadow-test-", resolvedAuth: AUTH_NONE, overrides: { handlePluginRequest }, run: async (server) => { - const response = await sendRequest(server, { path: "/healthz" }); - expect(response.res.statusCode).toBe(200); - expect(response.getBody()).toBe(JSON.stringify({ ok: true, route: "plugin-health" })); - expect(handlePluginRequest).toHaveBeenCalledTimes(1); + await expectHealthzPluginShadow({ server, handlePluginRequest }); }, }); }); @@ -238,14 +257,7 @@ describe("gateway plugin HTTP auth boundary", () => { }); await withTempConfig({ - cfg: { - gateway: { trustedProxies: [] }, - channels: { - mattermost: { - commands: { callbackPath: "/api/channels/mattermost/command" }, - }, - }, - }, + cfg: createMattermostCallbackConfig("/api/channels/mattermost/command"), prefix: "openclaw-plugin-http-auth-mm-callback-", run: async () => { const server = createTestGatewayServer({ @@ -281,14 +293,7 @@ describe("gateway plugin HTTP auth boundary", () => { }); await withTempConfig({ - cfg: { - gateway: { trustedProxies: [] }, - channels: { - mattermost: { - commands: { callbackPath: "/api/channels/nostr/default/profile" }, - }, - }, - }, + cfg: createMattermostCallbackConfig("/api/channels/nostr/default/profile"), prefix: "openclaw-plugin-http-auth-mm-misconfig-", run: async () => { const server = createTestGatewayServer({ @@ -512,26 +517,13 @@ describe("gateway plugin HTTP auth boundary", () => { }); test("root-mounted control ui still lets plugins claim probe paths first", async () => { - const handlePluginRequest = vi.fn(async (req: IncomingMessage, res: ServerResponse) => { - const pathname = new URL(req.url ?? "/", "http://localhost").pathname; - if (pathname !== "/healthz") { - return false; - } - res.statusCode = 200; - res.setHeader("Content-Type", "application/json; charset=utf-8"); - res.end(JSON.stringify({ ok: true, route: "plugin-health" })); - return true; - }); + const handlePluginRequest = createHealthzPluginHandler(); await withRootMountedControlUiServer({ prefix: "openclaw-plugin-http-control-ui-probe-shadow-test-", handlePluginRequest, run: async (server) => { - const response = await sendRequest(server, { path: "/healthz" }); - - expect(response.res.statusCode).toBe(200); - expect(response.getBody()).toBe(JSON.stringify({ ok: true, route: "plugin-health" })); - expect(handlePluginRequest).toHaveBeenCalledTimes(1); + await expectHealthzPluginShadow({ server, handlePluginRequest }); }, }); });