@@ -439,6 +439,53 @@ test('encoded backslashes do not get decoded', t => {
439439 t . is ( normalizeUrl ( 'https://foo.com/something\\else/great' ) , 'https://foo.com/something/else/great' ) ;
440440} ) ;
441441
442+ test ( 'removePath option' , t => {
443+ // Boolean: Remove entire path
444+ t . is ( normalizeUrl ( 'https://example.com/path/to/page' , { removePath : true } ) , 'https://example.com' ) ;
445+ t . is ( normalizeUrl ( 'https://example.com/path/to/page?query=1' , { removePath : true } ) , 'https://example.com/?query=1' ) ;
446+ t . is ( normalizeUrl ( 'https://example.com/path/to/page#hash' , { removePath : true } ) , 'https://example.com/#hash' ) ;
447+ t . is ( normalizeUrl ( 'https://example.com/' , { removePath : true } ) , 'https://example.com' ) ;
448+ t . is ( normalizeUrl ( 'https://example.com' , { removePath : true } ) , 'https://example.com' ) ;
449+
450+ // With other options
451+ t . is ( normalizeUrl ( 'https://example.com/path/' , { removePath : true , removeTrailingSlash : true } ) , 'https://example.com' ) ;
452+ t . is ( normalizeUrl ( 'https://www.example.com/path' , { removePath : true , stripWWW : true } ) , 'https://example.com' ) ;
453+ } ) ;
454+
455+ test ( 'transformPath option' , t => {
456+ // Function: Keep only first path component
457+ const keepFirst = pathComponents => pathComponents . slice ( 0 , 1 ) ;
458+ t . is ( normalizeUrl ( 'https://example.com/api/v1/users' , { transformPath : keepFirst } ) , 'https://example.com/api' ) ;
459+ t . is ( normalizeUrl ( 'https://example.com/path/to/page' , { transformPath : keepFirst } ) , 'https://example.com/path' ) ;
460+ t . is ( normalizeUrl ( 'https://example.com/' , { transformPath : keepFirst } ) , 'https://example.com' ) ;
461+
462+ // Function: Remove specific component
463+ const removeAdmin = pathComponents => pathComponents . filter ( c => c !== 'admin' ) ;
464+ t . is ( normalizeUrl ( 'https://example.com/admin/users' , { transformPath : removeAdmin } ) , 'https://example.com/users' ) ;
465+ t . is ( normalizeUrl ( 'https://example.com/path/admin/page' , { transformPath : removeAdmin } ) , 'https://example.com/path/page' ) ;
466+
467+ // Function: Custom logic
468+ const customLogic = pathComponents => {
469+ if ( pathComponents [ 0 ] === 'api' ) {
470+ return pathComponents . slice ( 0 , 1 ) ; // Keep /api only
471+ }
472+ return [ ] ; // Remove everything else
473+ } ;
474+ t . is ( normalizeUrl ( 'https://example.com/api/v1/users' , { transformPath : customLogic } ) , 'https://example.com/api' ) ;
475+ t . is ( normalizeUrl ( 'https://example.com/other/path' , { transformPath : customLogic } ) , 'https://example.com' ) ;
476+
477+ // Edge cases
478+ t . is ( normalizeUrl ( 'https://example.com/path' , { transformPath : ( ) => [ ] } ) , 'https://example.com' ) ;
479+ t . is ( normalizeUrl ( 'https://example.com/path' , { transformPath : ( ) => null } ) , 'https://example.com' ) ;
480+ t . is ( normalizeUrl ( 'https://example.com/path' , { transformPath : ( ) => undefined } ) , 'https://example.com' ) ;
481+
482+ // Combining with removePath (removePath should take precedence)
483+ t . is ( normalizeUrl ( 'https://example.com/path/to/page' , {
484+ removePath : true ,
485+ transformPath : pathComponents => pathComponents . slice ( 0 , 2 )
486+ } ) , 'https://example.com' ) ;
487+ } ) ;
488+
442489test ( 'path-like query strings without equals signs are preserved' , t => {
443490 // Issue #193 - Path-like query strings should not get '=' appended
444491 t . is ( normalizeUrl ( 'https://example.com/index.php?/Some/Route/To/Path/12345' ) , 'https://example.com/index.php?/Some/Route/To/Path/12345' ) ;
0 commit comments