Skip to content

Commit cc49ff7

Browse files
committed
test(rivetkit): cover bypass fetch header forwarding
1 parent c0b45b3 commit cc49ff7

1 file changed

Lines changed: 76 additions & 1 deletion

File tree

rivetkit-typescript/packages/rivetkit/tests/remote-engine-client-public-token.test.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
22
import { ClientConfigSchema } from "@/client/config";
3-
import { HEADER_RIVET_TOKEN } from "@/common/actor-router-consts";
3+
import {
4+
HEADER_RIVET_ACTOR,
5+
HEADER_RIVET_BYPASS_CONNECTABLE,
6+
HEADER_RIVET_TARGET,
7+
HEADER_RIVET_TOKEN,
8+
} from "@/common/actor-router-consts";
9+
import { createClient } from "@/client/mod";
410
import { RemoteEngineControlClient } from "@/engine-client/mod";
511

612
describe.sequential("RemoteEngineControlClient public token usage", () => {
@@ -76,6 +82,75 @@ describe.sequential("RemoteEngineControlClient public token usage", () => {
7682
expect(actorRequest?.headers.get("x-user-header")).toBe("present");
7783
});
7884

85+
test("sets bypass connectable header for actor HTTP gateway requests", async () => {
86+
const fetchCalls: Request[] = [];
87+
const fetchMock = vi.fn(async (input: Request | URL | string) => {
88+
const request = normalizeRequest(input);
89+
fetchCalls.push(request);
90+
return new Response("ok");
91+
});
92+
vi.stubGlobal("fetch", fetchMock);
93+
94+
const driver = new RemoteEngineControlClient(
95+
ClientConfigSchema.parse({
96+
endpoint: "https://api.rivet.dev",
97+
disableMetadataLookup: true,
98+
}),
99+
);
100+
101+
const response = await driver.sendRequest(
102+
{ directId: "actor-http-bypass" },
103+
new Request("http://actor/request/bypass"),
104+
{ bypassConnectable: true },
105+
);
106+
107+
expect(response.status).toBe(200);
108+
expect(fetchCalls).toHaveLength(1);
109+
110+
const actorRequest = fetchCalls[0];
111+
expect(actorRequest?.url).toBe("https://api.rivet.dev/request/bypass");
112+
expect(actorRequest?.headers.get(HEADER_RIVET_TARGET)).toBe("actor");
113+
expect(actorRequest?.headers.get(HEADER_RIVET_ACTOR)).toBe(
114+
"actor-http-bypass",
115+
);
116+
expect(actorRequest?.headers.get(HEADER_RIVET_BYPASS_CONNECTABLE)).toBe(
117+
"1",
118+
);
119+
});
120+
121+
test("handle fetch forwards bypass connectable to browser request", async () => {
122+
const fetchCalls: Request[] = [];
123+
const fetchMock = vi.fn(async (input: Request | URL | string) => {
124+
const request = normalizeRequest(input);
125+
fetchCalls.push(request);
126+
return new Response("ok");
127+
});
128+
vi.stubGlobal("fetch", fetchMock);
129+
130+
const client = createClient({
131+
endpoint: "https://api.rivet.dev",
132+
disableMetadataLookup: true,
133+
});
134+
const handle = client.getForId("mockAgenticLoop", "actor-http-bypass");
135+
136+
const response = await handle.fetch("/bypass", {
137+
gateway: { bypassConnectable: true },
138+
});
139+
140+
expect(response.status).toBe(200);
141+
expect(fetchCalls).toHaveLength(1);
142+
143+
const actorRequest = fetchCalls[0];
144+
expect(actorRequest?.url).toBe("https://api.rivet.dev/request/bypass");
145+
expect(actorRequest?.headers.get(HEADER_RIVET_TARGET)).toBe("actor");
146+
expect(actorRequest?.headers.get(HEADER_RIVET_ACTOR)).toBe(
147+
"actor-http-bypass",
148+
);
149+
expect(actorRequest?.headers.get(HEADER_RIVET_BYPASS_CONNECTABLE)).toBe(
150+
"1",
151+
);
152+
});
153+
79154
test("uses metadata clientToken for actor websocket gateway requests", async () => {
80155
const fetchMock = vi.fn(async (input: Request | URL | string) => {
81156
const request = normalizeRequest(input);

0 commit comments

Comments
 (0)