@@ -37,6 +37,16 @@ describe('matcher', () => {
3737 expect ( replaceTemplateParams ( template , params ) ) . toBe ( path ) ;
3838 } ) ;
3939
40+ it ( 'should replace template params with valid URI component' , ( ) => {
41+ expect . assertions ( 1 ) ;
42+
43+ const template = '/base/path/:id/:lastName/end' ;
44+ const params = { id : 12 , lastName : "Hello World!@#$%^&*()_+[]{}|;:',.<>?/`~ =" } ;
45+ const path = `/base/path/12/${ encodeURIComponent ( params . lastName ) } /end` ;
46+
47+ expect ( replaceTemplateParams ( template , params ) ) . toBe ( path ) ;
48+ } ) ;
49+
4050 it ( 'should strip the type from the template path then replace path params with the provided params' , ( ) => {
4151 expect . assertions ( 1 ) ;
4252
@@ -684,7 +694,16 @@ describe('matcher', () => {
684694 expect . assertions ( 1 ) ;
685695 const matcher = new Matcher ( template ) ;
686696 const path = '/base/path/something/12/path/john/doe/end' ;
687- const params = { id : '12' , name : 'john' , lastName : 'doe' } ;
697+ const params = { id : 12 , name : 'john' , lastName : 'doe' } ;
698+ const wildcards = { 1 : 'something' } ;
699+ expect ( matcher . extract ( path ) ) . toStrictEqual ( { params, wildcards } ) ;
700+ } ) ;
701+
702+ it ( 'should extract params from a location with uri component params' , ( ) => {
703+ expect . assertions ( 1 ) ;
704+ const matcher = new Matcher ( template ) ;
705+ const params = { id : 12 , name : 'john' , lastName : "Hello World!@#$%^&*()_+[]{}|;:',.<>?/`~ =" } ;
706+ const path = `/base/path/something/12/path/john/${ encodeURIComponent ( params . lastName ) } /end` ;
688707 const wildcards = { 1 : 'something' } ;
689708 expect ( matcher . extract ( path ) ) . toStrictEqual ( { params, wildcards } ) ;
690709 } ) ;
@@ -693,7 +712,7 @@ describe('matcher', () => {
693712 expect . assertions ( 1 ) ;
694713 const matcher = new Matcher ( '/base/path/:{number}:id/end' ) ;
695714 const path = '/base/path/12/end' ;
696- const params = { id : '12' } ;
715+ const params = { id : 12 } ;
697716 const wildcards = { } ;
698717 expect ( matcher . extract ( path ) ) . toStrictEqual ( { params, wildcards } ) ;
699718 } ) ;
@@ -729,7 +748,7 @@ describe('matcher', () => {
729748 expect . assertions ( 1 ) ;
730749 const matcher = new Matcher ( '/base/path/:{number}:id/:{string}:name/end' ) ;
731750 const path = '/base/path/12/john/end' ;
732- const params = { id : '12' , name : 'john' } ;
751+ const params = { id : 12 , name : 'john' } ;
733752 const wildcards = { } ;
734753 expect ( matcher . extract ( path ) ) . toStrictEqual ( { params, wildcards } ) ;
735754 } ) ;
@@ -738,7 +757,7 @@ describe('matcher', () => {
738757 expect . assertions ( 1 ) ;
739758 const matcher = new Matcher ( '/base/path/:id/:first:?/:name/end' ) ;
740759 const path = '/base/path/12/john/doe/end' ;
741- const params = { id : '12' , first : 'john' , name : 'doe' } ;
760+ const params = { id : 12 , first : 'john' , name : 'doe' } ;
742761 const wildcards = { } ;
743762 expect ( matcher . extract ( path ) ) . toStrictEqual ( { params, wildcards } ) ;
744763 } ) ;
@@ -747,26 +766,26 @@ describe('matcher', () => {
747766 expect . assertions ( 1 ) ;
748767 const matcher = new Matcher ( '/base/path/:id/:first:?/:name/end' ) ;
749768 const path = '/base/path/12/doe/end' ;
750- const params = { id : '12' , first : undefined , name : 'doe' } ;
769+ const params = { id : 12 , first : undefined , name : 'doe' } ;
751770 const wildcards = { } ;
752771 expect ( matcher . extract ( path ) ) . toStrictEqual ( { params, wildcards } ) ;
753772 } ) ;
754773
755774 it ( 'should extract params from a location with wildcards' , ( ) => {
756775 expect . assertions ( 1 ) ;
757- const matcher = new Matcher ( '/base/path/*/segment/*/end' ) ;
758- const path = '/base/path/something/segment/else/end' ;
759- const params = { } ;
760- const wildcards = { 1 : 'something' , 2 : 'else' } ;
776+ const matcher = new Matcher ( '/base/:boolean/ path/*/segment/*/end' ) ;
777+ const path = '/base/FALSE/ path/something/segment/else/end' ;
778+ const params = { boolean : false } ;
779+ const wildcards = { 2 : 'something' , 3 : 'else' } ;
761780 expect ( matcher . extract ( path ) ) . toStrictEqual ( { params, wildcards } ) ;
762781 } ) ;
763782
764783 it ( 'should extract params from a location with params and wildcards' , ( ) => {
765784 expect . assertions ( 1 ) ;
766- const matcher = new Matcher ( '/base/path/*/segment/:id/*' ) ;
767- const path = '/base/path/something/segment/12/segment/end' ;
768- const params = { id : '12' } ;
769- const wildcards = { 1 : 'something' , 3 : 'segment/end' } ;
785+ const matcher = new Matcher ( '/base/:boolean/ path/*/segment/:id/*' ) ;
786+ const path = '/base/true/ path/something/segment/12/segment/end' ;
787+ const params = { id : 12 , boolean : true } ;
788+ const wildcards = { 2 : 'something' , 4 : 'segment/end' } ;
770789 expect ( matcher . extract ( path ) ) . toStrictEqual ( { params, wildcards } ) ;
771790 } ) ;
772791
0 commit comments