From d8ebbedf45bdef3396a05a578e98da228cae97dd Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 1 Jun 2026 13:04:21 +0200 Subject: [PATCH] refactor: share plugin http auth request assertions --- src/gateway/server.plugin-http-auth.test.ts | 74 +++++++++------------ 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/src/gateway/server.plugin-http-auth.test.ts b/src/gateway/server.plugin-http-auth.test.ts index 342c557826a..d1502232c68 100644 --- a/src/gateway/server.plugin-http-auth.test.ts +++ b/src/gateway/server.plugin-http-auth.test.ts @@ -158,6 +158,16 @@ function createRuntimeScopeRecorderHandler(params: { }); } +async function expectPluginRequestOk( + server: Parameters[0], + request: Parameters[0], +): Promise { + const response = createResponse(); + await dispatchRequest(server, createRequest(request), response.res); + expect(response.res.statusCode).toBe(200); + expect(response.getBody()).toBe("ok"); +} + describe("gateway plugin HTTP auth boundary", () => { test("applies default security headers and optional strict transport security", async () => { await withGatewayTempConfig("openclaw-plugin-http-security-headers-test-", async () => { @@ -319,23 +329,15 @@ describe("gateway plugin HTTP auth boundary", () => { }, }); - const response = createResponse(); - await dispatchRequest( - server, - createRequest({ - path: "/secure-hook", - remoteAddress: "203.0.113.10", - headers: { - "x-forwarded-user": "operator", - "x-forwarded-for": "198.51.100.20", - "x-openclaw-scopes": "operator.read", - }, - }), - response.res, - ); - - expect(response.res.statusCode).toBe(200); - expect(response.getBody()).toBe("ok"); + await expectPluginRequestOk(server, { + path: "/secure-hook", + remoteAddress: "203.0.113.10", + headers: { + "x-forwarded-user": "operator", + "x-forwarded-for": "198.51.100.20", + "x-openclaw-scopes": "operator.read", + }, + }); }, }); @@ -362,21 +364,13 @@ describe("gateway plugin HTTP auth boundary", () => { shouldEnforcePluginGatewayAuth: (pathContext) => pathContext.pathname === "/secure-hook", }, run: async (server) => { - const response = createResponse(); - await dispatchRequest( - server, - createRequest({ - path: "/secure-hook", - authorization: "Bearer test-token", - headers: { - "x-openclaw-scopes": "operator.read", - }, - }), - response.res, - ); - - expect(response.res.statusCode).toBe(200); - expect(response.getBody()).toBe("ok"); + await expectPluginRequestOk(server, { + path: "/secure-hook", + authorization: "Bearer test-token", + headers: { + "x-openclaw-scopes": "operator.read", + }, + }); }, }); @@ -405,18 +399,10 @@ describe("gateway plugin HTTP auth boundary", () => { pathContext.pathname === "/secure-admin-hook", }, run: async (server) => { - const response = createResponse(); - await dispatchRequest( - server, - createRequest({ - path: "/secure-admin-hook", - authorization: "Bearer test-token", - }), - response.res, - ); - - expect(response.res.statusCode).toBe(200); - expect(response.getBody()).toBe("ok"); + await expectPluginRequestOk(server, { + path: "/secure-admin-hook", + authorization: "Bearer test-token", + }); }, });