1+ import { join } from 'node:path' ;
12import { requestHandler } from '../index.mts' ;
2- import { makeTestTempDir , makeTestTempFile } from '../test-helpers/makeFileStructure.mts' ;
3+ import { makeTestTempDir } from '../test-helpers/makeFileStructure.mts' ;
34import { responds } from '../test-helpers/responds.mts' ;
45import { withServer } from '../test-helpers/withServer.mts' ;
56import type { ConfigMount } from './config/types.mts' ;
@@ -308,10 +309,8 @@ describe('buildRouter', () => {
308309 } ) ;
309310 } ) ;
310311
311- const TEST_MAPPING_FILE = makeTestTempFile (
312- 'map-' ,
313- 'redirects.map' ,
314- `
312+ const NGINX_MAPS = makeTestTempDir ( 'map-' , {
313+ 'syntax.map' : `
315314/foo /new-foo;
316315# comment
317316/bar\t/new-bar #comment
@@ -324,7 +323,15 @@ describe('buildRouter', () => {
324323/s1 /escaped\\ space;
325324/s2 "/quoted space";
326325` ,
327- ) ;
326+ 'with-default.map' : `
327+ /foo /one;
328+ default /other;
329+ ` ,
330+ 'regex.map' : `
331+ ~*^/one/(.+)/end.(?<ext>.+)$ /new/$1.$ext;
332+ ~^/case/(.+)/end.(?<ext>.+)$ /new/$1.$ext;
333+ ` ,
334+ } ) ;
328335
329336 it (
330337 'loads mappings from an nginx-formatted mapping file' ,
@@ -333,7 +340,7 @@ describe('buildRouter', () => {
333340 const router = await buildRouter ( [
334341 {
335342 type : 'redirect-map' ,
336- mapping : getTyped ( TEST_MAPPING_FILE ) ,
343+ mapping : join ( getTyped ( NGINX_MAPS ) , 'syntax.map' ) ,
337344 status : 307 ,
338345 options : { caseSensitive : false } ,
339346 } ,
@@ -368,6 +375,69 @@ describe('buildRouter', () => {
368375 } ) ;
369376 } ,
370377 ) ;
378+
379+ it (
380+ 'supports "default" in nginx-formatted mapping files' ,
381+ { timeout : 3000 } ,
382+ async ( { getTyped } ) => {
383+ const router = await buildRouter ( [
384+ {
385+ type : 'redirect-map' ,
386+ mapping : join ( getTyped ( NGINX_MAPS ) , 'with-default.map' ) ,
387+ status : 307 ,
388+ options : { caseSensitive : false } ,
389+ } ,
390+ ] ) ;
391+ return withServer ( router , async ( url ) => {
392+ await expect (
393+ fetch ( url + '/foo' , { redirect : 'manual' } ) ,
394+ responds ( { status : 307 , headers : { location : '/one' } , body : '' } ) ,
395+ ) ;
396+ await expect (
397+ fetch ( url + '/nope' , { redirect : 'manual' } ) ,
398+ responds ( { status : 307 , headers : { location : '/other' } , body : '' } ) ,
399+ ) ;
400+ } ) ;
401+ } ,
402+ ) ;
403+
404+ it (
405+ 'supports regular expressions in nginx-formatted mapping files' ,
406+ { timeout : 3000 } ,
407+ async ( { getTyped } ) => {
408+ const router = await buildRouter ( [
409+ {
410+ type : 'redirect-map' ,
411+ mapping : join ( getTyped ( NGINX_MAPS ) , 'regex.map' ) ,
412+ status : 307 ,
413+ options : { caseSensitive : false } ,
414+ } ,
415+ FALLBACK_200 ,
416+ ] ) ;
417+ return withServer ( router , async ( url ) => {
418+ await expect (
419+ fetch ( url + '/one/a/end.xyz' , { redirect : 'manual' } ) ,
420+ responds ( { status : 307 , headers : { location : '/new/a.xyz' } , body : '' } ) ,
421+ ) ;
422+ await expect (
423+ fetch ( url + '/one/a/END.xyz' , { redirect : 'manual' } ) ,
424+ responds ( { status : 307 , headers : { location : '/new/a.xyz' } , body : '' } ) ,
425+ ) ;
426+ await expect (
427+ fetch ( url + '/nope/one/a/end.xyz' , { redirect : 'manual' } ) ,
428+ responds ( { status : 200 } ) ,
429+ ) ;
430+ await expect (
431+ fetch ( url + '/case/b/end.w' , { redirect : 'manual' } ) ,
432+ responds ( { status : 307 , headers : { location : '/new/b.w' } , body : '' } ) ,
433+ ) ;
434+ await expect (
435+ fetch ( url + '/case/b/END.w' , { redirect : 'manual' } ) ,
436+ responds ( { status : 200 } ) ,
437+ ) ;
438+ } ) ;
439+ } ,
440+ ) ;
371441 } ) ;
372442
373443 it ( 'logs requests' , { timeout : 3000 } , async ( ) => {
0 commit comments