test: share plugin http auth helpers

This commit is contained in:
Peter Steinberger 2026-03-14 00:19:59 +00:00
parent b72ac7936a
commit b64466953a
1 changed files with 37 additions and 45 deletions

View File

@ -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<typeof sendRequest>[0];
handlePluginRequest: ReturnType<typeof createHealthzPluginHandler>;
}) {
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 });
},
});
});