Skip to content

Commit 9bde14c

Browse files
committed
test(hermes): reuse broker integration fixture
Signed-off-by: Apurv Kumaria <akumaria@nvidia.com>
1 parent 3096524 commit 9bde14c

3 files changed

Lines changed: 61 additions & 198 deletions

File tree

test/helpers/hermes-tool-gateway-broker-fixture.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,48 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import type { IncomingMessage, ServerResponse } from "node:http";
5+
import zlib from "node:zlib";
6+
7+
export const HERMES_BROKER_REDIRECT_TARGET = "https://redirect-probe.invalid/collect";
8+
9+
export type HermesBrokerUpstreamRequest = {
10+
url?: string;
11+
authorization?: string;
12+
browserUseApiKey?: string;
13+
apiKey?: string;
14+
acceptEncoding?: string;
15+
};
16+
17+
export function handleHermesBrokerUpstream(
18+
requests: HermesBrokerUpstreamRequest[],
19+
req: IncomingMessage,
20+
res: ServerResponse,
21+
): void {
22+
requests.push({
23+
url: req.url,
24+
authorization: req.headers.authorization,
25+
browserUseApiKey: req.headers["x-browser-use-api-key"] as string | undefined,
26+
apiKey: req.headers["x-api-key"] as string | undefined,
27+
acceptEncoding: req.headers["accept-encoding"] as string | undefined,
28+
});
29+
if (req.url === "/v1/redirect-probe") {
30+
res.writeHead(302, {
31+
Location: HERMES_BROKER_REDIRECT_TARGET,
32+
"Content-Type": "text/plain",
33+
});
34+
res.end("moved");
35+
return;
36+
}
37+
const body = zlib.gzipSync(JSON.stringify({ ok: true, path: req.url }));
38+
res.writeHead(200, {
39+
"Content-Type": "application/json",
40+
"Content-Encoding": "gzip",
41+
"Content-Length": String(body.length),
42+
"Content-MD5": "not-a-real-digest",
43+
"Set-Cookie": "fixture_session=1; HttpOnly; Secure; SameSite=Strict",
44+
});
45+
res.end(body);
46+
}
547

648
export function handleHermesBrokerCoexistencePortal(
749
refreshHeaders: string[],

test/hermes-tool-gateway-broker-redirect.test.ts

Lines changed: 0 additions & 171 deletions
This file was deleted.

test/hermes-tool-gateway-broker.test.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ import http from "node:http";
1010
import { createRequire } from "node:module";
1111
import net from "node:net";
1212
import path from "node:path";
13-
import zlib from "node:zlib";
1413
import { vi } from "vitest";
15-
import { handleHermesBrokerCoexistencePortal } from "./helpers/hermes-tool-gateway-broker-fixture";
14+
import {
15+
handleHermesBrokerCoexistencePortal,
16+
handleHermesBrokerUpstream,
17+
HERMES_BROKER_REDIRECT_TARGET,
18+
type HermesBrokerUpstreamRequest,
19+
} from "./helpers/hermes-tool-gateway-broker-fixture";
1620
import {
1721
describe,
1822
expect,
@@ -988,32 +992,9 @@ describe("Hermes managed-tool gateway broker", () => {
988992
);
989993
const portalPort = await listen(portal);
990994

991-
const upstreamRequests: Array<{
992-
url?: string;
993-
authorization?: string;
994-
browserUseApiKey?: string;
995-
apiKey?: string;
996-
acceptEncoding?: string;
997-
}> = [];
995+
const upstreamRequests: HermesBrokerUpstreamRequest[] = [];
998996
const upstream = resources.ownServer(
999-
http.createServer((req, res) => {
1000-
upstreamRequests.push({
1001-
url: req.url,
1002-
authorization: req.headers.authorization,
1003-
browserUseApiKey: req.headers["x-browser-use-api-key"] as string | undefined,
1004-
apiKey: req.headers["x-api-key"] as string | undefined,
1005-
acceptEncoding: req.headers["accept-encoding"] as string | undefined,
1006-
});
1007-
const body = zlib.gzipSync(JSON.stringify({ ok: true, path: req.url }));
1008-
res.writeHead(200, {
1009-
"Content-Type": "application/json",
1010-
"Content-Encoding": "gzip",
1011-
"Content-Length": String(body.length),
1012-
"Content-MD5": "not-a-real-digest",
1013-
"Set-Cookie": "fixture_session=1; HttpOnly; Secure; SameSite=Strict",
1014-
});
1015-
res.end(body);
1016-
}),
997+
http.createServer((req, res) => handleHermesBrokerUpstream(upstreamRequests, req, res)),
1017998
);
1018999
const upstreamPort = await listen(upstream);
10191000
const matrixPath = path.join(tmp, "matrix.json");
@@ -1165,6 +1146,17 @@ describe("Hermes managed-tool gateway broker", () => {
11651146
url: "/sandboxes",
11661147
authorization: "Bearer access-2",
11671148
});
1149+
1150+
const redirect = await fetch(`http://127.0.0.1:${brokerPort}/firecrawl/v1/redirect-probe`, {
1151+
headers: { "x-api-key": "refresh-2" },
1152+
redirect: "manual",
1153+
});
1154+
const redirectBody = await redirect.text();
1155+
expect(upstreamRequests.at(-1)?.url).toBe("/v1/redirect-probe");
1156+
expect(redirect.status).toBe(502);
1157+
expect(redirect.headers.get("location")).toBeNull();
1158+
expect(redirectBody).toContain("redirect");
1159+
expect(redirectBody).not.toContain(HERMES_BROKER_REDIRECT_TARGET);
11681160
expect(tokenRequests).toHaveLength(1);
11691161
expect(agentKeyRequests).toHaveLength(1);
11701162
expect(output).not.toContain("refresh-1");

0 commit comments

Comments
 (0)