@@ -9,6 +9,16 @@ var zlib = require('zlib')
9
9
10
10
var compression = require ( '..' )
11
11
12
+ /**
13
+ * @const
14
+ * whether current node version has brotli support
15
+ */
16
+ var hasBrotliSupport = false
17
+ try {
18
+ hasBrotliSupport = 'brotli' in require ( 'process' ) . versions
19
+ } catch ( ignored ) {
20
+ }
21
+
12
22
describe ( 'compression()' , function ( ) {
13
23
it ( 'should skip HEAD' , function ( done ) {
14
24
var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -465,6 +475,20 @@ describe('compression()', function () {
465
475
} )
466
476
} )
467
477
478
+ describe ( 'when "Accept-Encoding: br"' , function ( ) {
479
+ it ( 'should respond with br' , function ( done ) {
480
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
481
+ res . setHeader ( 'Content-Type' , 'text/plain' )
482
+ res . end ( 'hello, world' )
483
+ } )
484
+
485
+ request ( server )
486
+ . get ( '/' )
487
+ . set ( 'Accept-Encoding' , 'br' )
488
+ . expect ( 'Content-Encoding' , 'br' , done )
489
+ } )
490
+ } )
491
+
468
492
describe ( 'when "Accept-Encoding: gzip, deflate"' , function ( ) {
469
493
it ( 'should respond with gzip' , function ( done ) {
470
494
var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -493,6 +517,20 @@ describe('compression()', function () {
493
517
} )
494
518
} )
495
519
520
+ describe ( 'when "Accept-Encoding: deflate, br"' , function ( ) {
521
+ it ( 'should respond with br' , function ( done ) {
522
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
523
+ res . setHeader ( 'Content-Type' , 'text/plain' )
524
+ res . end ( 'hello, world' )
525
+ } )
526
+
527
+ request ( server )
528
+ . get ( '/' )
529
+ . set ( 'Accept-Encoding' , 'deflate, br' )
530
+ . expect ( 'Content-Encoding' , 'br' , done )
531
+ } )
532
+ } )
533
+
496
534
describe ( 'when "Cache-Control: no-transform" response header' , function ( ) {
497
535
it ( 'should not compress response' , function ( done ) {
498
536
var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -631,6 +669,32 @@ describe('compression()', function () {
631
669
. end ( )
632
670
} )
633
671
672
+ it ( 'should flush small chunks for br' , function ( done ) {
673
+ var chunks = 0
674
+ var next
675
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
676
+ next = writeAndFlush ( res , 2 , Buffer . from ( '..' ) )
677
+ res . setHeader ( 'Content-Type' , 'text/plain' )
678
+ next ( )
679
+ } )
680
+
681
+ function onchunk ( chunk ) {
682
+ assert . ok ( chunks ++ < 20 )
683
+ assert . strictEqual ( chunk . toString ( ) , '..' )
684
+ next ( )
685
+ }
686
+
687
+ request ( server )
688
+ . get ( '/' )
689
+ . set ( 'Accept-Encoding' , 'br' )
690
+ . request ( )
691
+ . on ( 'response' , unchunk ( 'br' , onchunk , function ( err ) {
692
+ if ( err ) return done ( err )
693
+ server . close ( done )
694
+ } ) )
695
+ . end ( )
696
+ } )
697
+
634
698
it ( 'should flush small chunks for deflate' , function ( done ) {
635
699
var chunks = 0
636
700
var next
@@ -710,6 +774,9 @@ function unchunk (encoding, onchunk, onend) {
710
774
case 'gzip' :
711
775
stream = res . pipe ( zlib . createGunzip ( ) )
712
776
break
777
+ case 'br' :
778
+ stream = res . pipe ( zlib . createBrotliDecompress ( ) )
779
+ break
713
780
}
714
781
715
782
stream . on ( 'data' , onchunk )
0 commit comments