1
1
import { assert , assertEquals , assertFalse } from 'https://deno.land/[email protected] /testing/asserts.ts'
2
- import { Header , Redirect , Rewrite , RoutesManifest } from './next-utils.ts'
2
+ import { HeaderRule , RedirectRule , RewriteRule , RoutesManifest } from './next-utils.ts'
3
3
import {
4
4
applyHeaderRule ,
5
- applyHeaders ,
5
+ applyHeaderRules ,
6
6
applyRedirectRule ,
7
- applyRedirects ,
7
+ applyRedirectRules ,
8
8
applyRewriteRule ,
9
- applyRewrites ,
9
+ applyRewriteRules ,
10
10
matchesRule ,
11
11
runPostMiddleware ,
12
12
} from './router.ts'
@@ -46,7 +46,7 @@ Deno.test('rewrites paths', () => {
46
46
} )
47
47
48
48
Deno . test ( 'rewrite matches headers "has" rule' , ( ) => {
49
- const rule : Rewrite = {
49
+ const rule : RewriteRule = {
50
50
source : '/has-rewrite-1' ,
51
51
regex : '/has-rewrite-1(?:/)?$' ,
52
52
has : [
@@ -71,7 +71,7 @@ Deno.test('rewrite matches headers "has" rule', () => {
71
71
} )
72
72
73
73
Deno . test ( 'matches named regex param' , ( ) => {
74
- const rule : Rewrite = {
74
+ const rule : RewriteRule = {
75
75
source : '/old-blog/:post(\\d{1,})' ,
76
76
destination : '/blog/:post' , // Matched parameters can be used in the destination
77
77
regex : '/old-blog/(?<post>\\d{1,})(?:/)?$' ,
@@ -83,7 +83,7 @@ Deno.test('matches named regex param', () => {
83
83
} )
84
84
85
85
Deno . test ( 'applies headers' , ( ) => {
86
- const rule : Header = {
86
+ const rule : HeaderRule = {
87
87
source : '/apply-header' ,
88
88
regex : '/apply-header(?:/)?$' ,
89
89
headers : [
@@ -96,13 +96,13 @@ Deno.test('applies headers', () => {
96
96
97
97
const request = new Request ( 'http://n/apply-header' )
98
98
99
- const result = applyHeaderRule ( { request, rule } )
99
+ const result = applyHeaderRule ( { request, rule, headers : new Map ( ) } )
100
100
assert ( result )
101
- assertEquals ( result . headers . get ( 'x-my-header' ) , 'my-value' )
101
+ assertEquals ( result . get ( 'x-my-header' ) , 'my-value' )
102
102
} )
103
103
104
104
Deno . test ( 'applies dynamic headers' , ( ) => {
105
- const rule : Header = {
105
+ const rule : HeaderRule = {
106
106
source : '/blog/:slug' ,
107
107
regex : '/blog/(?<slug>[^/]+?)(?:/)?$' ,
108
108
headers : [
@@ -119,14 +119,14 @@ Deno.test('applies dynamic headers', () => {
119
119
120
120
const request = new Request ( 'http://n/blog/hello-world' )
121
121
122
- const result = applyHeaderRule ( { request, rule } )
122
+ const result = applyHeaderRule ( { request, rule, headers : new Map ( ) } )
123
123
assert ( result )
124
- assertEquals ( result . headers . get ( 'x-slug' ) , 'hello-world' )
125
- assertEquals ( result . headers . get ( 'x-slug-hello-world' ) , 'my other custom header value' )
124
+ assertEquals ( result . get ( 'x-slug' ) , 'hello-world' )
125
+ assertEquals ( result . get ( 'x-slug-hello-world' ) , 'my other custom header value' )
126
126
} )
127
127
128
128
Deno . test ( 'applies wildcard headers' , ( ) => {
129
- const rule : Header = {
129
+ const rule : HeaderRule = {
130
130
source : '/blog/:slug*' ,
131
131
regex : '/blog(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))?(?:/)?$' ,
132
132
headers : [
@@ -143,14 +143,14 @@ Deno.test('applies wildcard headers', () => {
143
143
144
144
const request = new Request ( 'http://n/blog/a/b/c/d/hello-world' )
145
145
146
- const result = applyHeaderRule ( { request, rule } )
146
+ const result = applyHeaderRule ( { request, rule, headers : new Map ( ) } )
147
147
assert ( result )
148
- assertEquals ( result . headers . get ( 'x-slug' ) , 'a/b/c/d/hello-world' )
149
- assertEquals ( result . headers . get ( 'x-slug-abcdhello-world' ) , 'my other custom header value' )
148
+ assertEquals ( result . get ( 'x-slug' ) , 'a/b/c/d/hello-world' )
149
+ assertEquals ( result . get ( 'x-slug-abcdhello-world' ) , 'my other custom header value' )
150
150
} )
151
151
152
152
Deno . test ( 'applies regex headers' , ( ) => {
153
- const rule : Header = {
153
+ const rule : HeaderRule = {
154
154
source : '/blog/:post(\\d{1,})' ,
155
155
regex : '/blog(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))?(?:/)?$' ,
156
156
headers : [
@@ -163,13 +163,13 @@ Deno.test('applies regex headers', () => {
163
163
164
164
const request = new Request ( 'http://localhost/blog/123' )
165
165
166
- const result = applyHeaderRule ( { request, rule } )
166
+ const result = applyHeaderRule ( { request, rule, headers : new Map ( ) } )
167
167
assert ( result )
168
- assertEquals ( result . headers . get ( 'x-post' ) , '123' )
168
+ assertEquals ( result . get ( 'x-post' ) , '123' )
169
169
} )
170
170
171
171
Deno . test ( 'applies header based on value of a cookie' , ( ) => {
172
- const rule : Header = {
172
+ const rule : HeaderRule = {
173
173
source : '/specific/:path*' ,
174
174
regex : '/specific(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))?(?:/)?$' ,
175
175
has : [
@@ -201,13 +201,13 @@ Deno.test('applies header based on value of a cookie', () => {
201
201
} ) ,
202
202
} )
203
203
204
- const result = applyHeaderRule ( { request, rule } )
204
+ const result = applyHeaderRule ( { request, rule, headers : new Map ( ) } )
205
205
assert ( result )
206
- assertEquals ( result . headers . get ( 'x-authorized' ) , 'true' )
206
+ assertEquals ( result . get ( 'x-authorized' ) , 'true' )
207
207
} )
208
208
209
209
Deno . test ( 'applies "has" host rule' , ( ) => {
210
- const rule : Redirect = {
210
+ const rule : RedirectRule = {
211
211
source : '/has-redirect-6' ,
212
212
regex : '/has-redirect-6(?:/)?$' ,
213
213
has : [
@@ -229,122 +229,134 @@ Deno.test('applies "has" host rule', () => {
229
229
230
230
Deno . test ( 'headers' , async ( t ) => {
231
231
await t . step ( 'has headers rule' , ( ) => {
232
- const result = applyHeaders (
232
+ const result = applyHeaderRules (
233
233
new Request ( 'http://localhost/has-header-1' , {
234
234
headers : {
235
235
'x-my-header' : 'hello world!!' ,
236
236
} ,
237
237
} ) ,
238
- manifest . headers as Header [ ] ,
238
+ manifest . headers as HeaderRule [ ] ,
239
239
)
240
240
assert ( result )
241
- assertEquals ( result . headers . get ( 'x-another' ) , 'header' )
241
+ assertEquals ( new Headers ( result ) . get ( 'x-another' ) , 'header' )
242
242
243
- const result2 = applyHeaders ( new Request ( 'http://localhost/has-header-1' ) , manifest . headers as Header [ ] )
243
+ const result2 = applyHeaderRules ( new Request ( 'http://localhost/has-header-1' ) , manifest . headers as HeaderRule [ ] )
244
244
assert ( result2 )
245
- assertFalse ( result2 . headers . get ( 'x-another' ) )
245
+ assertFalse ( new Headers ( result2 ) . get ( 'x-another' ) )
246
246
} )
247
247
248
248
await t . step ( 'has query rule' , ( ) => {
249
- const result = applyHeaders (
249
+ const result = applyHeaderRules (
250
250
new Request ( 'http://localhost/has-header-2?my-query=hellooo' ) ,
251
- manifest . headers as Header [ ] ,
251
+ manifest . headers as HeaderRule [ ] ,
252
252
)
253
253
assert ( result )
254
- assertEquals ( result . headers . get ( 'x-added' ) , 'value' )
254
+ assertEquals ( new Headers ( result ) . get ( 'x-added' ) , 'value' )
255
255
256
- const result2 = applyHeaders ( new Request ( 'http://localhost/has-header-2' ) , manifest . headers as Header [ ] )
256
+ const result2 = applyHeaderRules ( new Request ( 'http://localhost/has-header-2' ) , manifest . headers as HeaderRule [ ] )
257
257
assert ( result2 )
258
- assertFalse ( result2 . headers . get ( 'x-added' ) )
258
+ assertFalse ( new Headers ( result2 ) . get ( 'x-added' ) )
259
259
} )
260
260
261
261
await t . step ( 'has cookie rule' , ( ) => {
262
- const result = applyHeaders (
262
+ const result = applyHeaderRules (
263
263
new Request ( 'http://localhost/has-header-3' , {
264
264
headers : {
265
265
cookie : 'loggedIn=true' ,
266
266
} ,
267
267
} ) ,
268
- manifest . headers as Header [ ] ,
268
+ manifest . headers as HeaderRule [ ] ,
269
269
)
270
270
assert ( result )
271
- assertEquals ( result . headers . get ( 'x-is-user' ) , 'yuuuup' )
271
+ assertEquals ( new Headers ( result ) . get ( 'x-is-user' ) , 'yuuuup' )
272
272
273
- const result2 = applyHeaders ( new Request ( 'http://localhost/has-header-3' ) , manifest . headers as Header [ ] )
273
+ const result2 = applyHeaderRules ( new Request ( 'http://localhost/has-header-3' ) , manifest . headers as HeaderRule [ ] )
274
274
assert ( result2 )
275
- assertFalse ( result2 . headers . get ( 'x-is-user' ) )
275
+ assertFalse ( new Headers ( result2 ) . get ( 'x-is-user' ) )
276
276
} )
277
277
278
278
await t . step ( 'has host rule' , ( ) => {
279
- const result = applyHeaders ( new Request ( 'http://example.com/has-header-4' ) , manifest . headers as Header [ ] )
279
+ const result = applyHeaderRules ( new Request ( 'http://example.com/has-header-4' ) , manifest . headers as HeaderRule [ ] )
280
280
assert ( result )
281
- assertEquals ( result . headers . get ( 'x-is-host' ) , 'yuuuup' )
281
+ assertEquals ( new Headers ( result ) . get ( 'x-is-host' ) , 'yuuuup' )
282
282
283
- const result2 = applyHeaders ( new Request ( 'http://localhost/has-header-4' ) , manifest . headers as Header [ ] )
283
+ const result2 = applyHeaderRules ( new Request ( 'http://localhost/has-header-4' ) , manifest . headers as HeaderRule [ ] )
284
284
assert ( result2 )
285
- assertFalse ( result2 . headers . get ( 'x-is-host' ) )
285
+ assertFalse ( new Headers ( result2 ) . get ( 'x-is-host' ) )
286
286
} )
287
287
} )
288
288
Deno . test ( 'redirects' , async ( t ) => {
289
289
await t . step ( 'chained redirects' , ( ) => {
290
- const result = applyRedirects ( new Request ( 'http://localhost/redir-chain1' ) , manifest . redirects as Redirect [ ] )
290
+ const result = applyRedirectRules (
291
+ new Request ( 'http://localhost/redir-chain1' ) ,
292
+ manifest . redirects as RedirectRule [ ] ,
293
+ )
291
294
assert ( result )
292
295
assertEquals ( result . headers . get ( 'location' ) , 'http://localhost/redir-chain2' )
293
296
assertEquals ( result . status , 301 )
294
297
295
- const result2 = applyRedirects ( new Request ( 'http://localhost/redir-chain2' ) , manifest . redirects as Redirect [ ] )
298
+ const result2 = applyRedirectRules (
299
+ new Request ( 'http://localhost/redir-chain2' ) ,
300
+ manifest . redirects as RedirectRule [ ] ,
301
+ )
296
302
assert ( result2 )
297
303
assertEquals ( result2 . headers . get ( 'location' ) , 'http://localhost/redir-chain3' )
298
304
assertEquals ( result2 . status , 302 )
299
305
300
- const result3 = applyRedirects ( new Request ( 'http://localhost/redir-chain3' ) , manifest . redirects as Redirect [ ] )
306
+ const result3 = applyRedirectRules (
307
+ new Request ( 'http://localhost/redir-chain3' ) ,
308
+ manifest . redirects as RedirectRule [ ] ,
309
+ )
301
310
assert ( result3 )
302
311
assertEquals ( result3 . headers . get ( 'location' ) , 'http://localhost/' )
303
312
assertEquals ( result3 . status , 303 )
304
313
} )
305
314
306
315
await t . step ( 'does not match _next' , ( ) => {
307
- const result = applyRedirects (
316
+ const result = applyRedirectRules (
308
317
new Request ( 'http://localhost/_next/has-redirect-5' , {
309
318
headers : {
310
319
'x-test-next' : 'true' ,
311
320
} ,
312
321
} ) ,
313
- manifest . redirects as Redirect [ ] ,
322
+ manifest . redirects as RedirectRule [ ] ,
314
323
)
315
324
assertFalse ( result )
316
325
317
- const result2 = applyRedirects (
326
+ const result2 = applyRedirectRules (
318
327
new Request ( 'http://localhost/another/has-redirect-5' , {
319
328
headers : {
320
329
'x-test-next' : 'true' ,
321
330
} ,
322
331
} ) ,
323
- manifest . redirects as Redirect [ ] ,
332
+ manifest . redirects as RedirectRule [ ] ,
324
333
)
325
334
326
335
assert ( result2 )
327
336
assertEquals ( result2 . status , 307 )
328
337
} )
329
338
330
339
await t . step ( 'with permanent: false' , ( ) => {
331
- const result = applyRedirects ( new Request ( 'http://localhost/redirect1' ) , manifest . redirects as Redirect [ ] )
340
+ const result = applyRedirectRules ( new Request ( 'http://localhost/redirect1' ) , manifest . redirects as RedirectRule [ ] )
332
341
assert ( result )
333
342
assertEquals ( result . headers . get ( 'location' ) , 'http://localhost/' )
334
343
assertEquals ( result . status , 307 )
335
344
} )
336
345
337
346
await t . step ( 'with params' , ( ) => {
338
- const result = applyRedirects ( new Request ( 'http://localhost/hello/123/another' ) , manifest . redirects as Redirect [ ] )
347
+ const result = applyRedirectRules (
348
+ new Request ( 'http://localhost/hello/123/another' ) ,
349
+ manifest . redirects as RedirectRule [ ] ,
350
+ )
339
351
assert ( result )
340
352
assertEquals ( result . headers . get ( 'location' ) , 'http://localhost/blog/123' )
341
353
assertEquals ( result . status , 307 )
342
354
} )
343
355
344
356
await t . step ( 'to a URL with a hash' , ( ) => {
345
- const result = applyRedirects (
357
+ const result = applyRedirectRules (
346
358
new Request ( 'http://localhost/docs/router-status/500' ) ,
347
- manifest . redirects as Redirect [ ] ,
359
+ manifest . redirects as RedirectRule [ ] ,
348
360
)
349
361
assert ( result )
350
362
const location = result . headers . get ( 'location' )
@@ -356,7 +368,7 @@ Deno.test('redirects', async (t) => {
356
368
} )
357
369
358
370
await t . step ( 'with provided statusCode' , ( ) => {
359
- const result = applyRedirects ( new Request ( 'http://localhost/redirect2' ) , manifest . redirects as Redirect [ ] )
371
+ const result = applyRedirectRules ( new Request ( 'http://localhost/redirect2' ) , manifest . redirects as RedirectRule [ ] )
360
372
assert ( result )
361
373
const location = result . headers . get ( 'location' )
362
374
assert ( location )
@@ -367,9 +379,9 @@ Deno.test('redirects', async (t) => {
367
379
} )
368
380
369
381
await t . step ( 'using a catchall' , ( ) => {
370
- const result = applyRedirects (
382
+ const result = applyRedirectRules (
371
383
new Request ( 'http://localhost/catchall-redirect/hello/world' ) ,
372
- manifest . redirects as Redirect [ ] ,
384
+ manifest . redirects as RedirectRule [ ] ,
373
385
)
374
386
assert ( result )
375
387
const location = result . headers . get ( 'location' )
@@ -383,19 +395,19 @@ Deno.test('redirects', async (t) => {
383
395
384
396
Deno . test ( 'rewrites' , async ( t ) => {
385
397
await t . step ( 'afterFiles rewrite' , ( ) => {
386
- const result = applyRewrites ( {
398
+ const result = applyRewriteRules ( {
387
399
request : new Request ( 'http://localhost/to-another' ) ,
388
- rules : manifest . rewrites . afterFiles as Rewrite [ ] ,
400
+ rules : manifest . rewrites . afterFiles as RewriteRule [ ] ,
389
401
checkStaticRoutes : true ,
390
402
staticRoutes,
391
403
} )
392
404
assert ( result )
393
405
assertEquals ( result . url , 'http://localhost/another/one' )
394
406
} )
395
407
await t . step ( 'afterFiles with params' , ( ) => {
396
- const result = applyRewrites ( {
408
+ const result = applyRewriteRules ( {
397
409
request : new Request ( 'http://localhost/test/hello' ) ,
398
- rules : manifest . rewrites . afterFiles as Rewrite [ ] ,
410
+ rules : manifest . rewrites . afterFiles as RewriteRule [ ] ,
399
411
checkStaticRoutes : true ,
400
412
staticRoutes,
401
413
} )
@@ -404,9 +416,9 @@ Deno.test('rewrites', async (t) => {
404
416
} )
405
417
406
418
await t . step ( 'afterFiles matching static file' , ( ) => {
407
- const result = applyRewrites ( {
419
+ const result = applyRewriteRules ( {
408
420
request : new Request ( 'http://localhost/hello-world' ) ,
409
- rules : manifest . rewrites . afterFiles as Rewrite [ ] ,
421
+ rules : manifest . rewrites . afterFiles as RewriteRule [ ] ,
410
422
checkStaticRoutes : true ,
411
423
staticRoutes,
412
424
} )
@@ -416,9 +428,9 @@ Deno.test('rewrites', async (t) => {
416
428
} )
417
429
418
430
await t . step ( 'afterFiles matching dynamic route' , ( ) => {
419
- const result = applyRewrites ( {
431
+ const result = applyRewriteRules ( {
420
432
request : new Request ( 'http://localhost/test/nothing' ) ,
421
- rules : manifest . rewrites . afterFiles as Rewrite [ ] ,
433
+ rules : manifest . rewrites . afterFiles as RewriteRule [ ] ,
422
434
checkStaticRoutes : true ,
423
435
staticRoutes,
424
436
} )
@@ -428,19 +440,19 @@ Deno.test('rewrites', async (t) => {
428
440
} )
429
441
430
442
await t . step ( 'non-matching' , ( ) => {
431
- const result = applyRewrites ( {
443
+ const result = applyRewriteRules ( {
432
444
request : new Request ( 'http://localhost/no-match' ) ,
433
- rules : manifest . rewrites . afterFiles as Rewrite [ ] ,
445
+ rules : manifest . rewrites . afterFiles as RewriteRule [ ] ,
434
446
checkStaticRoutes : true ,
435
447
staticRoutes,
436
448
} )
437
449
assertFalse ( result )
438
450
} )
439
451
440
452
await t . step ( 'preserves query params' , ( ) => {
441
- const result = applyRewrites ( {
453
+ const result = applyRewriteRules ( {
442
454
request : new Request ( 'http://localhost/proxy-me/first?keep=me&and=me' ) ,
443
- rules : manifest . rewrites . afterFiles as Rewrite [ ] ,
455
+ rules : manifest . rewrites . afterFiles as RewriteRule [ ] ,
444
456
checkStaticRoutes : true ,
445
457
staticRoutes,
446
458
} )
0 commit comments