mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-29 17:28:40 +00:00
refactor(nextcloud-talk): extract webhook pipeline and shared test harness
This commit is contained in:
@@ -1,50 +1,5 @@
|
||||
import { type AddressInfo } from "node:net";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { createNextcloudTalkWebhookServer } from "./monitor.js";
|
||||
|
||||
type WebhookHarness = {
|
||||
webhookUrl: string;
|
||||
stop: () => Promise<void>;
|
||||
};
|
||||
|
||||
const cleanupFns: Array<() => Promise<void>> = [];
|
||||
|
||||
afterEach(async () => {
|
||||
while (cleanupFns.length > 0) {
|
||||
const cleanup = cleanupFns.pop();
|
||||
if (cleanup) {
|
||||
await cleanup();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
async function startWebhookServer(params: {
|
||||
path: string;
|
||||
maxBodyBytes: number;
|
||||
readBody?: (req: import("node:http").IncomingMessage, maxBodyBytes: number) => Promise<string>;
|
||||
}): Promise<WebhookHarness> {
|
||||
const { server, start } = createNextcloudTalkWebhookServer({
|
||||
port: 0,
|
||||
host: "127.0.0.1",
|
||||
path: params.path,
|
||||
secret: "nextcloud-secret",
|
||||
maxBodyBytes: params.maxBodyBytes,
|
||||
readBody: params.readBody,
|
||||
onMessage: vi.fn(),
|
||||
});
|
||||
await start();
|
||||
const address = server.address() as AddressInfo | null;
|
||||
if (!address) {
|
||||
throw new Error("missing server address");
|
||||
}
|
||||
return {
|
||||
webhookUrl: `http://127.0.0.1:${address.port}${params.path}`,
|
||||
stop: () =>
|
||||
new Promise<void>((resolve) => {
|
||||
server.close(() => resolve());
|
||||
}),
|
||||
};
|
||||
}
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { startWebhookServer } from "./monitor.test-harness.js";
|
||||
|
||||
describe("createNextcloudTalkWebhookServer auth order", () => {
|
||||
it("rejects missing signature headers before reading request body", async () => {
|
||||
@@ -55,8 +10,8 @@ describe("createNextcloudTalkWebhookServer auth order", () => {
|
||||
path: "/nextcloud-auth-order",
|
||||
maxBodyBytes: 128,
|
||||
readBody,
|
||||
onMessage: vi.fn(),
|
||||
});
|
||||
cleanupFns.push(harness.stop);
|
||||
|
||||
const response = await fetch(harness.webhookUrl, {
|
||||
method: "POST",
|
||||
|
||||
Reference in New Issue
Block a user