@@ -94,4 +94,161 @@ describe("unit tests", async () => {
9494 const response = await worker . fetch ( request , env , ctx ) ;
9595 expect ( await response . text ( ) ) . toEqual ( "hello from asset worker" ) ;
9696 } ) ;
97+
98+ it ( "it returns fetch from user worker when static_routing include rule matches" , async ( ) => {
99+ const request = new Request ( "https://example.com/api/includeme" ) ;
100+ const ctx = createExecutionContext ( ) ;
101+
102+ const env = {
103+ CONFIG : {
104+ has_user_worker : true ,
105+ static_routing : {
106+ version : 1 ,
107+ include : [ "/api/*" ] ,
108+ } ,
109+ } ,
110+ USER_WORKER : {
111+ async fetch ( _ : Request ) : Promise < Response > {
112+ return new Response ( "hello from user worker" ) ;
113+ } ,
114+ } ,
115+ ASSET_WORKER : {
116+ async fetch ( _ : Request ) : Promise < Response > {
117+ return new Response ( "hello from asset worker" ) ;
118+ } ,
119+ async unstable_canFetch ( _ : Request ) : Promise < boolean > {
120+ return true ;
121+ } ,
122+ } ,
123+ } as Env ;
124+
125+ const response = await worker . fetch ( request , env , ctx ) ;
126+ expect ( await response . text ( ) ) . toEqual ( "hello from user worker" ) ;
127+ } ) ;
128+
129+ it ( "it returns fetch from asset worker when static_routing exclude rule matches" , async ( ) => {
130+ const request = new Request ( "https://example.com/api/excludeme" ) ;
131+ const ctx = createExecutionContext ( ) ;
132+
133+ const env = {
134+ CONFIG : {
135+ has_user_worker : true ,
136+ static_routing : {
137+ version : 1 ,
138+ include : [ "/api/includeme" ] ,
139+ exclude : [ "/api/excludeme" ] ,
140+ } ,
141+ } ,
142+ USER_WORKER : {
143+ async fetch ( _ : Request ) : Promise < Response > {
144+ return new Response ( "hello from user worker" ) ;
145+ } ,
146+ } ,
147+ ASSET_WORKER : {
148+ async fetch ( _ : Request ) : Promise < Response > {
149+ return new Response ( "hello from asset worker" ) ;
150+ } ,
151+ async unstable_canFetch ( _ : Request ) : Promise < boolean > {
152+ return true ;
153+ } ,
154+ } ,
155+ } as Env ;
156+
157+ const response = await worker . fetch ( request , env , ctx ) ;
158+ expect ( await response . text ( ) ) . toEqual ( "hello from asset worker" ) ;
159+ } ) ;
160+
161+ it ( "it returns fetch from asset worker when static_routing exclude and include rule matches" , async ( ) => {
162+ const request = new Request ( "https://example.com/api/excludeme" ) ;
163+ const ctx = createExecutionContext ( ) ;
164+
165+ const env = {
166+ CONFIG : {
167+ has_user_worker : true ,
168+ static_routing : {
169+ version : 1 ,
170+ include : [ "/api/*" ] ,
171+ exclude : [ "/api/excludeme" ] ,
172+ } ,
173+ } ,
174+ USER_WORKER : {
175+ async fetch ( _ : Request ) : Promise < Response > {
176+ return new Response ( "hello from user worker" ) ;
177+ } ,
178+ } ,
179+ ASSET_WORKER : {
180+ async fetch ( _ : Request ) : Promise < Response > {
181+ return new Response ( "hello from asset worker" ) ;
182+ } ,
183+ async unstable_canFetch ( _ : Request ) : Promise < boolean > {
184+ return true ;
185+ } ,
186+ } ,
187+ } as Env ;
188+
189+ const response = await worker . fetch ( request , env , ctx ) ;
190+ expect ( await response . text ( ) ) . toEqual ( "hello from asset worker" ) ;
191+ } ) ;
192+
193+ it ( "it returns fetch from asset worker when no static_routing rule matches but asset exists" , async ( ) => {
194+ const request = new Request ( "https://example.com/someasset" ) ;
195+ const ctx = createExecutionContext ( ) ;
196+
197+ const env = {
198+ CONFIG : {
199+ has_user_worker : true ,
200+ static_routing : {
201+ version : 1 ,
202+ include : [ "/api/*" ] ,
203+ } ,
204+ } ,
205+ USER_WORKER : {
206+ async fetch ( _ : Request ) : Promise < Response > {
207+ return new Response ( "hello from user worker" ) ;
208+ } ,
209+ } ,
210+ ASSET_WORKER : {
211+ async fetch ( _ : Request ) : Promise < Response > {
212+ return new Response ( "hello from asset worker" ) ;
213+ } ,
214+ async unstable_canFetch ( _ : Request ) : Promise < boolean > {
215+ return true ;
216+ } ,
217+ } ,
218+ } as Env ;
219+
220+ const response = await worker . fetch ( request , env , ctx ) ;
221+ expect ( await response . text ( ) ) . toEqual ( "hello from asset worker" ) ;
222+ } ) ;
223+
224+ it ( "it returns fetch from user worker when no static_routing rule matches and no asset exists" , async ( ) => {
225+ const request = new Request ( "https://example.com/somemissingasset" ) ;
226+ const ctx = createExecutionContext ( ) ;
227+
228+ const env = {
229+ CONFIG : {
230+ has_user_worker : true ,
231+ static_routing : {
232+ version : 1 ,
233+ include : [ "/api/*" ] ,
234+ } ,
235+ } ,
236+ USER_WORKER : {
237+ async fetch ( _ : Request ) : Promise < Response > {
238+ return new Response ( "hello from user worker" ) ;
239+ } ,
240+ } ,
241+ ASSET_WORKER : {
242+ async fetch ( _ : Request ) : Promise < Response > {
243+ return new Response ( "hello from asset worker" ) ;
244+ } ,
245+ async unstable_canFetch ( _ : Request ) : Promise < boolean > {
246+ return false ;
247+ } ,
248+ } ,
249+ } as Env ;
250+
251+ const response = await worker . fetch ( request , env , ctx ) ;
252+ expect ( await response . text ( ) ) . toEqual ( "hello from user worker" ) ;
253+ } ) ;
97254} ) ;
0 commit comments