@@ -223,120 +223,122 @@ for (const {
223223 expect ( response . headers . get ( 'x-runtime' ) ) . toEqual ( expectedRuntime )
224224 } )
225225 } )
226- } )
227- }
228-
229- describe ( "aborts middleware execution when the matcher conditions don't match the request" , ( ) => {
230- test < FixtureTestContext > ( 'when the path is excluded' , async ( ctx ) => {
231- await createFixture ( 'middleware' , ctx )
232- await runPlugin ( ctx )
233226
234- const origin = await LocalServer . run ( async ( req , res ) => {
235- expect ( req . url ) . toBe ( '/_next/data' )
236- expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
237-
238- res . write ( 'Hello from origin!' )
239- res . end ( )
240- } )
227+ describe ( "aborts middleware execution when the matcher conditions don't match the request" , ( ) => {
228+ test < FixtureTestContext > ( 'when the path is excluded' , async ( ctx ) => {
229+ await createFixture ( 'middleware' , ctx )
230+ await runPlugin ( ctx , runPluginConstants )
241231
242- ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
232+ const origin = await LocalServer . run ( async ( req , res ) => {
233+ expect ( req . url ) . toBe ( '/_next/data' )
234+ expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
243235
244- const response = await invokeEdgeFunction ( ctx , {
245- functions : [ EDGE_MIDDLEWARE_FUNCTION_NAME ] ,
246- origin,
247- url : '/_next/data' ,
248- } )
236+ res . write ( 'Hello from origin!' )
237+ res . end ( )
238+ } )
249239
250- expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
251- expect ( response . status ) . toBe ( 200 )
252- expect ( response . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeFalsy ( )
253- expect ( origin . calls ) . toBe ( 1 )
254- } )
240+ ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
255241
256- test < FixtureTestContext > ( 'when a request header matches a condition' , async ( ctx ) => {
257- await createFixture ( 'middleware-conditions' , ctx )
258- await runPlugin ( ctx )
242+ const response = await invokeEdgeFunction ( ctx , {
243+ functions : [ edgeFunctionNameRoot ] ,
244+ origin,
245+ url : '/_next/data' ,
246+ } )
259247
260- const origin = await LocalServer . run ( async ( req , res ) => {
261- expect ( req . url ) . toBe ( '/foo' )
262- expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
248+ expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
249+ expect ( response . status ) . toBe ( 200 )
250+ expect ( response . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeFalsy ( )
251+ expect ( origin . calls ) . toBe ( 1 )
252+ } )
263253
264- res . write ( 'Hello from origin!' )
265- res . end ( )
266- } )
254+ test < FixtureTestContext > ( 'when a request header matches a condition' , async ( ctx ) => {
255+ await createFixture ( 'middleware-conditions' , ctx )
256+ await runPlugin ( ctx , runPluginConstants )
267257
268- ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
258+ const origin = await LocalServer . run ( async ( req , res ) => {
259+ expect ( req . url ) . toBe ( '/foo' )
260+ expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
269261
270- // Request 1: Middleware should run because we're not sending the header.
271- const response1 = await invokeEdgeFunction ( ctx , {
272- functions : [ EDGE_MIDDLEWARE_FUNCTION_NAME ] ,
273- origin,
274- url : '/foo' ,
275- } )
262+ res . write ( 'Hello from origin!' )
263+ res . end ( )
264+ } )
276265
277- expect ( await response1 . text ( ) ) . toBe ( 'Hello from origin!' )
278- expect ( response1 . status ) . toBe ( 200 )
279- expect ( response1 . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeTruthy ( )
280- expect ( origin . calls ) . toBe ( 1 )
266+ ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
281267
282- // Request 2: Middleware should not run because we're sending the header.
283- const response2 = await invokeEdgeFunction ( ctx , {
284- headers : {
285- 'x-custom-header' : 'custom-value' ,
286- } ,
287- functions : [ EDGE_MIDDLEWARE_FUNCTION_NAME ] ,
288- origin,
289- url : '/foo' ,
290- } )
268+ // Request 1: Middleware should run because we're not sending the header.
269+ const response1 = await invokeEdgeFunction ( ctx , {
270+ functions : [ edgeFunctionNameRoot ] ,
271+ origin,
272+ url : '/foo' ,
273+ } )
291274
292- expect ( await response2 . text ( ) ) . toBe ( 'Hello from origin!' )
293- expect ( response2 . status ) . toBe ( 200 )
294- expect ( response2 . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeFalsy ( )
295- expect ( origin . calls ) . toBe ( 2 )
296- } )
275+ expect ( await response1 . text ( ) ) . toBe ( 'Hello from origin!' )
276+ expect ( response1 . status ) . toBe ( 200 )
277+ expect ( response1 . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeTruthy ( )
278+ expect ( response1 . headers . get ( 'x-runtime' ) ) . toEqual ( expectedRuntime )
279+ expect ( origin . calls ) . toBe ( 1 )
280+
281+ // Request 2: Middleware should not run because we're sending the header.
282+ const response2 = await invokeEdgeFunction ( ctx , {
283+ headers : {
284+ 'x-custom-header' : 'custom-value' ,
285+ } ,
286+ functions : [ edgeFunctionNameRoot ] ,
287+ origin,
288+ url : '/foo' ,
289+ } )
297290
298- test < FixtureTestContext > ( 'should handle locale matching correctly' , async ( ctx ) => {
299- await createFixture ( 'middleware-conditions' , ctx )
300- await runPlugin ( ctx )
291+ expect ( await response2 . text ( ) ) . toBe ( 'Hello from origin!' )
292+ expect ( response2 . status ) . toBe ( 200 )
293+ expect ( response2 . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeFalsy ( )
294+ expect ( origin . calls ) . toBe ( 2 )
295+ } )
301296
302- const origin = await LocalServer . run ( async ( req , res ) => {
303- expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
297+ test < FixtureTestContext > ( 'should handle locale matching correctly' , async ( ctx ) => {
298+ await createFixture ( 'middleware-conditions' , ctx )
299+ await runPlugin ( ctx , runPluginConstants )
304300
305- res . write ( 'Hello from origin!' )
306- res . end ( )
307- } )
301+ const origin = await LocalServer . run ( async ( req , res ) => {
302+ expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
308303
309- ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
304+ res . write ( 'Hello from origin!' )
305+ res . end ( )
306+ } )
310307
311- for ( const path of [ '/hello' , '/en/hello' , '/nl/hello' , '/nl/about' ] ) {
312- const response = await invokeEdgeFunction ( ctx , {
313- functions : [ EDGE_MIDDLEWARE_FUNCTION_NAME ] ,
314- origin,
315- url : path ,
316- } )
317- expect (
318- response . headers . has ( 'x-hello-from-middleware-res' ) ,
319- `should match ${ path } ` ,
320- ) . toBeTruthy ( )
321- expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
322- expect ( response . status ) . toBe ( 200 )
323- }
308+ ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
324309
325- for ( const path of [ '/invalid/hello' , '/hello/invalid' , '/about' , '/en/about' ] ) {
326- const response = await invokeEdgeFunction ( ctx , {
327- functions : [ EDGE_MIDDLEWARE_FUNCTION_NAME ] ,
328- origin,
329- url : path ,
310+ for ( const path of [ '/hello' , '/en/hello' , '/nl/hello' , '/nl/about' ] ) {
311+ const response = await invokeEdgeFunction ( ctx , {
312+ functions : [ edgeFunctionNameRoot ] ,
313+ origin,
314+ url : path ,
315+ } )
316+ expect (
317+ response . headers . has ( 'x-hello-from-middleware-res' ) ,
318+ `should match ${ path } ` ,
319+ ) . toBeTruthy ( )
320+ expect ( response . headers . get ( 'x-runtime' ) ) . toEqual ( expectedRuntime )
321+ expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
322+ expect ( response . status ) . toBe ( 200 )
323+ }
324+
325+ for ( const path of [ '/invalid/hello' , '/hello/invalid' , '/about' , '/en/about' ] ) {
326+ const response = await invokeEdgeFunction ( ctx , {
327+ functions : [ edgeFunctionNameRoot ] ,
328+ origin,
329+ url : path ,
330+ } )
331+ expect (
332+ response . headers . has ( 'x-hello-from-middleware-res' ) ,
333+ `should not match ${ path } ` ,
334+ ) . toBeFalsy ( )
335+ expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
336+ expect ( response . status ) . toBe ( 200 )
337+ }
330338 } )
331- expect (
332- response . headers . has ( 'x-hello-from-middleware-res' ) ,
333- `should not match ${ path } ` ,
334- ) . toBeFalsy ( )
335- expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
336- expect ( response . status ) . toBe ( 200 )
337- }
339+ } )
338340 } )
339- } )
341+ }
340342
341343describe ( 'should run middleware on data requests' , ( ) => {
342344 test < FixtureTestContext > ( 'when `trailingSlash: false`' , async ( ctx ) => {
0 commit comments