惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
博客园_首页
Jina AI
Jina AI
WordPress大学
WordPress大学
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
美团技术团队
IT之家
IT之家
爱范儿
爱范儿
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
feat: add bundled admin HTTP RPC plugin · openclaw/opencl...
steipete · 2026-05-15 · via Recent Commits to openclaw:main

@@ -0,0 +1,160 @@

1+

import { Readable } from "node:stream";

2+

import { beforeEach, describe, expect, it, vi } from "vitest";

3+

import { handleAdminHttpRpcRequest } from "./handler.js";

4+

import { listAdminHttpRpcAllowedMethods } from "./methods.js";

5+6+

const { dispatchGatewayMethod } = vi.hoisted(() => ({

7+

dispatchGatewayMethod: vi.fn(),

8+

}));

9+10+

vi.mock("openclaw/plugin-sdk/gateway-method-runtime", () => ({

11+

dispatchGatewayMethod,

12+

}));

13+14+

type CapturedResponse = {

15+

statusCode: number;

16+

headers: Record<string, string | number | readonly string[]>;

17+

body: string;

18+

};

19+20+

function createRequest(body: unknown, method = "POST") {

21+

const req = Readable.from([typeof body === "string" ? body : JSON.stringify(body)]);

22+

Object.assign(req, {

23+

method,

24+

url: "/api/v1/admin/rpc",

25+

headers: {

26+

"content-type": "application/json",

27+

},

28+

});

29+

return req as import("node:http").IncomingMessage;

30+

}

31+32+

function createResponse() {

33+

const captured: CapturedResponse = {

34+

statusCode: 200,

35+

headers: {},

36+

body: "",

37+

};

38+

const res = {

39+

get statusCode() {

40+

return captured.statusCode;

41+

},

42+

set statusCode(value: number) {

43+

captured.statusCode = value;

44+

},

45+

setHeader(name: string, value: string | number | readonly string[]) {

46+

captured.headers[name.toLowerCase()] = value;

47+

},

48+

end(chunk?: string | Buffer) {

49+

captured.body = Buffer.isBuffer(chunk) ? chunk.toString("utf8") : (chunk ?? "");

50+

},

51+

} as import("node:http").ServerResponse;

52+

return { res, captured };

53+

}

54+55+

async function invoke(body: unknown, method = "POST") {

56+

const { res, captured } = createResponse();

57+

const handled = await handleAdminHttpRpcRequest(createRequest(body, method), res);

58+

return {

59+

handled,

60+

captured,

61+

json: captured.body ? (JSON.parse(captured.body) as unknown) : undefined,

62+

};

63+

}

64+65+

describe("admin-http-rpc plugin handler", () => {

66+

beforeEach(() => {

67+

dispatchGatewayMethod.mockReset();

68+

});

69+70+

it("returns the allowlist without dispatching through the Gateway", async () => {

71+

const result = await invoke({ id: "1", method: "commands.list" });

72+73+

expect(result.handled).toBe(true);

74+

expect(result.captured.statusCode).toBe(200);

75+

expect(result.json).toEqual({

76+

id: "1",

77+

ok: true,

78+

payload: {

79+

methods: listAdminHttpRpcAllowedMethods(),

80+

},

81+

});

82+

expect(dispatchGatewayMethod).not.toHaveBeenCalled();

83+

});

84+85+

it("dispatches allowed methods through the authenticated plugin request scope", async () => {

86+

dispatchGatewayMethod.mockResolvedValueOnce({

87+

ok: true,

88+

payload: { status: "ok" },

89+

meta: { requestId: "abc" },

90+

});

91+92+

const result = await invoke({

93+

id: "cfg",

94+

method: "config.get",

95+

params: { path: "gateway" },

96+

});

97+98+

expect(dispatchGatewayMethod).toHaveBeenCalledWith("config.get", { path: "gateway" });

99+

expect(result.captured.statusCode).toBe(200);

100+

expect(result.json).toEqual({

101+

id: "cfg",

102+

ok: true,

103+

payload: { status: "ok" },

104+

meta: { requestId: "abc" },

105+

});

106+

});

107+108+

it("rejects methods outside the admin HTTP RPC allowlist", async () => {

109+

const result = await invoke({ id: "bad", method: "sessions.send" });

110+111+

expect(dispatchGatewayMethod).not.toHaveBeenCalled();

112+

expect(result.captured.statusCode).toBe(400);

113+

expect(result.json).toEqual({

114+

id: "bad",

115+

ok: false,

116+

error: {

117+

code: "INVALID_REQUEST",

118+

message: "admin HTTP RPC method is not supported: sessions.send",

119+

},

120+

});

121+

});

122+123+

it("maps Gateway errors to HTTP status codes", async () => {

124+

dispatchGatewayMethod.mockResolvedValueOnce({

125+

ok: false,

126+

error: { code: "NOT_PAIRED", message: "pair first" },

127+

});

128+129+

const result = await invoke({ id: "node", method: "node.list" });

130+131+

expect(result.captured.statusCode).toBe(409);

132+

expect(result.json).toEqual({

133+

id: "node",

134+

ok: false,

135+

error: { code: "NOT_PAIRED", message: "pair first" },

136+

});

137+

});

138+139+

it("rejects invalid request bodies before dispatch", async () => {

140+

const result = await invoke({ id: "missing" });

141+142+

expect(result.captured.statusCode).toBe(400);

143+

expect(result.json).toEqual({

144+

ok: false,

145+

error: {

146+

type: "invalid_request",

147+

message: "method must be a non-empty string",

148+

},

149+

});

150+

expect(dispatchGatewayMethod).not.toHaveBeenCalled();

151+

});

152+153+

it("only accepts POST", async () => {

154+

const result = await invoke({ method: "status" }, "GET");

155+156+

expect(result.captured.statusCode).toBe(405);

157+

expect(result.captured.headers.allow).toBe("POST");

158+

expect(dispatchGatewayMethod).not.toHaveBeenCalled();

159+

});

160+

});