@@ -41,6 +41,7 @@ import {
4141 triggerPromise ,
4242 useMiniflare ,
4343 useMiniflareWithHandler ,
44+ useServer ,
4445 useTmp ,
4546 utf8Decode ,
4647 utf8Encode ,
@@ -1087,17 +1088,29 @@ test("MiniflareCore: dispatchFetch: rewrites url to match upstream if different"
10871088 new globals . Response ( `${ req . url } :${ req . headers . get ( "host" ) } ` )
10881089 ) ;
10891090 // Check url and host header are correct
1090- let res = await mf . dispatchFetch ( "http://localhost/a" ) ;
1091- t . is ( await res . text ( ) , "https://miniflare.dev/a:miniflare.dev" ) ;
1091+ const init : RequestInit = { headers : { host : "localhost" } } ;
1092+ let res = await mf . dispatchFetch ( "http://localhost/a" , init ) ;
1093+ t . is ( await res . text ( ) , "https://miniflare.dev/a:localhost" ) ;
10921094
10931095 // Check includes query string
1094- res = await mf . dispatchFetch ( "http://localhost/a?b=c" ) ;
1095- t . is ( await res . text ( ) , "https://miniflare.dev/a?b=c:miniflare.dev " ) ;
1096+ res = await mf . dispatchFetch ( "http://localhost/a?b=c" , init ) ;
1097+ t . is ( await res . text ( ) , "https://miniflare.dev/a?b=c:localhost " ) ;
10961098
10971099 // Check includes subpath
10981100 await mf . setOptions ( { upstream : "https://miniflare.dev/subpath/" } ) ;
1099- res = await mf . dispatchFetch ( "http://localhost/a" ) ;
1100- t . is ( await res . text ( ) , "https://miniflare.dev/subpath/a:miniflare.dev" ) ;
1101+ res = await mf . dispatchFetch ( "http://localhost/a" , init ) ;
1102+ t . is ( await res . text ( ) , "https://miniflare.dev/subpath/a:localhost" ) ;
1103+ } ) ;
1104+ test ( "MiniflareCore: dispatchFetch: fetching incoming request responds with upstream" , async ( t ) => {
1105+ const upstream = ( await useServer ( t , ( req , res ) => res . end ( "upstream" ) ) ) . http ;
1106+ const mf = useMiniflareWithHandler (
1107+ { } ,
1108+ { upstream : upstream . toString ( ) } ,
1109+ ( globals , req ) => globals . fetch ( req )
1110+ ) ;
1111+ // Host should be rewritten to match upstream
1112+ const res = await mf . dispatchFetch ( "https://random.mf/" ) ;
1113+ t . is ( await res . text ( ) , "upstream" ) ;
11011114} ) ;
11021115test ( "MiniflareCore: dispatchFetch: request gets immutable headers" , async ( t ) => {
11031116 const mf = useMiniflareWithHandler ( { } , { } , ( globals , req ) => {
0 commit comments