@@ -3,7 +3,7 @@ import { apiGatewayRequest, handlerContext, albRequest, albMultiValHeadersReques
33import { spy , SinonSpy , assert } from 'sinon' ;
44import { Application , Request , Response , Router } from '../src' ;
55import { RequestEvent } from '../src/request-response-types' ;
6- import { NextCallback } from '../src/interfaces' ;
6+ import { NextCallback , IRoute , IRouter } from '../src/interfaces' ;
77import { expect } from 'chai' ;
88import { StringArrayOfStringsMap , StringMap , KeyValueStringObject } from '../src/utils/common-types' ;
99
@@ -592,4 +592,101 @@ describe('integration tests', () => {
592592
593593 } ) ;
594594
595+ describe ( 'building routes with router.route' , ( ) => {
596+
597+ it ( 'is chainable' , ( ) => {
598+ let handler = ( _req : Request , resp : Response ) : void => { resp . send ( 'Test' ) ; } ,
599+ getSpy = spy ( handler ) ,
600+ postSpy = spy ( handler ) ,
601+ putSpy = spy ( handler ) ;
602+
603+ app . route ( '/test' )
604+ . get ( getSpy )
605+ . post ( postSpy )
606+ . put ( putSpy ) ;
607+
608+ // Ensure that chained handlers were registered properly
609+
610+ testOutcome ( 'GET' , '/test' , 'Test' ) ;
611+ assert . calledOnce ( getSpy ) ;
612+ assert . notCalled ( postSpy ) ;
613+ assert . notCalled ( putSpy ) ;
614+ getSpy . resetHistory ( ) ;
615+
616+ testOutcome ( 'POST' , '/test' , 'Test' ) ;
617+ assert . calledOnce ( postSpy ) ;
618+ assert . notCalled ( getSpy ) ;
619+ assert . notCalled ( putSpy ) ;
620+ postSpy . resetHistory ( ) ;
621+
622+ testOutcome ( 'PUT' , '/test' , 'Test' ) ;
623+ assert . calledOnce ( putSpy ) ;
624+ assert . notCalled ( getSpy ) ;
625+ assert . notCalled ( postSpy ) ;
626+ putSpy . resetHistory ( ) ;
627+ } ) ;
628+
629+ it ( 'registers route handlers properly' , ( ) => {
630+ let methods : ( keyof IRoute & keyof IRouter ) [ ] ,
631+ allHandler : SinonSpy ,
632+ route : IRoute ;
633+
634+ route = app . route ( '/test' ) ;
635+
636+ // methods to test
637+ methods = [ 'get' , 'post' , 'put' , 'delete' , 'head' , 'options' , 'patch' ] ;
638+
639+ // Register handler that runs for every request
640+ allHandler = spy ( ( _req : Request , _resp : Response , next : NextCallback ) => { next ( ) ; } ) ;
641+ route . all ( allHandler ) ;
642+
643+
644+ // Register a handler for each method
645+ const handlers = _ . reduce ( methods , ( memo , method ) => {
646+ let handler = spy ( ( _req : Request , resp : Response ) => { resp . send ( `Test ${ method } ` ) ; } ) ;
647+
648+ // Save the handler spy for testing later
649+ memo [ method ] = handler ;
650+
651+ // add the handler to our route
652+ route [ method ] ( handler ) ;
653+
654+ return memo ;
655+ } , { } as { [ k : string ] : SinonSpy } ) ;
656+
657+ app . use ( ( _req : Request , resp : Response ) => {
658+ resp . send ( 'not found' ) ;
659+ } ) ;
660+
661+ // Run once for each method type
662+ // Both a path with and without a trailing slash should match
663+ _ . each ( [ '/test' , '/test/' ] , ( path ) => {
664+ _ . each ( methods , ( method ) => {
665+ testOutcome ( method . toUpperCase ( ) , path , `Test ${ method } ` ) ;
666+
667+ // Check that the "all" handler was called
668+ assert . calledOnce ( allHandler ) ;
669+ allHandler . resetHistory ( ) ;
670+
671+ // Check that only the one handler was called
672+ _ . each ( handlers , ( handler , handlerMethod ) => {
673+ if ( method === handlerMethod ) {
674+ assert . calledOnce ( handler ) ;
675+ } else {
676+ assert . notCalled ( handler ) ;
677+ }
678+ handler . resetHistory ( ) ;
679+ } ) ;
680+ } ) ;
681+ } ) ;
682+
683+ // Other tests
684+ _ . each ( methods , ( method ) => {
685+ // Ensure only exact matches trigger the route handler
686+ testOutcome ( method . toUpperCase ( ) , '/test/anything' , 'not found' ) ;
687+ } ) ;
688+ } ) ;
689+
690+ } ) ;
691+
595692} ) ;
0 commit comments