@@ -377,4 +377,163 @@ describe("unit tests", async () => {
377377 const response = await worker . fetch ( request , env , ctx ) ;
378378 expect ( response . status ) . toBe ( 304 ) ;
379379 } ) ;
380+
381+ describe ( "free tier limiting" , ( ) => {
382+ it ( "returns fetch from asset worker for assets" , async ( ) => {
383+ const request = new Request ( "https://example.com/asset" ) ;
384+ const ctx = createExecutionContext ( ) ;
385+
386+ const env = {
387+ CONFIG : {
388+ has_user_worker : true ,
389+ } ,
390+ EYEBALL_CONFIG : { limitedAssetsOnly : true } ,
391+ USER_WORKER : {
392+ async fetch ( _ : Request ) : Promise < Response > {
393+ return new Response ( "hello from user worker" ) ;
394+ } ,
395+ } ,
396+ ASSET_WORKER : {
397+ async fetch ( _ : Request ) : Promise < Response > {
398+ return new Response ( "hello from asset worker" ) ;
399+ } ,
400+ async unstable_canFetch ( _ : Request ) : Promise < boolean > {
401+ return true ;
402+ } ,
403+ } ,
404+ } as Env ;
405+
406+ const response = await worker . fetch ( request , env , ctx ) ;
407+ expect ( await response . text ( ) ) . toEqual ( "hello from asset worker" ) ;
408+ } ) ;
409+
410+ it ( "returns error page instead of user worker when no asset found" , async ( ) => {
411+ const request = new Request ( "https://example.com/asset" ) ;
412+ const ctx = createExecutionContext ( ) ;
413+
414+ const env = {
415+ CONFIG : {
416+ has_user_worker : true ,
417+ } ,
418+ EYEBALL_CONFIG : { limitedAssetsOnly : true } ,
419+ USER_WORKER : {
420+ async fetch ( _ : Request ) : Promise < Response > {
421+ return new Response ( "hello from user worker" ) ;
422+ } ,
423+ } ,
424+ ASSET_WORKER : {
425+ async fetch ( _ : Request ) : Promise < Response > {
426+ return new Response ( "hello from asset worker" ) ;
427+ } ,
428+ async unstable_canFetch ( _ : Request ) : Promise < boolean > {
429+ return false ;
430+ } ,
431+ } ,
432+ } as Env ;
433+
434+ const response = await worker . fetch ( request , env , ctx ) ;
435+ expect ( response . status ) . toEqual ( 429 ) ;
436+ const text = await response . text ( ) ;
437+ expect ( text ) . not . toEqual ( "hello from user worker" ) ;
438+ expect ( text ) . toContain ( "This website has been temporarily rate limited" ) ;
439+ } ) ;
440+
441+ it ( "returns error page instead of user worker for invoke_user_worker_ahead_of_assets" , async ( ) => {
442+ const request = new Request ( "https://example.com/asset" ) ;
443+ const ctx = createExecutionContext ( ) ;
444+
445+ const env = {
446+ CONFIG : {
447+ has_user_worker : true ,
448+ invoke_user_worker_ahead_of_assets : true ,
449+ } ,
450+ EYEBALL_CONFIG : { limitedAssetsOnly : true } ,
451+ USER_WORKER : {
452+ async fetch ( _ : Request ) : Promise < Response > {
453+ return new Response ( "hello from user worker" ) ;
454+ } ,
455+ } ,
456+ ASSET_WORKER : {
457+ async fetch ( _ : Request ) : Promise < Response > {
458+ return new Response ( "hello from asset worker" ) ;
459+ } ,
460+ async unstable_canFetch ( _ : Request ) : Promise < boolean > {
461+ return true ;
462+ } ,
463+ } ,
464+ } as Env ;
465+
466+ const response = await worker . fetch ( request , env , ctx ) ;
467+ expect ( response . status ) . toEqual ( 429 ) ;
468+ const text = await response . text ( ) ;
469+ expect ( text ) . not . toEqual ( "hello from user worker" ) ;
470+ expect ( text ) . toContain ( "This website has been temporarily rate limited" ) ;
471+ } ) ;
472+
473+ it ( "returns error page instead of user worker for user_worker rules" , async ( ) => {
474+ const request = new Request ( "https://example.com/api/asset" ) ;
475+ const ctx = createExecutionContext ( ) ;
476+
477+ const env = {
478+ CONFIG : {
479+ has_user_worker : true ,
480+ static_routing : {
481+ user_worker : [ "/api/*" ] ,
482+ } ,
483+ } ,
484+ EYEBALL_CONFIG : { limitedAssetsOnly : true } ,
485+ USER_WORKER : {
486+ async fetch ( _ : Request ) : Promise < Response > {
487+ return new Response ( "hello from user worker" ) ;
488+ } ,
489+ } ,
490+ ASSET_WORKER : {
491+ async fetch ( _ : Request ) : Promise < Response > {
492+ return new Response ( "hello from asset worker" ) ;
493+ } ,
494+ async unstable_canFetch ( _ : Request ) : Promise < boolean > {
495+ return true ;
496+ } ,
497+ } ,
498+ } as Env ;
499+
500+ const response = await worker . fetch ( request , env , ctx ) ;
501+ expect ( response . status ) . toEqual ( 429 ) ;
502+ const text = await response . text ( ) ;
503+ expect ( text ) . not . toEqual ( "hello from user worker" ) ;
504+ expect ( text ) . toContain ( "This website has been temporarily rate limited" ) ;
505+ } ) ;
506+
507+ it ( "returns fetch from asset worker for asset_worker rules" , async ( ) => {
508+ const request = new Request ( "https://example.com/api/asset" ) ;
509+ const ctx = createExecutionContext ( ) ;
510+
511+ const env = {
512+ CONFIG : {
513+ has_user_worker : true ,
514+ static_routing : {
515+ user_worker : [ "/api/*" ] ,
516+ asset_worker : [ "/api/asset" ] ,
517+ } ,
518+ } ,
519+ EYEBALL_CONFIG : { limitedAssetsOnly : true } ,
520+ USER_WORKER : {
521+ async fetch ( _ : Request ) : Promise < Response > {
522+ return new Response ( "hello from user worker" ) ;
523+ } ,
524+ } ,
525+ ASSET_WORKER : {
526+ async fetch ( _ : Request ) : Promise < Response > {
527+ return new Response ( "hello from asset worker" ) ;
528+ } ,
529+ async unstable_canFetch ( _ : Request ) : Promise < boolean > {
530+ return true ;
531+ } ,
532+ } ,
533+ } as Env ;
534+
535+ const response = await worker . fetch ( request , env , ctx ) ;
536+ expect ( await response . text ( ) ) . toEqual ( "hello from asset worker" ) ;
537+ } ) ;
538+ } ) ;
380539} ) ;
0 commit comments