33 Get ,
44 MiddlewareConsumer ,
55 Module ,
6- Param ,
76 Query ,
87 RequestMethod ,
98} from '@nestjs/common' ;
@@ -15,16 +14,12 @@ import { Test } from '@nestjs/testing';
1514import { expect } from 'chai' ;
1615import { ApplicationModule } from '../src/app.module' ;
1716
18- const OPTIONAL_PARAM_VALUE = 'test_optional_param' ;
19- const FOR_ROUTE_CONTROLLER_VALUE = 'test_for_route_controller' ;
20- const FOR_ROUTE_PATH_VALUE = 'test_for_route_path' ;
2117const INCLUDED_VALUE = 'test_included' ;
2218const QUERY_VALUE = 'test_query' ;
2319const REQ_URL_VALUE = 'test_req_url' ;
2420const RETURN_VALUE = 'test' ;
2521const SCOPED_VALUE = 'test_scoped' ;
2622const WILDCARD_VALUE = 'test_wildcard' ;
27- const CATCH_ALL_VALUE = 'test_catch_all' ;
2823
2924@Controller ( )
3025class TestController {
@@ -62,35 +57,9 @@ class TestQueryController {
6257 }
6358}
6459
65- @Controller ( OPTIONAL_PARAM_VALUE )
66- class TestParamController {
67- @Get ( ':test' )
68- [ OPTIONAL_PARAM_VALUE ] ( ) {
69- return RETURN_VALUE ;
70- }
71- }
72-
73- @Controller ( )
74- class ForRouteController {
75- @Get ( 'for_route_controller' )
76- forRouteController ( ) {
77- return RETURN_VALUE ;
78- }
79-
80- @Get ( 'for_route_controller/required_param/:param' )
81- requiredParam ( ) {
82- return RETURN_VALUE ;
83- }
84- }
85-
8660@Module ( {
8761 imports : [ ApplicationModule ] ,
88- controllers : [
89- TestController ,
90- TestQueryController ,
91- TestParamController ,
92- ForRouteController ,
93- ] ,
62+ controllers : [ TestController , TestQueryController ] ,
9463} )
9564class TestModule {
9665 configure ( consumer : MiddlewareConsumer ) {
@@ -107,17 +76,8 @@ class TestModule {
10776 . forRoutes ( TestQueryController )
10877 . apply ( ( req , res , next ) => res . end ( SCOPED_VALUE ) )
10978 . forRoutes ( TestController )
110- . apply ( ( req , res , next ) => res . end ( FOR_ROUTE_PATH_VALUE ) )
111- . forRoutes ( { path : 'for_route_path' , method : RequestMethod . GET } )
112- . apply ( ( req , res , next ) => res . end ( FOR_ROUTE_CONTROLLER_VALUE ) )
113- . forRoutes ( ForRouteController )
114- . apply ( ( req , res , next ) => res . end ( OPTIONAL_PARAM_VALUE ) )
115- . forRoutes ( { path : `${ OPTIONAL_PARAM_VALUE } /:test?` , method : RequestMethod . GET } )
116- . apply ( ( req , res , next ) => res . end ( CATCH_ALL_VALUE ) )
117- . exclude (
118- { path : QUERY_VALUE , method : RequestMethod . ALL } ,
119- { path : `${ OPTIONAL_PARAM_VALUE } /(.*)` , method : RequestMethod . ALL } ,
120- )
79+ . apply ( ( req , res , next ) => res . end ( RETURN_VALUE ) )
80+ . exclude ( { path : QUERY_VALUE , method : - 1 } )
12181 . forRoutes ( '(.*)' ) ;
12282 }
12383}
@@ -141,7 +101,7 @@ describe('Middleware (FastifyAdapter)', () => {
141101 method : 'GET' ,
142102 url : '/hello' ,
143103 } )
144- . then ( ( { payload } ) => expect ( payload ) . to . be . eql ( CATCH_ALL_VALUE ) ) ;
104+ . then ( ( { payload } ) => expect ( payload ) . to . be . eql ( RETURN_VALUE ) ) ;
145105 } ) ;
146106
147107 it ( `forRoutes(TestController)` , ( ) => {
@@ -225,82 +185,6 @@ describe('Middleware (FastifyAdapter)', () => {
225185 . then ( ( { payload } ) => expect ( payload ) . to . be . eql ( INCLUDED_VALUE ) ) ;
226186 } ) ;
227187
228- it ( `/for_route_path forRoutes(/for_route_path)` , ( ) => {
229- return app
230- . inject ( {
231- method : 'GET' ,
232- url : '/for_route_path' ,
233- } )
234- . then ( ( { payload } ) => expect ( payload ) . to . be . eql ( FOR_ROUTE_PATH_VALUE ) ) ;
235- } ) ;
236-
237- it ( `/for_route_path/test forRoutes(/for_route_path)` , ( ) => {
238- return app
239- . inject ( {
240- method : 'GET' ,
241- url : '/for_route_path/test' ,
242- } )
243- . then ( ( { payload } ) => expect ( payload ) . to . be . eql ( FOR_ROUTE_PATH_VALUE ) ) ;
244- } ) ;
245-
246- it ( `/for_route_controller forRoutes(ForRouteController)` , ( ) => {
247- return app
248- . inject ( {
249- method : 'GET' ,
250- url : '/for_route_controller' ,
251- } )
252- . then ( ( { payload } ) =>
253- expect ( payload ) . to . be . eql ( FOR_ROUTE_CONTROLLER_VALUE ) ,
254- ) ;
255- } ) ;
256-
257- it ( `/for_route_controller/test forRoutes(ForRouteController)` , ( ) => {
258- return app
259- . inject ( {
260- method : 'GET' ,
261- url : '/for_route_controller/test' ,
262- } )
263- . then ( ( { payload } ) => expect ( payload ) . to . be . eql ( CATCH_ALL_VALUE ) ) ;
264- } ) ;
265-
266- it ( `/for_route_controller/required_param/ forRoutes(ForRouteController)` , ( ) => {
267- return app
268- . inject ( {
269- method : 'GET' ,
270- url : '/for_route_controller/required_param/' ,
271- } )
272- . then ( ( { payload } ) => expect ( payload ) . to . be . eql ( CATCH_ALL_VALUE ) ) ;
273- } ) ;
274-
275- it ( `/for_route_controller/required_param/test forRoutes(ForRouteController)` , ( ) => {
276- return app
277- . inject ( {
278- method : 'GET' ,
279- url : '/for_route_controller/required_param/test' ,
280- } )
281- . then ( ( { payload } ) =>
282- expect ( payload ) . to . be . eql ( FOR_ROUTE_CONTROLLER_VALUE ) ,
283- ) ;
284- } ) ;
285-
286- it ( `/${ OPTIONAL_PARAM_VALUE } / forRoutes(${ OPTIONAL_PARAM_VALUE } /:test?)` , ( ) => {
287- return app
288- . inject ( {
289- method : 'GET' ,
290- url : `/${ OPTIONAL_PARAM_VALUE } /` ,
291- } )
292- . then ( ( { payload } ) => expect ( payload ) . to . be . eql ( OPTIONAL_PARAM_VALUE ) ) ;
293- } ) ;
294-
295- it ( `/${ OPTIONAL_PARAM_VALUE } /test forRoutes(${ OPTIONAL_PARAM_VALUE } /:test?)` , ( ) => {
296- return app
297- . inject ( {
298- method : 'GET' ,
299- url : `/${ OPTIONAL_PARAM_VALUE } /test` ,
300- } )
301- . then ( ( { payload } ) => expect ( payload ) . to . be . eql ( OPTIONAL_PARAM_VALUE ) ) ;
302- } ) ;
303-
304188 afterEach ( async ( ) => {
305189 await app . close ( ) ;
306190 } ) ;
0 commit comments