Files
openclaw-zero-token/gateway/http-endpoint-helpers.test.ts
sjhu 571e14a236 feat: upgrade to upstream v2026.3.28
Major upgrade from e26988a38 to upstream v2026.3.28 (f9b107928).
Key changes:
- Upstream src/, ui/, extensions/ (89 bundled extensions)
- Zero-token web providers preserved in src/zero-token/
- AskOnce plugin restored and registered as CLI command
- Added missing packages: @anthropic-ai/vertex-sdk, @modelcontextprotocol/sdk
- Fixed tsconfig rootDir, skipLibCheck for plugin-sdk DTS build
- Added askonce to bundled plugin metadata and package.json exports
- Fixed AskOnce CLI command registration (missing commands metadata)
- Restored AskOnce adapter imports (correct 5-level relative paths)
- Removed stale migration artifacts from root directory
2026-03-30 17:58:12 +08:00

135 lines
4.6 KiB
TypeScript

import type { IncomingMessage, ServerResponse } from "node:http";
import { describe, expect, it, vi } from "vitest";
import type { ResolvedGatewayAuth } from "./auth.js";
import { handleGatewayPostJsonEndpoint } from "./http-endpoint-helpers.js";
vi.mock("./http-auth-helpers.js", () => {
return {
authorizeGatewayBearerRequestOrReply: vi.fn(),
resolveGatewayRequestedOperatorScopes: vi.fn(),
};
});
vi.mock("./http-common.js", () => {
return {
readJsonBodyOrError: vi.fn(),
sendJson: vi.fn(),
sendMethodNotAllowed: vi.fn(),
};
});
vi.mock("./method-scopes.js", () => {
return {
authorizeOperatorScopesForMethod: vi.fn(),
};
});
const { authorizeGatewayBearerRequestOrReply } = await import("./http-auth-helpers.js");
const { resolveGatewayRequestedOperatorScopes } = await import("./http-auth-helpers.js");
const { readJsonBodyOrError, sendJson, sendMethodNotAllowed } = await import("./http-common.js");
const { authorizeOperatorScopesForMethod } = await import("./method-scopes.js");
describe("handleGatewayPostJsonEndpoint", () => {
it("returns false when path does not match", async () => {
const result = await handleGatewayPostJsonEndpoint(
{
url: "/nope",
method: "POST",
headers: { host: "localhost" },
} as unknown as IncomingMessage,
{} as unknown as ServerResponse,
{ pathname: "/v1/ok", auth: {} as unknown as ResolvedGatewayAuth, maxBodyBytes: 1 },
);
expect(result).toBe(false);
});
it("returns undefined and replies when method is not POST", async () => {
const mockedSendMethodNotAllowed = vi.mocked(sendMethodNotAllowed);
mockedSendMethodNotAllowed.mockClear();
const result = await handleGatewayPostJsonEndpoint(
{
url: "/v1/ok",
method: "GET",
headers: { host: "localhost" },
} as unknown as IncomingMessage,
{} as unknown as ServerResponse,
{ pathname: "/v1/ok", auth: {} as unknown as ResolvedGatewayAuth, maxBodyBytes: 1 },
);
expect(result).toBeUndefined();
expect(mockedSendMethodNotAllowed).toHaveBeenCalledTimes(1);
});
it("returns undefined when auth fails", async () => {
vi.mocked(authorizeGatewayBearerRequestOrReply).mockResolvedValue(false);
const result = await handleGatewayPostJsonEndpoint(
{
url: "/v1/ok",
method: "POST",
headers: { host: "localhost" },
} as unknown as IncomingMessage,
{} as unknown as ServerResponse,
{ pathname: "/v1/ok", auth: {} as unknown as ResolvedGatewayAuth, maxBodyBytes: 1 },
);
expect(result).toBeUndefined();
});
it("returns body when auth succeeds and JSON parsing succeeds", async () => {
vi.mocked(authorizeGatewayBearerRequestOrReply).mockResolvedValue(true);
vi.mocked(readJsonBodyOrError).mockResolvedValue({ hello: "world" });
const result = await handleGatewayPostJsonEndpoint(
{
url: "/v1/ok",
method: "POST",
headers: { host: "localhost" },
} as unknown as IncomingMessage,
{} as unknown as ServerResponse,
{ pathname: "/v1/ok", auth: {} as unknown as ResolvedGatewayAuth, maxBodyBytes: 123 },
);
expect(result).toEqual({ body: { hello: "world" } });
});
it("returns undefined and replies when required operator scope is missing", async () => {
vi.mocked(authorizeGatewayBearerRequestOrReply).mockResolvedValue(true);
vi.mocked(resolveGatewayRequestedOperatorScopes).mockReturnValue(["operator.approvals"]);
vi.mocked(authorizeOperatorScopesForMethod).mockReturnValue({
allowed: false,
missingScope: "operator.write",
});
const mockedSendJson = vi.mocked(sendJson);
mockedSendJson.mockClear();
vi.mocked(readJsonBodyOrError).mockClear();
const result = await handleGatewayPostJsonEndpoint(
{
url: "/v1/ok",
method: "POST",
headers: { host: "localhost" },
} as unknown as IncomingMessage,
{} as unknown as ServerResponse,
{
pathname: "/v1/ok",
auth: {} as unknown as ResolvedGatewayAuth,
maxBodyBytes: 123,
requiredOperatorMethod: "chat.send",
},
);
expect(result).toBeUndefined();
expect(vi.mocked(authorizeOperatorScopesForMethod)).toHaveBeenCalledWith("chat.send", [
"operator.approvals",
]);
expect(mockedSendJson).toHaveBeenCalledWith(
expect.anything(),
403,
expect.objectContaining({
ok: false,
error: expect.objectContaining({
type: "forbidden",
message: "missing scope: operator.write",
}),
}),
);
expect(vi.mocked(readJsonBodyOrError)).not.toHaveBeenCalled();
});
});