Skip to content

Commit 27f79b7

Browse files
add e2e tests for startRemoteProxySession
1 parent a589291 commit 27f79b7

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import assert from "node:assert";
2+
import { resolve } from "node:path";
3+
import { afterAll, beforeAll, describe, expect, test } from "vitest";
4+
import { CLOUDFLARE_ACCOUNT_ID } from "../helpers/account-id";
5+
import { WranglerE2ETestHelper } from "../helpers/e2e-wrangler-test";
6+
import { generateResourceName } from "../helpers/generate-resource-name";
7+
import type {
8+
Miniflare,
9+
MiniflareOptions,
10+
RemoteProxyConnectionString,
11+
Response,
12+
} from "miniflare";
13+
14+
describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
15+
"wrangler dev - remote bindings - programmatic API",
16+
async () => {
17+
const remoteWorkerName = generateResourceName();
18+
const helper = new WranglerE2ETestHelper();
19+
20+
const { Miniflare } = await helper.importMiniflare();
21+
22+
const { experimental_startRemoteProxySession: startRemoteProxySession } =
23+
await helper.importWrangler();
24+
25+
beforeAll(async () => {
26+
await helper.seed(resolve(__dirname, "./workers"));
27+
await helper.run(
28+
`wrangler deploy remote-worker.js --name ${remoteWorkerName} --compatibility-date 2025-01-01`
29+
);
30+
}, 35_000);
31+
32+
afterAll(async () => {
33+
await helper.run(`wrangler delete --name ${remoteWorkerName}`);
34+
});
35+
36+
describe("startRemoteProxySession", () => {
37+
function getMfOptions(
38+
remoteProxyConnectionString: RemoteProxyConnectionString
39+
): MiniflareOptions {
40+
return {
41+
modules: true,
42+
script: `
43+
export default {
44+
async fetch(req, env) {
45+
const myServiceMsg = !env.MY_SERVICE ? null : await (await env.MY_SERVICE.fetch(req)).text();
46+
return new Response("worker response: " + (myServiceMsg ?? ""));
47+
}
48+
}`,
49+
serviceBindings: {
50+
MY_SERVICE: {
51+
name: remoteWorkerName,
52+
remoteProxyConnectionString,
53+
},
54+
},
55+
};
56+
}
57+
58+
test("base usage", async () => {
59+
const remoteProxySession = await startRemoteProxySession({
60+
MY_SERVICE: {
61+
type: "service",
62+
service: remoteWorkerName,
63+
},
64+
});
65+
await remoteProxySession.ready;
66+
67+
const mf = new Miniflare(
68+
getMfOptions(remoteProxySession.remoteProxyConnectionString)
69+
);
70+
71+
const response = await timedDispatchFetch(mf);
72+
const responseText = await response?.text();
73+
74+
expect(responseText).toEqual(
75+
"worker response: Hello from a remote worker"
76+
);
77+
78+
await mf.dispose();
79+
await remoteProxySession.dispose();
80+
});
81+
82+
test("user provided (incorrect but then corrected) auth data", async () => {
83+
const remoteProxySession = await startRemoteProxySession(
84+
{
85+
MY_SERVICE: {
86+
type: "service",
87+
service: remoteWorkerName,
88+
},
89+
},
90+
{
91+
auth: {
92+
accountId: CLOUDFLARE_ACCOUNT_ID,
93+
apiToken: {
94+
apiToken: "This is an incorrect API TOKEN!",
95+
},
96+
},
97+
}
98+
);
99+
await remoteProxySession.ready;
100+
101+
const mf = new Miniflare(
102+
getMfOptions(remoteProxySession.remoteProxyConnectionString)
103+
);
104+
105+
const noResponse = await timedDispatchFetch(mf);
106+
// We are unable to fetch from the worker since the remote connection is not correctly established
107+
expect(noResponse).toBe(null);
108+
109+
assert(process.env.CLOUDFLARE_API_TOKEN);
110+
111+
const amendedRemoteProxySession = await startRemoteProxySession(
112+
{
113+
MY_SERVICE: {
114+
type: "service",
115+
service: remoteWorkerName,
116+
},
117+
},
118+
{
119+
auth: {
120+
accountId: CLOUDFLARE_ACCOUNT_ID,
121+
apiToken: {
122+
apiToken: process.env.CLOUDFLARE_API_TOKEN,
123+
},
124+
},
125+
}
126+
);
127+
await amendedRemoteProxySession.ready;
128+
129+
await mf.setOptions(
130+
getMfOptions(amendedRemoteProxySession.remoteProxyConnectionString)
131+
);
132+
133+
const response = await timedDispatchFetch(mf);
134+
const responseText = await response?.text();
135+
136+
expect(responseText).toEqual(
137+
"worker response: Hello from a remote worker"
138+
);
139+
140+
await mf.dispose();
141+
await remoteProxySession.dispose();
142+
await amendedRemoteProxySession.dispose();
143+
});
144+
});
145+
}
146+
);
147+
148+
async function timedDispatchFetch(mf: Miniflare): Promise<Response | null> {
149+
try {
150+
return await mf.dispatchFetch("http://localhost/", {
151+
signal: AbortSignal.timeout(5000),
152+
});
153+
} catch {
154+
return null;
155+
}
156+
}

0 commit comments

Comments
 (0)