Commit 6293f4a 1 parent fda24b1 commit 6293f4a Copy full SHA for 6293f4a
File tree 2 files changed +45
-0
lines changed
2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,7 @@ type Route = {
72
72
pathname : string ;
73
73
params : string [ ] ;
74
74
handler : AnyRequestHandler [ ] ;
75
+ isActive : boolean ;
75
76
} ;
76
77
/**
77
78
* Events that may be emitted or listened to by HttpServer
@@ -218,6 +219,7 @@ export class HttpServer<T extends EventMap> extends TypedEmitter<T & BaseEvents>
218
219
pathname : `/${ basePathname } ` ,
219
220
params : paramsSegments ,
220
221
handler,
222
+ isActive : true ,
221
223
} ) ;
222
224
}
223
225
@@ -243,9 +245,24 @@ export class HttpServer<T extends EventMap> extends TypedEmitter<T & BaseEvents>
243
245
pathname : '*' ,
244
246
handler,
245
247
params : [ ] ,
248
+ isActive : true ,
246
249
} ) ;
247
250
}
248
251
252
+ public activateRoute ( pathname : string ) {
253
+ const route = this . routes . find ( r => r . pathname === pathname ) ;
254
+ if ( route ) {
255
+ route . isActive = true ;
256
+ }
257
+ }
258
+
259
+ public deactivateRoute ( pathname : string ) {
260
+ const route = this . routes . find ( r => r . pathname === pathname ) ;
261
+ if ( route ) {
262
+ route . isActive = false ;
263
+ }
264
+ }
265
+
249
266
/**
250
267
* pathname could be /a/b/c/d
251
268
* return route with highest number of matching segments
@@ -330,6 +347,13 @@ export class HttpServer<T extends EventMap> extends TypedEmitter<T & BaseEvents>
330
347
return ;
331
348
}
332
349
350
+ if ( ! route . isActive ) {
351
+ response . statusCode = 404 ;
352
+ response . end ( ) ;
353
+
354
+ return ;
355
+ }
356
+
333
357
const paramsSegments = pathname
334
358
. replace ( route . pathname , '' )
335
359
. split ( '/' )
Original file line number Diff line number Diff line change @@ -392,4 +392,25 @@ describe('HttpServer', () => {
392
392
) ;
393
393
expect ( res . status ) . toEqual ( 403 ) ;
394
394
} ) ;
395
+
396
+ test ( 'any registered route is active by default and can be deactivated' , async ( ) => {
397
+ const handler = jest . fn ( ( _request , response ) => {
398
+ response . end ( 'ok' ) ;
399
+ } ) ;
400
+ server . get ( '/foo' , [ handler ] ) ;
401
+ server . get ( '/bar' , [ handler ] ) ;
402
+ await server . start ( ) ;
403
+ const address = server . getServerAddress ( ) ;
404
+ expect ( address ) . toBeDefined ( ) ;
405
+ let res = await fetch ( `http://${ address . address } :${ address . port } /foo` ) ;
406
+ expect ( res . status ) . toEqual ( 200 ) ;
407
+ res = await fetch ( `http://${ address . address } :${ address . port } /bar` ) ;
408
+ expect ( res . status ) . toEqual ( 200 ) ;
409
+
410
+ server . deactivateRoute ( '/foo' ) ;
411
+ res = await fetch ( `http://${ address . address } :${ address . port } /foo` ) ;
412
+ expect ( res . status ) . toEqual ( 404 ) ;
413
+ res = await fetch ( `http://${ address . address } :${ address . port } /bar` ) ;
414
+ expect ( res . status ) . toEqual ( 200 ) ;
415
+ } ) ;
395
416
} ) ;
You can’t perform that action at this time.
0 commit comments