@@ -2,6 +2,7 @@ import assert from "assert";
22import fs from "fs/promises" ;
33import path from "path" ;
44import { setTimeout } from "timers/promises" ;
5+ import { CachePlugin } from "@miniflare/cache" ;
56import {
67 BindingsPlugin ,
78 BuildPlugin ,
@@ -25,6 +26,7 @@ import {
2526 Options ,
2627 Storage ,
2728 TypedEventListener ,
29+ getRequestContext ,
2830} from "@miniflare/shared" ;
2931import {
3032 LogEntry ,
@@ -44,6 +46,7 @@ import {
4446import test , { Macro } from "ava" ;
4547import { Request as BaseRequest , File , FormData } from "undici" ;
4648
49+ const log = new NoOpLog ( ) ;
4750// Only use this shared storage factory when the test doesn't care about storage
4851const storageFactory = new MemoryStorageFactory ( ) ;
4952const scriptRunner = new VMScriptRunner ( ) ;
@@ -1085,6 +1088,37 @@ test("MiniflareCore: dispatchFetch: Request parse files in FormData as File obje
10851088 t . is ( await file . text ( ) , "test" ) ;
10861089 t . is ( file . name , "test.txt" ) ;
10871090} ) ;
1091+ test ( "MiniflareCore: dispatchFetch: creates new request context" , async ( t ) => {
1092+ const mf = useMiniflareWithHandler (
1093+ { BindingsPlugin, CachePlugin } ,
1094+ {
1095+ globals : {
1096+ assertSubrequests ( expected : number ) {
1097+ t . is ( getRequestContext ( ) ?. subrequests , expected ) ;
1098+ } ,
1099+ } ,
1100+ } ,
1101+ async ( globals , req ) => {
1102+ globals . assertSubrequests ( 0 ) ;
1103+ await globals . caches . default . match ( "http://localhost/" ) ;
1104+ globals . assertSubrequests ( 1 ) ;
1105+
1106+ const n = parseInt ( new globals . URL ( req . url ) . searchParams . get ( "n" ) ) ;
1107+ await Promise . all (
1108+ Array . from ( Array ( n ) ) . map ( ( ) =>
1109+ globals . caches . default . match ( "http://localhost/" )
1110+ )
1111+ ) ;
1112+ return new globals . Response ( "body" ) ;
1113+ }
1114+ ) ;
1115+ await t . throwsAsync ( mf . dispatchFetch ( "http://localhost/?n=50" ) , {
1116+ instanceOf : Error ,
1117+ message : / ^ T o o m a n y s u b r e q u e s t s / ,
1118+ } ) ;
1119+ const res = await mf . dispatchFetch ( "http://localhost/?n=1" ) ;
1120+ t . is ( await res . text ( ) , "body" ) ;
1121+ } ) ;
10881122
10891123test ( "MiniflareCore: dispatchScheduled: dispatches scheduled event" , async ( t ) => {
10901124 const mf = useMiniflare (
@@ -1101,6 +1135,40 @@ test("MiniflareCore: dispatchScheduled: dispatches scheduled event", async (t) =
11011135 const res = await mf . dispatchScheduled ( 1000 , "30 * * * *" ) ;
11021136 t . deepEqual ( res , [ 1000 , "30 * * * *" ] ) ;
11031137} ) ;
1138+ test ( "MiniflareCore: dispatchScheduled: creates new request context" , async ( t ) => {
1139+ const mf = new MiniflareCore (
1140+ { CorePlugin, BindingsPlugin, CachePlugin } ,
1141+ { log, storageFactory, scriptRunner } ,
1142+ {
1143+ globals : {
1144+ assertSubrequests ( expected : number ) {
1145+ t . is ( getRequestContext ( ) ?. subrequests , expected ) ;
1146+ } ,
1147+ } ,
1148+ modules : true ,
1149+ script : `export default {
1150+ async scheduled(controller) {
1151+ assertSubrequests(0);
1152+ await caches.default.match("http://localhost/");
1153+ assertSubrequests(1);
1154+
1155+ await Promise.all(
1156+ Array.from(Array(controller.scheduledTime)).map(() =>
1157+ caches.default.match("http://localhost/")
1158+ )
1159+ );
1160+ return true;
1161+ }
1162+ }` ,
1163+ }
1164+ ) ;
1165+ await t . throwsAsync ( mf . dispatchScheduled ( 50 ) , {
1166+ instanceOf : Error ,
1167+ message : / ^ T o o m a n y s u b r e q u e s t s / ,
1168+ } ) ;
1169+ const waitUntil = await mf . dispatchScheduled ( 1 ) ;
1170+ t . true ( waitUntil [ 0 ] ) ;
1171+ } ) ;
11041172
11051173test ( "MiniflareCore: dispose: runs dispose for all plugins" , async ( t ) => {
11061174 const log = new TestLog ( ) ;
0 commit comments