Files
openclaw/src/plugin-sdk/test-helpers/http-test-server.ts
Alix-007 65b2e7a4e3 fix(mattermost): add timeout to REST client requests (#102027)
* fix(mattermost): add timeout to REST client requests

* fixup: preserve Mattermost DM retry timeout

* test(mattermost): reuse hanging server helper

* test(mattermost): satisfy timeout lint

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-07-09 12:12:30 +01:00

26 lines
945 B
TypeScript

// Plugin SDK test helper for temporary local HTTP servers.
import { createServer, type RequestListener } from "node:http";
import type { AddressInfo } from "node:net";
/** Run an ephemeral loopback HTTP server for the duration of an async test callback. */
export async function withServer(handler: RequestListener, fn: (baseUrl: string) => Promise<void>) {
const server = createServer(handler);
await new Promise<void>((resolve) => {
server.listen(0, "127.0.0.1", () => resolve());
});
const address = server.address() as AddressInfo | null;
if (!address) {
throw new Error("missing server address");
}
try {
await fn(`http://127.0.0.1:${address.port}`);
} finally {
const closed = new Promise<void>((resolve) => {
server.close(() => resolve());
});
// Hanging-response tests must still release active sockets when their assertion fails.
server.closeAllConnections();
await closed;
}
}