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