@@ -379,6 +379,357 @@ describe('input-validation middleware tests', function () {
379379 } ) ;
380380 } ) ;
381381 } ) ;
382+ describe ( 'Simple server - with base path' , function ( ) {
383+ var app ;
384+ before ( function ( ) {
385+ return require ( './test-simple-server-with-base-path' ) . then ( function ( testServer ) {
386+ app = testServer ;
387+ } ) ;
388+ } ) ;
389+ it ( 'valid request - should pass validation' , function ( done ) {
390+ request ( app )
391+ . get ( '/v1/pets' )
392+ . set ( 'api-version' , '1.0' )
393+ . set ( 'request-id' , '123456' )
394+ . query ( { page : 0 } )
395+ . expect ( 200 , function ( err , res ) {
396+ if ( err ) {
397+ throw err ;
398+ }
399+ expect ( res . body . result ) . to . equal ( 'OK' ) ;
400+ done ( ) ;
401+ } ) ;
402+ } ) ;
403+ it ( 'missing header - should fail' , function ( done ) {
404+ request ( app )
405+ . get ( '/v1/pets' )
406+ . set ( 'request-id' , '123456' )
407+ . query ( { page : 0 } )
408+ . expect ( 400 , function ( err , res ) {
409+ if ( err ) {
410+ throw err ;
411+ }
412+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
413+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
414+ expect ( res . body . more_info ) . to . includes ( 'api-version' ) ;
415+ expect ( res . body . more_info ) . to . includes ( 'should have required property \'api-version\'' ) ;
416+ done ( ) ;
417+ } ) ;
418+ } ) ;
419+ it ( 'bad header - invalid pattern' , function ( done ) {
420+ request ( app )
421+ . get ( '/v1/pets' )
422+ . set ( 'request-id' , '123456' )
423+ . set ( 'api-version' , '1' )
424+ . query ( { page : 0 } )
425+ . expect ( 400 , function ( err , res ) {
426+ if ( err ) {
427+ throw err ;
428+ }
429+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
430+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
431+ expect ( res . body . more_info ) . to . includes ( 'api-version' ) ;
432+ expect ( res . body . more_info ) . to . includes ( 'should match pattern' ) ;
433+ done ( ) ;
434+ } ) ;
435+ } ) ;
436+ it ( 'bad header - empty header' , function ( done ) {
437+ request ( app )
438+ . get ( '/v1/pets' )
439+ . set ( 'request-id' , '' )
440+ . set ( 'api-version' , '1.0' )
441+ . query ( { page : 0 } )
442+ . expect ( 400 , function ( err , res ) {
443+ if ( err ) {
444+ throw err ;
445+ }
446+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
447+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
448+ expect ( res . body . more_info ) . to . includes ( 'request-id' ) ;
449+ expect ( res . body . more_info ) . to . includes ( 'should NOT be shorter than 1 characters' ) ;
450+ done ( ) ;
451+ } ) ;
452+ } ) ;
453+ it ( 'bad body - wrong type' , function ( done ) {
454+ request ( app )
455+ . post ( '/v1/pets' )
456+ . set ( 'request-id' , '123234' )
457+ . set ( 'api-version' , '1.0' )
458+ . send ( {
459+ name : '111' ,
460+ tag : 12344 ,
461+ 'test' : {
462+ field1 : '1'
463+ }
464+ } )
465+ . expect ( 400 , function ( err , res ) {
466+ if ( err ) {
467+ throw err ;
468+ }
469+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
470+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
471+ expect ( res . body . more_info ) . to . includes ( 'tag' ) ;
472+ done ( ) ;
473+ } ) ;
474+ } ) ;
475+ it ( 'bad body - missing required params' , function ( done ) {
476+ request ( app )
477+ . post ( '/v1/pets' )
478+ . set ( 'request-id' , '123324' )
479+ . set ( 'api-version' , '1.0' )
480+ . send ( {
481+ tag : 'tag' ,
482+ 'test' : {
483+ field1 : '1'
484+ }
485+ } )
486+ . expect ( 400 , function ( err , res ) {
487+ if ( err ) {
488+ throw err ;
489+ }
490+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
491+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
492+ expect ( res . body . more_info ) . to . includes ( 'name' ) ;
493+ done ( ) ;
494+ } ) ;
495+ } ) ;
496+ it ( 'bad body - missing required object attribute' , function ( done ) {
497+ request ( app )
498+ . post ( '/v1/pets' )
499+ . set ( 'request-id' , '123434' )
500+ . set ( 'api-version' , '1.0' )
501+ . send ( {
502+ name : 'name' ,
503+ tag : 'tag'
504+ } )
505+ . expect ( 400 , function ( err , res ) {
506+ if ( err ) {
507+ throw err ;
508+ }
509+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
510+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
511+ expect ( res . body . more_info ) . to . includes ( 'test' ) ;
512+ done ( ) ;
513+ } ) ;
514+ } ) ;
515+ it ( 'bad body - wrong type object attribute' , function ( done ) {
516+ request ( app )
517+ . post ( '/v1/pets' )
518+ . set ( 'request-id' , '12334' )
519+ . set ( 'api-version' , '1.0' )
520+ . send ( {
521+ name : 'name' ,
522+ tag : 'tag' ,
523+ test : ''
524+ } )
525+ . expect ( 400 , function ( err , res ) {
526+ if ( err ) {
527+ throw err ;
528+ }
529+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
530+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
531+ expect ( res . body . more_info ) . to . includes ( 'test' ) ;
532+ done ( ) ;
533+ } ) ;
534+ } ) ;
535+ it ( 'bad body - missing required nested attribute' , function ( done ) {
536+ request ( app )
537+ . post ( '/v1/pets' )
538+ . set ( 'request-id' , '12343' )
539+ . set ( 'api-version' , '1.0' )
540+ . send ( {
541+ name : 'name' ,
542+ tag : 'tag' ,
543+ test : { }
544+ } )
545+ . expect ( 400 , function ( err , res ) {
546+ if ( err ) {
547+ throw err ;
548+ }
549+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
550+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
551+ expect ( res . body . more_info ) . to . includes ( 'field1' ) ;
552+ done ( ) ;
553+ } ) ;
554+ } ) ;
555+ it ( 'bad body - wrong format nested attribute' , function ( done ) {
556+ request ( app )
557+ . post ( '/v1/pets' )
558+ . set ( 'request-id' , '12343' )
559+ . set ( 'api-version' , '1.0' )
560+ . send ( {
561+ name : 'name' ,
562+ tag : 'tag' ,
563+ test : {
564+ field1 : 1234
565+ }
566+ } )
567+ . expect ( 400 , function ( err , res ) {
568+ if ( err ) {
569+ throw err ;
570+ }
571+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
572+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
573+ expect ( res . body . more_info ) . to . includes ( 'field1' ) ;
574+ done ( ) ;
575+ } ) ;
576+ } ) ;
577+ it ( 'bad body - wrong enum value' , function ( done ) {
578+ request ( app )
579+ . post ( '/v1/pets' )
580+ . set ( 'request-id' , '1234' )
581+ . set ( 'api-version' , '1.0' )
582+ . send ( {
583+ name : 'name' ,
584+ tag : 'tag' ,
585+ test : {
586+ field1 : 'field1'
587+ }
588+ } )
589+ . expect ( 400 , function ( err , res ) {
590+ if ( err ) {
591+ throw err ;
592+ }
593+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
594+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
595+ expect ( res . body . more_info ) . to . includes ( 'should be equal to one of the allowed values' ) ;
596+ done ( ) ;
597+ } ) ;
598+ } ) ;
599+ it ( 'bad query param - missing required params' , function ( done ) {
600+ request ( app )
601+ . get ( '/v1/pets' )
602+ . set ( 'request-id' , '1234' )
603+ . set ( 'api-version' , '1.0' )
604+ . query ( { limit : 100 } )
605+ . expect ( 400 , function ( err , res ) {
606+ if ( err ) {
607+ throw err ;
608+ }
609+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
610+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
611+ expect ( res . body . more_info ) . to . includes ( 'page' ) ;
612+ done ( ) ;
613+ } ) ;
614+ } ) ;
615+ it ( 'bad query param - over limit' , function ( done ) {
616+ request ( app )
617+ . get ( '/v1/pets' )
618+ . set ( 'request-id' , '1234' )
619+ . set ( 'api-version' , '1.0' )
620+ . query ( { limit : 150 , page : 0 } )
621+ . expect ( 400 , function ( err , res ) {
622+ if ( err ) {
623+ throw err ;
624+ }
625+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
626+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
627+ expect ( res . body . more_info ) . to . includes ( 'limit' ) ;
628+ done ( ) ;
629+ } ) ;
630+ } ) ;
631+ it ( 'bad query param - under limit' , function ( done ) {
632+ request ( app )
633+ . get ( '/v1/pets' )
634+ . set ( 'request-id' , '1234' )
635+ . set ( 'api-version' , '1.0' )
636+ . query ( { limit : 0 , page : 0 } )
637+ . expect ( 400 , function ( err , res ) {
638+ if ( err ) {
639+ throw err ;
640+ }
641+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
642+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
643+ expect ( res . body . more_info ) . to . includes ( 'limit' ) ;
644+ done ( ) ;
645+ } ) ;
646+ } ) ;
647+ it ( 'bad path param - wrong format' , function ( done ) {
648+ request ( app )
649+ . get ( '/v1/pets/12' )
650+ . set ( 'request-id' , '1234' )
651+ . set ( 'api-version' , '1.0' )
652+ . query ( { limit : '50' , page : 0 } )
653+ . expect ( 400 , function ( err , res ) {
654+ if ( err ) {
655+ throw err ;
656+ }
657+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
658+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
659+ expect ( res . body . more_info ) . to . includes ( 'petId' ) ;
660+ done ( ) ;
661+ } ) ;
662+ } ) ;
663+ it ( 'bad body - wrong format nested attribute (not parameters)' , function ( done ) {
664+ request ( app )
665+ . put ( '/v1/pets' )
666+ . send ( [ {
667+ name : 'name' ,
668+ tag : 'tag' ,
669+ test : {
670+ field1 : 1234
671+ }
672+ } ] )
673+ . expect ( 400 , function ( err , res ) {
674+ if ( err ) {
675+ throw err ;
676+ }
677+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
678+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
679+ expect ( moreInfoAsJson . length ) . to . equal ( 2 ) ;
680+ expect ( res . body . more_info ) . to . includes ( 'field1' ) ;
681+ done ( ) ;
682+ } ) ;
683+ } ) ;
684+ it ( 'bad body - wrong format in array item body (second item)' , function ( done ) {
685+ request ( app )
686+ . put ( '/v1/pets' )
687+ . send ( [
688+ {
689+ name : 'name' ,
690+ tag : 'tag' ,
691+ test : {
692+ field1 : 'enum1'
693+ }
694+ } ,
695+ {
696+ name : 'name' ,
697+ tag : 'tag' ,
698+ test : {
699+ field1 : 1234
700+ }
701+ } ] )
702+ . expect ( 400 , function ( err , res ) {
703+ if ( err ) {
704+ throw err ;
705+ }
706+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
707+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
708+ expect ( res . body . more_info ) . to . includes ( '[1].test.field1' ) ;
709+ done ( ) ;
710+ } ) ;
711+ } ) ;
712+ it ( 'bad body - wrong format body (should be an array)' , function ( done ) {
713+ request ( app )
714+ . put ( '/v1/pets' )
715+ . send ( {
716+ name : 'name' ,
717+ tag : 'tag' ,
718+ test : {
719+ field1 : '1234'
720+ }
721+ } )
722+ . expect ( 400 , function ( err , res ) {
723+ if ( err ) {
724+ throw err ;
725+ }
726+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
727+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
728+ expect ( res . body . more_info ) . to . includes ( 'should be array' ) ;
729+ done ( ) ;
730+ } ) ;
731+ } ) ;
732+ } ) ;
382733 describe ( 'Simple server using routes' , function ( ) {
383734 var app ;
384735 before ( function ( ) {
0 commit comments