@@ -649,3 +649,104 @@ test('content-type regexp list should be cloned when plugin override', async t =
649
649
t . same ( payload , 'png' )
650
650
}
651
651
} )
652
+
653
+ test ( 'allow partial content-type - essence check' , async t => {
654
+ t . plan ( 1 )
655
+
656
+ const fastify = Fastify ( )
657
+ fastify . removeAllContentTypeParsers ( )
658
+ fastify . addContentTypeParser ( 'json' , function ( request , body , done ) {
659
+ t . pass ( 'should be called' )
660
+ done ( null , body )
661
+ } )
662
+
663
+ fastify . post ( '/' , async ( ) => {
664
+ return 'ok'
665
+ } )
666
+
667
+ await fastify . inject ( {
668
+ method : 'POST' ,
669
+ path : '/' ,
670
+ headers : {
671
+ 'content-type' : 'application/json; foo=bar; charset=utf8'
672
+ } ,
673
+ body : ''
674
+ } )
675
+
676
+ await fastify . inject ( {
677
+ method : 'POST' ,
678
+ path : '/' ,
679
+ headers : {
680
+ 'content-type' : 'image/jpeg'
681
+ } ,
682
+ body : ''
683
+ } )
684
+ } )
685
+
686
+ test ( 'allow partial content-type - not essence check' , async t => {
687
+ t . plan ( 1 )
688
+
689
+ const fastify = Fastify ( )
690
+ fastify . removeAllContentTypeParsers ( )
691
+ fastify . addContentTypeParser ( 'json;' , function ( request , body , done ) {
692
+ t . pass ( 'should be called' )
693
+ done ( null , body )
694
+ } )
695
+
696
+ fastify . post ( '/' , async ( ) => {
697
+ return 'ok'
698
+ } )
699
+
700
+ await fastify . inject ( {
701
+ method : 'POST' ,
702
+ path : '/' ,
703
+ headers : {
704
+ 'content-type' : 'application/json; foo=bar; charset=utf8'
705
+ } ,
706
+ body : ''
707
+ } )
708
+
709
+ await fastify . inject ( {
710
+ method : 'POST' ,
711
+ path : '/' ,
712
+ headers : {
713
+ 'content-type' : 'image/jpeg'
714
+ } ,
715
+ body : ''
716
+ } )
717
+ } )
718
+
719
+ test ( 'edge case content-type - ;' , async t => {
720
+ t . plan ( 1 )
721
+
722
+ const fastify = Fastify ( )
723
+ fastify . removeAllContentTypeParsers ( )
724
+ fastify . addContentTypeParser ( ';' , function ( request , body , done ) {
725
+ t . fail ( 'should not be called' )
726
+ done ( null , body )
727
+ } )
728
+
729
+ fastify . post ( '/' , async ( ) => {
730
+ return 'ok'
731
+ } )
732
+
733
+ await fastify . inject ( {
734
+ method : 'POST' ,
735
+ path : '/' ,
736
+ headers : {
737
+ 'content-type' : 'application/json; foo=bar; charset=utf8'
738
+ } ,
739
+ body : ''
740
+ } )
741
+
742
+ await fastify . inject ( {
743
+ method : 'POST' ,
744
+ path : '/' ,
745
+ headers : {
746
+ 'content-type' : 'image/jpeg'
747
+ } ,
748
+ body : ''
749
+ } )
750
+
751
+ t . pass ( 'end' )
752
+ } )
0 commit comments