@@ -588,6 +588,182 @@ describe('RouterExplorer', () => {
588588 } ) ;
589589 } ) ;
590590
591+ describe ( 'when the versioning type is CUSTOM' , ( ) => {
592+ const extractor = ( request : { headers : { accept ?: string } } ) => {
593+ const match = request . headers . accept ?. match ( / v ( \d + \. ? \d * ) \+ j s o n $ / ) ;
594+ if ( match ) {
595+ return match [ 1 ] ;
596+ }
597+ return null ;
598+ } ;
599+
600+ it ( 'should return next if there is no pertinent request object' , ( ) => {
601+ const version = '1' ;
602+ const versioningOptions : VersioningOptions = {
603+ type : VersioningType . CUSTOM ,
604+ extractor,
605+ } ;
606+ const handler = sinon . stub ( ) ;
607+
608+ const routePathMetadata : RoutePathMetadata = {
609+ methodVersion : version ,
610+ versioningOptions,
611+ } ;
612+ const versionFilter = ( routerBuilder as any ) . applyVersionFilter (
613+ null ,
614+ routePathMetadata ,
615+ handler ,
616+ ) ;
617+
618+ const req = { headers : { } } ;
619+ const res = { } ;
620+ const next = sinon . stub ( ) ;
621+
622+ versionFilter ( req , res , next ) ;
623+
624+ expect ( next . called ) . to . be . true ;
625+ } ) ;
626+
627+ it ( 'should return next if there is no version in the request object value' , ( ) => {
628+ const version = '1' ;
629+ const versioningOptions : VersioningOptions = {
630+ type : VersioningType . CUSTOM ,
631+ extractor,
632+ } ;
633+ const handler = sinon . stub ( ) ;
634+
635+ const routePathMetadata : RoutePathMetadata = {
636+ methodVersion : version ,
637+ versioningOptions,
638+ } ;
639+ const versionFilter = ( routerBuilder as any ) . applyVersionFilter (
640+ null ,
641+ routePathMetadata ,
642+ handler ,
643+ ) ;
644+
645+ const req = { headers : { accept : 'application/json;' } } ;
646+ const res = { } ;
647+ const next = sinon . stub ( ) ;
648+
649+ versionFilter ( req , res , next ) ;
650+
651+ expect ( next . called ) . to . be . true ;
652+ } ) ;
653+
654+ describe ( 'when the handler version is an array' , ( ) => {
655+ it ( 'should return next if the version in the request object value does not match the handler version' , ( ) => {
656+ const version = [ '1' , '2' ] ;
657+ const versioningOptions : VersioningOptions = {
658+ type : VersioningType . CUSTOM ,
659+ extractor,
660+ } ;
661+ const handler = sinon . stub ( ) ;
662+
663+ const routePathMetadata : RoutePathMetadata = {
664+ methodVersion : version ,
665+ versioningOptions,
666+ } ;
667+ const versionFilter = ( routerBuilder as any ) . applyVersionFilter (
668+ null ,
669+ routePathMetadata ,
670+ handler ,
671+ ) ;
672+
673+ const req = { headers : { accept : 'application/foo.v3+json' } } ;
674+ const res = { } ;
675+ const next = sinon . stub ( ) ;
676+
677+ versionFilter ( req , res , next ) ;
678+
679+ expect ( next . called ) . to . be . true ;
680+ } ) ;
681+
682+ it ( 'should return the handler if the version in the request object value matches the handler version' , ( ) => {
683+ const version = [ '1' , '2' ] ;
684+ const versioningOptions : VersioningOptions = {
685+ type : VersioningType . CUSTOM ,
686+ extractor,
687+ } ;
688+ const handler = sinon . stub ( ) ;
689+
690+ const routePathMetadata : RoutePathMetadata = {
691+ methodVersion : version ,
692+ versioningOptions,
693+ } ;
694+ const versionFilter = ( routerBuilder as any ) . applyVersionFilter (
695+ null ,
696+ routePathMetadata ,
697+ handler ,
698+ ) ;
699+
700+ const req = { headers : { accept : 'application/foo.v2+json' } } ;
701+ const res = { } ;
702+ const next = sinon . stub ( ) ;
703+
704+ versionFilter ( req , res , next ) ;
705+
706+ expect ( handler . calledWith ( req , res , next ) ) . to . be . true ;
707+ } ) ;
708+ } ) ;
709+
710+ describe ( 'when the handler version is a string' , ( ) => {
711+ it ( 'should return next if the version in the request object value does not match the handler version' , ( ) => {
712+ const version = '1' ;
713+ const versioningOptions : VersioningOptions = {
714+ type : VersioningType . CUSTOM ,
715+ extractor,
716+ } ;
717+ const handler = sinon . stub ( ) ;
718+
719+ const routePathMetadata : RoutePathMetadata = {
720+ methodVersion : version ,
721+ versioningOptions,
722+ } ;
723+ const versionFilter = ( routerBuilder as any ) . applyVersionFilter (
724+ null ,
725+ routePathMetadata ,
726+ handler ,
727+ ) ;
728+
729+ const req = { headers : { accept : 'application/foo.v2+json' } } ;
730+ const res = { } ;
731+ const next = sinon . stub ( ) ;
732+
733+ versionFilter ( req , res , next ) ;
734+
735+ expect ( next . called ) . to . be . true ;
736+ } ) ;
737+
738+ it ( 'should return the handler if the version in the request object value matches the handler version' , ( ) => {
739+ const version = '1' ;
740+ const versioningOptions : VersioningOptions = {
741+ type : VersioningType . CUSTOM ,
742+ extractor,
743+ } ;
744+ const handler = sinon . stub ( ) ;
745+
746+ const routePathMetadata : RoutePathMetadata = {
747+ methodVersion : version ,
748+ versioningOptions,
749+ } ;
750+ const versionFilter = ( routerBuilder as any ) . applyVersionFilter (
751+ null ,
752+ routePathMetadata ,
753+ handler ,
754+ ) ;
755+
756+ const req = { headers : { accept : 'application/foo.v1+json' } } ;
757+ const res = { } ;
758+ const next = sinon . stub ( ) ;
759+
760+ versionFilter ( req , res , next ) ;
761+
762+ expect ( handler . calledWith ( req , res , next ) ) . to . be . true ;
763+ } ) ;
764+ } ) ;
765+ } ) ;
766+
591767 describe ( 'when the versioning type is HEADER' , ( ) => {
592768 it ( 'should return next if there is no Custom Header' , ( ) => {
593769 const version = '1' ;
0 commit comments