@@ -560,24 +560,42 @@ func Test_Ctx_Body_With_Compression(t *testing.T) {
560
560
body : []byte ("john=doe" ),
561
561
expectedBody : []byte ("john=doe" ),
562
562
},
563
+ {
564
+ name : "gzip twice" ,
565
+ contentEncoding : "gzip, gzip" ,
566
+ body : []byte ("double" ),
567
+ expectedBody : []byte ("double" ),
568
+ },
563
569
{
564
570
name : "unsupported_encoding" ,
565
571
contentEncoding : "undefined" ,
566
572
body : []byte ("keeps_ORIGINAL" ),
567
- expectedBody : []byte ("keeps_ORIGINAL" ),
573
+ expectedBody : []byte ("Unsupported Media Type" ),
574
+ },
575
+ {
576
+ name : "compress_not_implemented" ,
577
+ contentEncoding : "compress" ,
578
+ body : []byte ("foo" ),
579
+ expectedBody : []byte ("Not Implemented" ),
568
580
},
569
581
{
570
582
name : "gzip then unsupported" ,
571
583
contentEncoding : "gzip, undefined" ,
572
584
body : []byte ("Go, be gzipped" ),
573
- expectedBody : []byte ("Go, be gzipped " ),
585
+ expectedBody : []byte ("Unsupported Media Type " ),
574
586
},
575
587
{
576
588
name : "invalid_deflate" ,
577
589
contentEncoding : "gzip,deflate" ,
578
590
body : []byte ("I'm not correctly compressed" ),
579
591
expectedBody : []byte (zlib .ErrHeader .Error ()),
580
592
},
593
+ {
594
+ name : "identity" ,
595
+ contentEncoding : "identity" ,
596
+ body : []byte ("bar" ),
597
+ expectedBody : []byte ("bar" ),
598
+ },
581
599
}
582
600
583
601
for _ , testObject := range tests {
@@ -588,25 +606,45 @@ func Test_Ctx_Body_With_Compression(t *testing.T) {
588
606
c := app .AcquireCtx (& fasthttp.RequestCtx {}).(* DefaultCtx ) //nolint:errcheck,forcetypeassert // not needed
589
607
c .Request ().Header .Set ("Content-Encoding" , tCase .contentEncoding )
590
608
591
- if strings .Contains (tCase .contentEncoding , "gzip" ) {
592
- var b bytes.Buffer
593
- gz := gzip .NewWriter (& b )
594
-
595
- _ , err := gz .Write (tCase .body )
596
- require .NoError (t , err )
597
-
598
- err = gz .Flush ()
599
- require .NoError (t , err )
600
-
601
- err = gz .Close ()
602
- require .NoError (t , err )
603
- tCase .body = b .Bytes ()
609
+ encs := strings .Split (tCase .contentEncoding , "," )
610
+ for _ , enc := range encs {
611
+ enc = strings .TrimSpace (enc )
612
+ if strings .Contains (tCase .name , "invalid_deflate" ) && enc == StrDeflate {
613
+ continue
614
+ }
615
+ switch enc {
616
+ case "gzip" :
617
+ var b bytes.Buffer
618
+ gz := gzip .NewWriter (& b )
619
+ _ , err := gz .Write (tCase .body )
620
+ require .NoError (t , err )
621
+ require .NoError (t , gz .Flush ())
622
+ require .NoError (t , gz .Close ())
623
+ tCase .body = b .Bytes ()
624
+ case StrDeflate :
625
+ var b bytes.Buffer
626
+ fl := zlib .NewWriter (& b )
627
+ _ , err := fl .Write (tCase .body )
628
+ require .NoError (t , err )
629
+ require .NoError (t , fl .Flush ())
630
+ require .NoError (t , fl .Close ())
631
+ tCase .body = b .Bytes ()
632
+ }
604
633
}
605
634
606
635
c .Request ().SetBody (tCase .body )
607
636
body := c .Body ()
608
637
require .Equal (t , tCase .expectedBody , body )
609
638
639
+ switch {
640
+ case strings .Contains (tCase .name , "unsupported" ):
641
+ require .Equal (t , StatusUnsupportedMediaType , c .Response ().StatusCode ())
642
+ case strings .Contains (tCase .name , "compress_not_implemented" ):
643
+ require .Equal (t , StatusNotImplemented , c .Response ().StatusCode ())
644
+ default :
645
+ require .Equal (t , StatusOK , c .Response ().StatusCode ())
646
+ }
647
+
610
648
// Check if body raw is the same as previous before decompression
611
649
require .Equal (
612
650
t , tCase .body , c .Request ().Body (),
@@ -663,7 +701,7 @@ func Benchmark_Ctx_Body_With_Compression(b *testing.B) {
663
701
compressWriter : compressGzip ,
664
702
},
665
703
{
666
- contentEncoding : "deflate" ,
704
+ contentEncoding : StrDeflate ,
667
705
compressWriter : compressDeflate ,
668
706
},
669
707
{
@@ -751,24 +789,42 @@ func Test_Ctx_Body_With_Compression_Immutable(t *testing.T) {
751
789
body : []byte ("john=doe" ),
752
790
expectedBody : []byte ("john=doe" ),
753
791
},
792
+ {
793
+ name : "gzip twice" ,
794
+ contentEncoding : "gzip, gzip" ,
795
+ body : []byte ("double" ),
796
+ expectedBody : []byte ("double" ),
797
+ },
754
798
{
755
799
name : "unsupported_encoding" ,
756
800
contentEncoding : "undefined" ,
757
801
body : []byte ("keeps_ORIGINAL" ),
758
- expectedBody : []byte ("keeps_ORIGINAL" ),
802
+ expectedBody : []byte ("Unsupported Media Type" ),
803
+ },
804
+ {
805
+ name : "compress_not_implemented" ,
806
+ contentEncoding : "compress" ,
807
+ body : []byte ("foo" ),
808
+ expectedBody : []byte ("Not Implemented" ),
759
809
},
760
810
{
761
811
name : "gzip then unsupported" ,
762
812
contentEncoding : "gzip, undefined" ,
763
813
body : []byte ("Go, be gzipped" ),
764
- expectedBody : []byte ("Go, be gzipped " ),
814
+ expectedBody : []byte ("Unsupported Media Type " ),
765
815
},
766
816
{
767
817
name : "invalid_deflate" ,
768
818
contentEncoding : "gzip,deflate" ,
769
819
body : []byte ("I'm not correctly compressed" ),
770
820
expectedBody : []byte (zlib .ErrHeader .Error ()),
771
821
},
822
+ {
823
+ name : "identity" ,
824
+ contentEncoding : "identity" ,
825
+ body : []byte ("bar" ),
826
+ expectedBody : []byte ("bar" ),
827
+ },
772
828
}
773
829
774
830
for _ , testObject := range tests {
@@ -780,25 +836,45 @@ func Test_Ctx_Body_With_Compression_Immutable(t *testing.T) {
780
836
c := app .AcquireCtx (& fasthttp.RequestCtx {}).(* DefaultCtx ) //nolint:errcheck,forcetypeassert // not needed
781
837
c .Request ().Header .Set ("Content-Encoding" , tCase .contentEncoding )
782
838
783
- if strings .Contains (tCase .contentEncoding , "gzip" ) {
784
- var b bytes.Buffer
785
- gz := gzip .NewWriter (& b )
786
-
787
- _ , err := gz .Write (tCase .body )
788
- require .NoError (t , err )
789
-
790
- err = gz .Flush ()
791
- require .NoError (t , err )
792
-
793
- err = gz .Close ()
794
- require .NoError (t , err )
795
- tCase .body = b .Bytes ()
839
+ encs := strings .Split (tCase .contentEncoding , "," )
840
+ for _ , enc := range encs {
841
+ enc = strings .TrimSpace (enc )
842
+ if strings .Contains (tCase .name , "invalid_deflate" ) && enc == StrDeflate {
843
+ continue
844
+ }
845
+ switch enc {
846
+ case "gzip" :
847
+ var b bytes.Buffer
848
+ gz := gzip .NewWriter (& b )
849
+ _ , err := gz .Write (tCase .body )
850
+ require .NoError (t , err )
851
+ require .NoError (t , gz .Flush ())
852
+ require .NoError (t , gz .Close ())
853
+ tCase .body = b .Bytes ()
854
+ case StrDeflate :
855
+ var b bytes.Buffer
856
+ fl := zlib .NewWriter (& b )
857
+ _ , err := fl .Write (tCase .body )
858
+ require .NoError (t , err )
859
+ require .NoError (t , fl .Flush ())
860
+ require .NoError (t , fl .Close ())
861
+ tCase .body = b .Bytes ()
862
+ }
796
863
}
797
864
798
865
c .Request ().SetBody (tCase .body )
799
866
body := c .Body ()
800
867
require .Equal (t , tCase .expectedBody , body )
801
868
869
+ switch {
870
+ case strings .Contains (tCase .name , "unsupported" ):
871
+ require .Equal (t , StatusUnsupportedMediaType , c .Response ().StatusCode ())
872
+ case strings .Contains (tCase .name , "compress_not_implemented" ):
873
+ require .Equal (t , StatusNotImplemented , c .Response ().StatusCode ())
874
+ default :
875
+ require .Equal (t , StatusOK , c .Response ().StatusCode ())
876
+ }
877
+
802
878
// Check if body raw is the same as previous before decompression
803
879
require .Equal (
804
880
t , tCase .body , c .Request ().Body (),
@@ -855,7 +931,7 @@ func Benchmark_Ctx_Body_With_Compression_Immutable(b *testing.B) {
855
931
compressWriter : compressGzip ,
856
932
},
857
933
{
858
- contentEncoding : "deflate" ,
934
+ contentEncoding : StrDeflate ,
859
935
compressWriter : compressDeflate ,
860
936
},
861
937
{
0 commit comments