@@ -19,63 +19,69 @@ beforeEach<FixtureTestContext>(async (ctx) => {
19
19
await startMockBlobStore ( ctx )
20
20
} )
21
21
22
- test . skipIf ( platform === "win32" ) < FixtureTestContext > ( 'should add request/response headers' , async ( ctx ) => {
23
- await createFixture ( 'middleware' , ctx )
24
- await runPlugin ( ctx )
22
+ test . skipIf ( platform === 'win32' ) < FixtureTestContext > (
23
+ 'should add request/response headers' ,
24
+ async ( ctx ) => {
25
+ await createFixture ( 'middleware' , ctx )
26
+ await runPlugin ( ctx )
25
27
26
- const origin = await LocalServer . run ( async ( req , res ) => {
27
- expect ( req . url ) . toBe ( '/test/next' )
28
- expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBe ( 'hello' )
28
+ const origin = await LocalServer . run ( async ( req , res ) => {
29
+ expect ( req . url ) . toBe ( '/test/next' )
30
+ expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBe ( 'hello' )
29
31
30
- res . write ( 'Hello from origin!' )
31
- res . end ( )
32
- } )
32
+ res . write ( 'Hello from origin!' )
33
+ res . end ( )
34
+ } )
33
35
34
- ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
36
+ ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
35
37
36
- const response = await invokeEdgeFunction ( ctx , {
37
- functions : [ '___netlify-edge-handler-middleware' ] ,
38
- origin,
39
- url : '/test/next' ,
40
- } )
38
+ const response = await invokeEdgeFunction ( ctx , {
39
+ functions : [ '___netlify-edge-handler-middleware' ] ,
40
+ origin,
41
+ url : '/test/next' ,
42
+ } )
41
43
42
- expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
43
- expect ( response . status ) . toBe ( 200 )
44
- expect ( response . headers . get ( 'x-hello-from-middleware-res' ) , 'added a response header' ) . toEqual (
45
- 'hello' ,
46
- )
47
- expect ( origin . calls ) . toBe ( 1 )
48
- } )
44
+ expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
45
+ expect ( response . status ) . toBe ( 200 )
46
+ expect ( response . headers . get ( 'x-hello-from-middleware-res' ) , 'added a response header' ) . toEqual (
47
+ 'hello' ,
48
+ )
49
+ expect ( origin . calls ) . toBe ( 1 )
50
+ } ,
51
+ )
49
52
50
- test . skipIf ( platform === "win32" ) < FixtureTestContext > ( 'should add request/response headers when using src dir' , async ( ctx ) => {
51
- await createFixture ( 'middleware-src' , ctx )
52
- await runPlugin ( ctx )
53
+ test . skipIf ( platform === 'win32' ) < FixtureTestContext > (
54
+ 'should add request/response headers when using src dir' ,
55
+ async ( ctx ) => {
56
+ await createFixture ( 'middleware-src' , ctx )
57
+ await runPlugin ( ctx )
53
58
54
- const origin = await LocalServer . run ( async ( req , res ) => {
55
- expect ( req . url ) . toBe ( '/test/next' )
56
- expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBe ( 'hello' )
59
+ const origin = await LocalServer . run ( async ( req , res ) => {
60
+ expect ( req . url ) . toBe ( '/test/next' )
61
+ expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBe ( 'hello' )
57
62
58
- res . write ( 'Hello from origin!' )
59
- res . end ( )
60
- } )
63
+ res . write ( 'Hello from origin!' )
64
+ res . end ( )
65
+ } )
61
66
62
- ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
67
+ ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
63
68
64
- const response = await invokeEdgeFunction ( ctx , {
65
- functions : [ '___netlify-edge-handler-src-middleware' ] ,
66
- origin,
67
- url : '/test/next' ,
68
- } )
69
+ const response = await invokeEdgeFunction ( ctx , {
70
+ functions : [ '___netlify-edge-handler-src-middleware' ] ,
71
+ origin,
72
+ url : '/test/next' ,
73
+ } )
69
74
70
- expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
71
- expect ( response . status ) . toBe ( 200 )
72
- expect ( response . headers . get ( 'x-hello-from-middleware-res' ) , 'added a response header' ) . toEqual (
73
- 'hello' ,
74
- )
75
- expect ( origin . calls ) . toBe ( 1 )
76
- } )
75
+ expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
76
+ expect ( response . status ) . toBe ( 200 )
77
+ expect ( response . headers . get ( 'x-hello-from-middleware-res' ) , 'added a response header' ) . toEqual (
78
+ 'hello' ,
79
+ )
80
+ expect ( origin . calls ) . toBe ( 1 )
81
+ } ,
82
+ )
77
83
78
- describe . skipIf ( platform === " win32" ) ( 'redirect' , ( ) => {
84
+ describe . skipIf ( platform === ' win32' ) ( 'redirect' , ( ) => {
79
85
test < FixtureTestContext > ( 'should return a redirect response' , async ( ctx ) => {
80
86
await createFixture ( 'middleware' , ctx )
81
87
await runPlugin ( ctx )
@@ -124,7 +130,7 @@ describe.skipIf(platform === "win32")('redirect', () => {
124
130
} )
125
131
} )
126
132
127
- describe . skipIf ( platform === " win32" ) ( 'rewrite' , ( ) => {
133
+ describe . skipIf ( platform === ' win32' ) ( 'rewrite' , ( ) => {
128
134
test < FixtureTestContext > ( 'should rewrite to an external URL' , async ( ctx ) => {
129
135
await createFixture ( 'middleware' , ctx )
130
136
await runPlugin ( ctx )
@@ -184,116 +190,122 @@ describe.skipIf(platform === "win32")('rewrite', () => {
184
190
} )
185
191
} )
186
192
187
- describe . skipIf ( platform === "win32" ) ( "aborts middleware execution when the matcher conditions don't match the request" , ( ) => {
188
- test < FixtureTestContext > ( 'when the path is excluded' , async ( ctx ) => {
189
- await createFixture ( 'middleware' , ctx )
190
- await runPlugin ( ctx )
193
+ describe . skipIf ( platform === 'win32' ) (
194
+ "aborts middleware execution when the matcher conditions don't match the request" ,
195
+ ( ) => {
196
+ test < FixtureTestContext > ( 'when the path is excluded' , async ( ctx ) => {
197
+ await createFixture ( 'middleware' , ctx )
198
+ await runPlugin ( ctx )
191
199
192
- const origin = await LocalServer . run ( async ( req , res ) => {
193
- expect ( req . url ) . toBe ( '/_next/data' )
194
- expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
200
+ const origin = await LocalServer . run ( async ( req , res ) => {
201
+ expect ( req . url ) . toBe ( '/_next/data' )
202
+ expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
195
203
196
- res . write ( 'Hello from origin!' )
197
- res . end ( )
198
- } )
199
-
200
- ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
201
-
202
- const response = await invokeEdgeFunction ( ctx , {
203
- functions : [ '___netlify-edge-handler-middleware' ] ,
204
- origin,
205
- url : '/_next/data' ,
206
- } )
207
-
208
- expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
209
- expect ( response . status ) . toBe ( 200 )
210
- expect ( response . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeFalsy ( )
211
- expect ( origin . calls ) . toBe ( 1 )
212
- } )
204
+ res . write ( 'Hello from origin!' )
205
+ res . end ( )
206
+ } )
213
207
214
- test < FixtureTestContext > ( 'when a request header matches a condition' , async ( ctx ) => {
215
- await createFixture ( 'middleware-conditions' , ctx )
216
- await runPlugin ( ctx )
208
+ ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
217
209
218
- const origin = await LocalServer . run ( async ( req , res ) => {
219
- expect ( req . url ) . toBe ( '/foo' )
220
- expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
210
+ const response = await invokeEdgeFunction ( ctx , {
211
+ functions : [ '___netlify-edge-handler-middleware' ] ,
212
+ origin,
213
+ url : '/_next/data' ,
214
+ } )
221
215
222
- res . write ( 'Hello from origin!' )
223
- res . end ( )
216
+ expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
217
+ expect ( response . status ) . toBe ( 200 )
218
+ expect ( response . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeFalsy ( )
219
+ expect ( origin . calls ) . toBe ( 1 )
224
220
} )
225
221
226
- ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
222
+ test < FixtureTestContext > ( 'when a request header matches a condition' , async ( ctx ) => {
223
+ await createFixture ( 'middleware-conditions' , ctx )
224
+ await runPlugin ( ctx )
227
225
228
- // Request 1: Middleware should run because we're not sending the header.
229
- const response1 = await invokeEdgeFunction ( ctx , {
230
- functions : [ '___netlify-edge-handler-middleware' ] ,
231
- origin,
232
- url : '/foo' ,
233
- } )
226
+ const origin = await LocalServer . run ( async ( req , res ) => {
227
+ expect ( req . url ) . toBe ( '/foo' )
228
+ expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
234
229
235
- expect ( await response1 . text ( ) ) . toBe ( 'Hello from origin!' )
236
- expect ( response1 . status ) . toBe ( 200 )
237
- expect ( response1 . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeTruthy ( )
238
- expect ( origin . calls ) . toBe ( 1 )
230
+ res . write ( 'Hello from origin!' )
231
+ res . end ( )
232
+ } )
239
233
240
- // Request 2: Middleware should not run because we're sending the header.
241
- const response2 = await invokeEdgeFunction ( ctx , {
242
- headers : {
243
- 'x-custom-header' : 'custom-value' ,
244
- } ,
245
- functions : [ '___netlify-edge-handler-middleware' ] ,
246
- origin,
247
- url : '/foo' ,
248
- } )
234
+ ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
249
235
250
- expect ( await response2 . text ( ) ) . toBe ( 'Hello from origin!' )
251
- expect ( response2 . status ) . toBe ( 200 )
252
- expect ( response2 . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeFalsy ( )
253
- expect ( origin . calls ) . toBe ( 2 )
254
- } )
236
+ // Request 1: Middleware should run because we're not sending the header.
237
+ const response1 = await invokeEdgeFunction ( ctx , {
238
+ functions : [ '___netlify-edge-handler-middleware' ] ,
239
+ origin,
240
+ url : '/foo' ,
241
+ } )
255
242
256
- test < FixtureTestContext > ( 'should handle locale matching correctly' , async ( ctx ) => {
257
- await createFixture ( 'middleware-conditions' , ctx )
258
- await runPlugin ( ctx )
243
+ expect ( await response1 . text ( ) ) . toBe ( 'Hello from origin!' )
244
+ expect ( response1 . status ) . toBe ( 200 )
245
+ expect ( response1 . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeTruthy ( )
246
+ expect ( origin . calls ) . toBe ( 1 )
259
247
260
- const origin = await LocalServer . run ( async ( req , res ) => {
261
- expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
248
+ // Request 2: Middleware should not run because we're sending the header.
249
+ const response2 = await invokeEdgeFunction ( ctx , {
250
+ headers : {
251
+ 'x-custom-header' : 'custom-value' ,
252
+ } ,
253
+ functions : [ '___netlify-edge-handler-middleware' ] ,
254
+ origin,
255
+ url : '/foo' ,
256
+ } )
262
257
263
- res . write ( 'Hello from origin!' )
264
- res . end ( )
258
+ expect ( await response2 . text ( ) ) . toBe ( 'Hello from origin!' )
259
+ expect ( response2 . status ) . toBe ( 200 )
260
+ expect ( response2 . headers . has ( 'x-hello-from-middleware-res' ) ) . toBeFalsy ( )
261
+ expect ( origin . calls ) . toBe ( 2 )
265
262
} )
266
263
267
- ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
264
+ test < FixtureTestContext > ( 'should handle locale matching correctly' , async ( ctx ) => {
265
+ await createFixture ( 'middleware-conditions' , ctx )
266
+ await runPlugin ( ctx )
268
267
269
- for ( const path of [ '/hello' , '/en/hello' , '/nl-NL/hello' , '/nl-NL/about' ] ) {
270
- const response = await invokeEdgeFunction ( ctx , {
271
- functions : [ '___netlify-edge-handler-middleware' ] ,
272
- origin,
273
- url : path ,
274
- } )
275
- expect ( response . headers . has ( 'x-hello-from-middleware-res' ) , `does match ${ path } ` ) . toBeTruthy ( )
276
- expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
277
- expect ( response . status ) . toBe ( 200 )
278
- }
268
+ const origin = await LocalServer . run ( async ( req , res ) => {
269
+ expect ( req . headers [ 'x-hello-from-middleware-req' ] ) . toBeUndefined ( )
279
270
280
- for ( const path of [ '/hello/invalid' , '/about' , '/en/about' ] ) {
281
- const response = await invokeEdgeFunction ( ctx , {
282
- functions : [ '___netlify-edge-handler-middleware' ] ,
283
- origin,
284
- url : path ,
271
+ res . write ( 'Hello from origin!' )
272
+ res . end ( )
285
273
} )
286
- expect (
287
- response . headers . has ( 'x-hello-from-middleware-res' ) ,
288
- `does not match ${ path } ` ,
289
- ) . toBeFalsy ( )
290
- expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
291
- expect ( response . status ) . toBe ( 200 )
292
- }
293
- } )
294
- } )
295
274
296
- describe . skipIf ( platform === "win32" ) ( 'should run middleware on data requests' , ( ) => {
275
+ ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
276
+
277
+ for ( const path of [ '/hello' , '/en/hello' , '/nl-NL/hello' , '/nl-NL/about' ] ) {
278
+ const response = await invokeEdgeFunction ( ctx , {
279
+ functions : [ '___netlify-edge-handler-middleware' ] ,
280
+ origin,
281
+ url : path ,
282
+ } )
283
+ expect (
284
+ response . headers . has ( 'x-hello-from-middleware-res' ) ,
285
+ `does match ${ path } ` ,
286
+ ) . toBeTruthy ( )
287
+ expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
288
+ expect ( response . status ) . toBe ( 200 )
289
+ }
290
+
291
+ for ( const path of [ '/hello/invalid' , '/about' , '/en/about' ] ) {
292
+ const response = await invokeEdgeFunction ( ctx , {
293
+ functions : [ '___netlify-edge-handler-middleware' ] ,
294
+ origin,
295
+ url : path ,
296
+ } )
297
+ expect (
298
+ response . headers . has ( 'x-hello-from-middleware-res' ) ,
299
+ `does not match ${ path } ` ,
300
+ ) . toBeFalsy ( )
301
+ expect ( await response . text ( ) ) . toBe ( 'Hello from origin!' )
302
+ expect ( response . status ) . toBe ( 200 )
303
+ }
304
+ } )
305
+ } ,
306
+ )
307
+
308
+ describe . skipIf ( platform === 'win32' ) ( 'should run middleware on data requests' , ( ) => {
297
309
test < FixtureTestContext > ( 'when `trailingSlash: false`' , async ( ctx ) => {
298
310
await createFixture ( 'middleware' , ctx )
299
311
await runPlugin ( ctx )
@@ -344,6 +356,28 @@ describe.skipIf(platform === "win32")('should run middleware on data requests',
344
356
} )
345
357
346
358
describe ( 'page router' , ( ) => {
359
+ test < FixtureTestContext > ( 'edge api routes should work with middleware' , async ( ctx ) => {
360
+ await createFixture ( 'middleware-pages' , ctx )
361
+ await runPlugin ( ctx )
362
+ const origin = await LocalServer . run ( async ( req , res ) => {
363
+ res . write (
364
+ JSON . stringify ( {
365
+ url : req . url ,
366
+ headers : req . headers ,
367
+ } ) ,
368
+ )
369
+ res . end ( )
370
+ } )
371
+ ctx . cleanup ?. push ( ( ) => origin . stop ( ) )
372
+ const response = await invokeEdgeFunction ( ctx , {
373
+ functions : [ '___netlify-edge-handler-middleware' ] ,
374
+ origin,
375
+ url : `/api/edge-headers` ,
376
+ } )
377
+ const res = await response . json ( )
378
+ expect ( res . url ) . toBe ( '/api/edge-headers' )
379
+ expect ( response . status ) . toBe ( 200 )
380
+ } )
347
381
test < FixtureTestContext > ( 'middleware should rewrite data requests' , async ( ctx ) => {
348
382
await createFixture ( 'middleware-pages' , ctx )
349
383
await runPlugin ( ctx )
0 commit comments