@@ -49,6 +49,26 @@ describe('server', () => {
4949 res . end ( JSON . stringify ( queryString ) ) ;
5050 } ) ;
5151
52+ router . get ( "/test/get/method" , ( req , res ) => {
53+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
54+ res . end ( "test text" ) ;
55+ } )
56+
57+ router . post ( "/test/post/method" , ( req , res ) => {
58+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
59+ res . end ( "test text" ) ;
60+ } )
61+
62+ router . put ( "/test/put/method" , ( req , res ) => {
63+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
64+ res . end ( "test text" ) ;
65+ } )
66+
67+ router . delete ( "/test/delete/method" , ( req , res ) => {
68+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
69+ res . end ( "test text" ) ;
70+ } )
71+
5272 router . addAssetPath ( 'images' , 'tests/images/' ) ;
5373 router . addAssetPath ( 'js' , 'tests/js/' ) ;
5474
@@ -361,6 +381,166 @@ describe('server', () => {
361381 } ) ;
362382 } ) ;
363383
384+ describe ( "Routes with specific request methods" , function ( ) {
385+ it ( "it should only accept get method requests" , ( done ) => {
386+ Promise . all ( [
387+ new Promise ( ( resolve , reject ) => {
388+ http . get ( `${ SERVER_URL } /test/get/method` , ( res ) => {
389+ res . statusCode . should . equal ( 200 ) ;
390+
391+ let body = '' ;
392+ res . on ( 'data' , ( chunk ) => body += chunk )
393+ . on ( "end" , ( ) => {
394+ body . should . equal ( "test text" )
395+ resolve ( ) ;
396+ } ) ;
397+ } ) ;
398+ } ) ,
399+ new Promise ( ( resolve , reject ) => {
400+ const req = http . request ( {
401+ url : `http://localhost/` ,
402+ port : '8000' ,
403+ method : 'POST' ,
404+ path : "/test/get/method" ,
405+ } , ( res ) => {
406+ res . statusCode . should . equal ( 405 ) ;
407+ let body = "" ;
408+ res . on ( "data" , ( chunk ) => body += chunk )
409+ . on ( "end" , ( ) => {
410+ body . should . equal ( "Method not allowed for /test/get/method" )
411+ resolve ( ) ;
412+ } ) ;
413+ } ) ;
414+
415+ req . on ( 'error' , ( err ) => {
416+ throw err ;
417+ } ) ;
418+
419+ req . end ( ) ;
420+ } )
421+ ] ) . then ( ( ) => done ( ) ) ;
422+ } ) ;
423+
424+ it ( "it should only accept post requests" , ( done ) => {
425+ const path = "/test/post/method" ;
426+ Promise . all ( [
427+ new Promise ( ( resolve , reject ) => {
428+ const req = http . request ( {
429+ url : "http://locahost" ,
430+ port : "8000" ,
431+ method : "POST" ,
432+ path,
433+ } , res => {
434+ res . statusCode . should . equal ( 200 ) ;
435+ let body = "" ;
436+ res . on ( "data" , chunk => body += chunk )
437+ . on ( "end" , ( ) => {
438+ body . should . equal ( "test text" ) ;
439+ resolve ( ) ;
440+ } ) ;
441+ } ) ;
442+
443+ req . on ( "error" , err => {
444+ throw err
445+ } ) ;
446+ req . end ( ) ;
447+ } ) ,
448+ new Promise ( ( resolve , reject ) => {
449+ http . get ( `${ SERVER_URL } ${ path } ` , res => {
450+ res . statusCode . should . equal ( 405 ) ;
451+
452+ let body = "" ;
453+ res . on ( "data" , chunk => body += chunk )
454+ . on ( "end" , ( ) => {
455+ body . should . equal ( `Method not allowed for ${ path } ` )
456+ resolve ( ) ;
457+ } ) ;
458+ } ) ;
459+ } )
460+ ] ) . then ( ( ) => done ( ) ) ;
461+ } ) ;
462+
463+ it ( "it should only accept put method requests" , ( done ) => {
464+ const path = "/test/put/method" ;
465+
466+ Promise . all ( [
467+ new Promise ( ( resolve , reject ) => {
468+ const req = http . request ( {
469+ url : "http://locahost" ,
470+ port : 8000 ,
471+ path,
472+ method : "PUT" ,
473+ } , res => {
474+ res . statusCode . should . equal ( 200 ) ;
475+
476+ let body = "" ;
477+ res . on ( "data" , chunk => body += chunk )
478+ . on ( "end" , ( ) => {
479+ body . should . equal ( "test text" ) ;
480+ resolve ( )
481+ } ) ;
482+ } ) ;
483+
484+ req . on ( "error" , ( err ) => {
485+ throw err ;
486+ } ) . end ( ) ;
487+ } ) ,
488+ new Promise ( ( resolve , reject ) => {
489+ http . get ( `${ SERVER_URL } ${ path } ` , res => {
490+ res . statusCode . should . equal ( 405 ) ;
491+
492+ let body = "" ;
493+ res . on ( "data" , chunk => body += chunk )
494+ . on ( "end" , ( ) => {
495+ body . should . equal ( `Method not allowed for ${ path } ` )
496+ resolve ( ) ;
497+ } ) ;
498+ } ) ;
499+ } )
500+ ] ) . then ( ( ) => done ( ) ) ;
501+ } ) ;
502+
503+ it ( "it should only accept delete method requests" , ( done ) => {
504+ const path = "/test/delete/method" ;
505+
506+ Promise . all ( [
507+ new Promise ( ( resolve , reject ) => {
508+ const req = http . request ( {
509+ url : "http://locahost" ,
510+ port : 8000 ,
511+ path,
512+ method : "DELETE" ,
513+ } , res => {
514+ res . statusCode . should . equal ( 200 ) ;
515+
516+ let body = "" ;
517+ res . on ( "data" , chunk => body += chunk )
518+ . on ( "end" , ( ) => {
519+ body . should . equal ( "test text" ) ;
520+ resolve ( )
521+ } ) ;
522+ } ) ;
523+
524+ req . on ( "error" , ( err ) => {
525+ throw err ;
526+ } ) . end ( ) ;
527+ } ) ,
528+ new Promise ( ( resolve , reject ) => {
529+ http . get ( `${ SERVER_URL } ${ path } ` , res => {
530+ res . statusCode . should . equal ( 405 ) ;
531+
532+ let body = "" ;
533+ res . on ( "data" , chunk => body += chunk )
534+ . on ( "end" , ( ) => {
535+ body . should . equal ( `Method not allowed for ${ path } ` )
536+ resolve ( ) ;
537+ } ) ;
538+ } ) ;
539+ } )
540+ ] ) . then ( ( ) => done ( ) ) ;
541+ } )
542+ } ) ;
543+
364544 after ( ( ) => {
365545 router . close ( ) ;
366546 } ) ;
0 commit comments