@@ -9,6 +9,8 @@ var zlib = require('zlib')
9
9
10
10
var compression = require ( '..' )
11
11
12
+ var hasBrotliSupport = 'createBrotliCompress' in zlib
13
+
12
14
describe ( 'compression()' , function ( ) {
13
15
it ( 'should skip HEAD' , function ( done ) {
14
16
var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -656,6 +658,201 @@ describe('compression()', function () {
656
658
} ) )
657
659
. end ( )
658
660
} )
661
+
662
+ var brotlit = hasBrotliSupport ? it : it . skip
663
+ brotlit ( 'should flush small chunks for brotli' , function ( done ) {
664
+ var chunks = 0
665
+ var next
666
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
667
+ next = writeAndFlush ( res , 2 , Buffer . from ( '..' ) )
668
+ res . setHeader ( 'Content-Type' , 'text/plain' )
669
+ next ( )
670
+ } )
671
+
672
+ function onchunk ( chunk ) {
673
+ assert . ok ( chunks ++ < 20 )
674
+ assert . strictEqual ( chunk . toString ( ) , '..' )
675
+ next ( )
676
+ }
677
+
678
+ request ( server )
679
+ . get ( '/' )
680
+ . set ( 'Accept-Encoding' , 'br' )
681
+ . request ( )
682
+ . on ( 'response' , unchunk ( 'br' , onchunk , function ( err ) {
683
+ if ( err ) return done ( err )
684
+ server . close ( done )
685
+ } ) )
686
+ . end ( )
687
+ } )
688
+ } )
689
+
690
+ describe ( 'when "Accept-Encoding: br"' , function ( ) {
691
+ var brotlit = hasBrotliSupport ? it : it . skip
692
+ brotlit ( 'should respond with br' , function ( done ) {
693
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
694
+ res . setHeader ( 'Content-Type' , 'text/plain' )
695
+ res . end ( 'hello, world' )
696
+ } )
697
+
698
+ request ( server )
699
+ . get ( '/' )
700
+ . set ( 'Accept-Encoding' , 'br' )
701
+ . expect ( 'Content-Encoding' , 'br' , done )
702
+ } )
703
+ } )
704
+
705
+ describe ( 'when "Accept-Encoding: br" and passing compression level' , function ( ) {
706
+ var brotlit = hasBrotliSupport ? it : it . skip
707
+ brotlit ( 'should respond with br' , function ( done ) {
708
+ var params = { }
709
+ params [ zlib . constants . BROTLI_PARAM_QUALITY ] = 11
710
+
711
+ var server = createServer ( { threshold : 0 , params : params } , function ( req , res ) {
712
+ res . setHeader ( 'Content-Type' , 'text/plain' )
713
+ res . end ( 'hello, world' )
714
+ } )
715
+
716
+ request ( server )
717
+ . get ( '/' )
718
+ . set ( 'Accept-Encoding' , 'br' )
719
+ . expect ( 'Content-Encoding' , 'br' , done )
720
+ } )
721
+ } )
722
+
723
+ describe ( 'when "Accept-Encoding: gzip, br"' , function ( ) {
724
+ var brotlit = hasBrotliSupport ? it : it . skip
725
+ brotlit ( 'should respond with br' , function ( done ) {
726
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
727
+ res . setHeader ( 'Content-Type' , 'text/plain' )
728
+ res . end ( 'hello, world' )
729
+ } )
730
+
731
+ request ( server )
732
+ . get ( '/' )
733
+ . set ( 'Accept-Encoding' , 'gzip, br' )
734
+ . expect ( 'Content-Encoding' , 'br' , done )
735
+ } )
736
+ } )
737
+
738
+ describe ( 'when "Accept-Encoding: deflate, gzip, br"' , function ( ) {
739
+ var brotlit = hasBrotliSupport ? it : it . skip
740
+ brotlit ( 'should respond with br' , function ( done ) {
741
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
742
+ res . setHeader ( 'Content-Type' , 'text/plain' )
743
+ res . end ( 'hello, world' )
744
+ } )
745
+
746
+ request ( server )
747
+ . get ( '/' )
748
+ . set ( 'Accept-Encoding' , 'deflate, gzip, br' )
749
+ . expect ( 'Content-Encoding' , 'br' , done )
750
+ } )
751
+ } )
752
+
753
+ describe ( 'when "Accept-Encoding: gzip;q=1, br;q=0.3"' , function ( ) {
754
+ var brotlit = hasBrotliSupport ? it : it . skip
755
+ brotlit ( 'should respond with gzip' , function ( done ) {
756
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
757
+ res . setHeader ( 'Content-Type' , 'text/plain' )
758
+ res . end ( 'hello, world' )
759
+ } )
760
+
761
+ request ( server )
762
+ . get ( '/' )
763
+ . set ( 'Accept-Encoding' , 'gzip;q=1, br;q=0.3' )
764
+ . expect ( 'Content-Encoding' , 'gzip' , done )
765
+ } )
766
+ } )
767
+
768
+ describe ( 'when "Accept-Encoding: gzip, br;q=0.8"' , function ( ) {
769
+ var brotlit = hasBrotliSupport ? it : it . skip
770
+ brotlit ( 'should respond with gzip' , function ( done ) {
771
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
772
+ res . setHeader ( 'Content-Type' , 'text/plain' )
773
+ res . end ( 'hello, world' )
774
+ } )
775
+
776
+ request ( server )
777
+ . get ( '/' )
778
+ . set ( 'Accept-Encoding' , 'gzip, br;q=0.8' )
779
+ . expect ( 'Content-Encoding' , 'gzip' , done )
780
+ } )
781
+ } )
782
+
783
+ describe ( 'when "Accept-Encoding: gzip;q=0.001"' , function ( ) {
784
+ var brotlit = hasBrotliSupport ? it : it . skip
785
+ brotlit ( 'should respond with gzip' , function ( done ) {
786
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
787
+ res . setHeader ( 'Content-Type' , 'text/plain' )
788
+ res . end ( 'hello, world' )
789
+ } )
790
+
791
+ request ( server )
792
+ . get ( '/' )
793
+ . set ( 'Accept-Encoding' , 'gzip;q=0.001' )
794
+ . expect ( 'Content-Encoding' , 'gzip' , done )
795
+ } )
796
+ } )
797
+
798
+ describe ( 'when "Accept-Encoding: deflate, br"' , function ( ) {
799
+ var brotlit = hasBrotliSupport ? it : it . skip
800
+ brotlit ( 'should respond with br' , function ( done ) {
801
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
802
+ res . setHeader ( 'Content-Type' , 'text/plain' )
803
+ res . end ( 'hello, world' )
804
+ } )
805
+
806
+ request ( server )
807
+ . get ( '/' )
808
+ . set ( 'Accept-Encoding' , 'deflate, br' )
809
+ . expect ( 'Content-Encoding' , 'br' , done )
810
+ } )
811
+ } )
812
+
813
+ describe ( 'when "Accept-Encoding: deflate, br, gzip"' , function ( ) {
814
+ var brotlit = hasBrotliSupport ? it : it . skip
815
+ brotlit ( 'should respond with br' , function ( done ) {
816
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
817
+ res . setHeader ( 'Content-Type' , 'text/plain' )
818
+ res . end ( 'hello, world' )
819
+ } )
820
+
821
+ request ( server )
822
+ . get ( '/' )
823
+ . set ( 'Accept-Encoding' , 'deflate, br, gzip' )
824
+ . expect ( 'Content-Encoding' , 'br' , done )
825
+ } )
826
+ } )
827
+
828
+ describe ( 'when "Accept-Encoding: deflate, gzip"' , function ( ) {
829
+ var brotlit = hasBrotliSupport ? it : it . skip
830
+ brotlit ( 'should respond with br' , function ( done ) {
831
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
832
+ res . setHeader ( 'Content-Type' , 'text/plain' )
833
+ res . end ( 'hello, world' )
834
+ } )
835
+
836
+ request ( server )
837
+ . get ( '/' )
838
+ . set ( 'Accept-Encoding' , 'deflate, gzip' )
839
+ . expect ( 'Content-Encoding' , 'gzip' , done )
840
+ } )
841
+ } )
842
+
843
+ describe ( 'when "Accept-Encoding: deflate, identity"' , function ( ) {
844
+ var brotlit = hasBrotliSupport ? it : it . skip
845
+ brotlit ( 'should respond with br' , function ( done ) {
846
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
847
+ res . setHeader ( 'Content-Type' , 'text/plain' )
848
+ res . end ( 'hello, world' )
849
+ } )
850
+
851
+ request ( server )
852
+ . get ( '/' )
853
+ . set ( 'Accept-Encoding' , 'deflate, identity' )
854
+ . expect ( 'Content-Encoding' , 'deflate' , done )
855
+ } )
659
856
} )
660
857
} )
661
858
@@ -710,6 +907,9 @@ function unchunk (encoding, onchunk, onend) {
710
907
case 'gzip' :
711
908
stream = res . pipe ( zlib . createGunzip ( ) )
712
909
break
910
+ case 'br' :
911
+ stream = res . pipe ( zlib . createBrotliDecompress ( ) )
912
+ break
713
913
}
714
914
715
915
stream . on ( 'data' , onchunk )
0 commit comments