@@ -11,6 +11,10 @@ import type {
1111 Response ,
1212} from "miniflare" ;
1313
14+ // Note: the tests in this file are simple ones that check basic functionalities of the remote bindings programmatic APIs
15+ // various other aspects of these APIs (e.g. different bindings, reloading capabilities) are indirectly tested when
16+ // generally testing remote bindings
17+
1418describe . skipIf ( ! CLOUDFLARE_ACCOUNT_ID ) (
1519 "wrangler dev - remote bindings - programmatic API" ,
1620 async ( ) => {
@@ -19,8 +23,11 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
1923
2024 const { Miniflare } = await helper . importMiniflare ( ) ;
2125
22- const { experimental_startRemoteProxySession : startRemoteProxySession } =
23- await helper . importWrangler ( ) ;
26+ const {
27+ experimental_startRemoteProxySession : startRemoteProxySession ,
28+ experimental_maybeStartOrUpdateRemoteProxySession :
29+ maybeStartOrUpdateRemoteProxySession ,
30+ } = await helper . importWrangler ( ) ;
2431
2532 beforeAll ( async ( ) => {
2633 await helper . seed ( resolve ( __dirname , "./workers" ) ) ;
@@ -33,28 +40,28 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
3340 await helper . run ( `wrangler delete --name ${ remoteWorkerName } ` ) ;
3441 } ) ;
3542
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- } ,
43+ function getMfOptions (
44+ remoteProxyConnectionString : RemoteProxyConnectionString
45+ ) : MiniflareOptions {
46+ return {
47+ modules : true ,
48+ script : `
49+ export default {
50+ async fetch(req, env) {
51+ const myServiceMsg = !env.MY_SERVICE ? null : await (await env.MY_SERVICE.fetch(req)).text();
52+ return new Response("worker response: " + (myServiceMsg ?? ""));
53+ }
54+ }` ,
55+ serviceBindings : {
56+ MY_SERVICE : {
57+ name : remoteWorkerName ,
58+ remoteProxyConnectionString,
5459 } ,
55- } ;
56- }
60+ } ,
61+ } ;
62+ }
5763
64+ describe ( "startRemoteProxySession" , ( ) => {
5865 test ( "base usage" , async ( ) => {
5966 const remoteProxySession = await startRemoteProxySession ( {
6067 MY_SERVICE : {
@@ -124,6 +131,7 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
124131 } ,
125132 }
126133 ) ;
134+
127135 await amendedRemoteProxySession . ready ;
128136
129137 await mf . setOptions (
@@ -142,6 +150,108 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
142150 await amendedRemoteProxySession . dispose ( ) ;
143151 } ) ;
144152 } ) ;
153+
154+ describe ( "maybeStartOrUpdateRemoteProxySession" , ( ) => {
155+ test ( "base usage" , async ( ) => {
156+ const proxySessionData = await maybeStartOrUpdateRemoteProxySession ( {
157+ bindings : {
158+ MY_SERVICE : {
159+ type : "service" ,
160+ service : remoteWorkerName ,
161+ experimental_remote : true ,
162+ } ,
163+ } ,
164+ } ) ;
165+
166+ assert ( proxySessionData ) ;
167+
168+ await proxySessionData . session . ready ;
169+
170+ const mf = new Miniflare (
171+ getMfOptions ( proxySessionData . session . remoteProxyConnectionString )
172+ ) ;
173+
174+ const response = await timedDispatchFetch ( mf ) ;
175+ const responseText = await response ?. text ( ) ;
176+
177+ expect ( responseText ) . toEqual (
178+ "worker response: Hello from a remote worker"
179+ ) ;
180+
181+ await mf . dispose ( ) ;
182+ await proxySessionData . session . dispose ( ) ;
183+ } ) ;
184+
185+ test ( "user provided (incorrect but then corrected) auth data" , async ( ) => {
186+ let proxySessionData = await maybeStartOrUpdateRemoteProxySession (
187+ {
188+ bindings : {
189+ MY_SERVICE : {
190+ type : "service" ,
191+ service : remoteWorkerName ,
192+ experimental_remote : true ,
193+ } ,
194+ } ,
195+ } ,
196+ undefined ,
197+ {
198+ accountId : CLOUDFLARE_ACCOUNT_ID ,
199+ apiToken : {
200+ apiToken : "This is an incorrect API TOKEN!" ,
201+ } ,
202+ }
203+ ) ;
204+
205+ assert ( proxySessionData ) ;
206+
207+ await proxySessionData . session . ready ;
208+
209+ const mf = new Miniflare (
210+ getMfOptions ( proxySessionData . session . remoteProxyConnectionString )
211+ ) ;
212+
213+ const noResponse = await timedDispatchFetch ( mf ) ;
214+ // We are unable to fetch from the worker since the remote connection is not correctly established
215+ expect ( noResponse ) . toBe ( null ) ;
216+
217+ assert ( process . env . CLOUDFLARE_API_TOKEN ) ;
218+
219+ proxySessionData = await maybeStartOrUpdateRemoteProxySession (
220+ {
221+ bindings : {
222+ MY_SERVICE : {
223+ type : "service" ,
224+ service : remoteWorkerName ,
225+ experimental_remote : true ,
226+ } ,
227+ } ,
228+ } ,
229+ proxySessionData ,
230+ {
231+ accountId : CLOUDFLARE_ACCOUNT_ID ,
232+ apiToken : {
233+ apiToken : process . env . CLOUDFLARE_API_TOKEN ,
234+ } ,
235+ }
236+ ) ;
237+
238+ assert ( proxySessionData ) ;
239+
240+ await mf . setOptions (
241+ getMfOptions ( proxySessionData . session . remoteProxyConnectionString )
242+ ) ;
243+
244+ const response = await timedDispatchFetch ( mf ) ;
245+ const responseText = await response ?. text ( ) ;
246+
247+ expect ( responseText ) . toEqual (
248+ "worker response: Hello from a remote worker"
249+ ) ;
250+
251+ await mf . dispose ( ) ;
252+ await proxySessionData . session . dispose ( ) ;
253+ } ) ;
254+ } ) ;
145255 }
146256) ;
147257
0 commit comments