refactor(channels): dedupe transport and gateway test scaffolds
This commit is contained in:
@@ -1,38 +1,8 @@
|
||||
import type { IncomingMessage, ServerResponse } from "node:http";
|
||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
import type { ResolvedGatewayAuth } from "./auth.js";
|
||||
import { createGatewayHttpServer } from "./server-http.js";
|
||||
|
||||
async function withTempConfig(params: { cfg: unknown; run: () => Promise<void> }): Promise<void> {
|
||||
const prevConfigPath = process.env.OPENCLAW_CONFIG_PATH;
|
||||
const prevDisableCache = process.env.OPENCLAW_DISABLE_CONFIG_CACHE;
|
||||
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), "openclaw-plugin-http-auth-test-"));
|
||||
const configPath = path.join(dir, "openclaw.json");
|
||||
|
||||
process.env.OPENCLAW_CONFIG_PATH = configPath;
|
||||
process.env.OPENCLAW_DISABLE_CONFIG_CACHE = "1";
|
||||
|
||||
try {
|
||||
await writeFile(configPath, JSON.stringify(params.cfg, null, 2), "utf-8");
|
||||
await params.run();
|
||||
} finally {
|
||||
if (prevConfigPath === undefined) {
|
||||
delete process.env.OPENCLAW_CONFIG_PATH;
|
||||
} else {
|
||||
process.env.OPENCLAW_CONFIG_PATH = prevConfigPath;
|
||||
}
|
||||
if (prevDisableCache === undefined) {
|
||||
delete process.env.OPENCLAW_DISABLE_CONFIG_CACHE;
|
||||
} else {
|
||||
process.env.OPENCLAW_DISABLE_CONFIG_CACHE = prevDisableCache;
|
||||
}
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
import { withTempConfig } from "./test-temp-config.js";
|
||||
|
||||
function createRequest(params: {
|
||||
path: string;
|
||||
@@ -106,6 +76,7 @@ describe("gateway plugin HTTP auth boundary", () => {
|
||||
|
||||
await withTempConfig({
|
||||
cfg: { gateway: { trustedProxies: [] } },
|
||||
prefix: "openclaw-plugin-http-auth-test-",
|
||||
run: async () => {
|
||||
const handlePluginRequest = vi.fn(async (req: IncomingMessage, res: ServerResponse) => {
|
||||
const pathname = new URL(req.url ?? "/", "http://localhost").pathname;
|
||||
|
||||
Reference in New Issue
Block a user