refactor: share plugin http auth request assertions

This commit is contained in:
Vincent Koc
2026-06-01 13:04:21 +02:00
parent 9ed1766696
commit d8ebbedf45

View File

@@ -158,6 +158,16 @@ function createRuntimeScopeRecorderHandler(params: {
});
}
async function expectPluginRequestOk(
server: Parameters<typeof dispatchRequest>[0],
request: Parameters<typeof createRequest>[0],
): Promise<void> {
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",
});
},
});