@@ -9,6 +9,12 @@ 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 = process . versions ? 'brotli' in process . versions : false
17
+
12
18
describe ( 'compression()' , function ( ) {
13
19
it ( 'should skip HEAD' , function ( done ) {
14
20
var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -465,6 +471,20 @@ describe('compression()', function () {
465
471
} )
466
472
} )
467
473
474
+ ( hasBrotliSupport ? describe . skip : describe ) ( 'when "Accept-Encoding: br"' , function ( ) {
475
+ it ( 'should respond with br' , function ( done ) {
476
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
477
+ res . setHeader ( 'Content-Type' , 'text/plain' )
478
+ res . end ( 'hello, world' )
479
+ } )
480
+
481
+ request ( server )
482
+ . get ( '/' )
483
+ . set ( 'Accept-Encoding' , 'br' )
484
+ . expect ( 'Content-Encoding' , 'br' , done )
485
+ } )
486
+ } )
487
+
468
488
describe ( 'when "Accept-Encoding: gzip, deflate"' , function ( ) {
469
489
it ( 'should respond with gzip' , function ( done ) {
470
490
var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -493,6 +513,20 @@ describe('compression()', function () {
493
513
} )
494
514
} )
495
515
516
+ ( hasBrotliSupport ? describe . skip : describe ) ( 'when "Accept-Encoding: deflate, br"' , function ( ) {
517
+ it ( 'should respond with br' , function ( done ) {
518
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
519
+ res . setHeader ( 'Content-Type' , 'text/plain' )
520
+ res . end ( 'hello, world' )
521
+ } )
522
+
523
+ request ( server )
524
+ . get ( '/' )
525
+ . set ( 'Accept-Encoding' , 'deflate, br' )
526
+ . expect ( 'Content-Encoding' , 'br' , done )
527
+ } )
528
+ } )
529
+
496
530
describe ( 'when "Cache-Control: no-transform" response header' , function ( ) {
497
531
it ( 'should not compress response' , function ( done ) {
498
532
var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -631,6 +665,32 @@ describe('compression()', function () {
631
665
. end ( )
632
666
} )
633
667
668
+ ( hasBrotliSupport ? it . skip : it ) ( 'should flush small chunks for brotli' , function ( done ) {
669
+ var chunks = 0
670
+ var next
671
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
672
+ next = writeAndFlush ( res , 2 , Buffer . from ( '..' ) )
673
+ res . setHeader ( 'Content-Type' , 'text/plain' )
674
+ next ( )
675
+ } )
676
+
677
+ function onchunk ( chunk ) {
678
+ assert . ok ( chunks ++ < 20 )
679
+ assert . strictEqual ( chunk . toString ( ) , '..' )
680
+ next ( )
681
+ }
682
+
683
+ request ( server )
684
+ . get ( '/' )
685
+ . set ( 'Accept-Encoding' , 'br' )
686
+ . request ( )
687
+ . on ( 'response' , unchunk ( 'br' , onchunk , function ( err ) {
688
+ if ( err ) return done ( err )
689
+ server . close ( done )
690
+ } ) )
691
+ . end ( )
692
+ } )
693
+
634
694
it ( 'should flush small chunks for deflate' , function ( done ) {
635
695
var chunks = 0
636
696
var next
@@ -710,6 +770,9 @@ function unchunk (encoding, onchunk, onend) {
710
770
case 'gzip' :
711
771
stream = res . pipe ( zlib . createGunzip ( ) )
712
772
break
773
+ case 'br' :
774
+ stream = res . pipe ( zlib . createBrotliDecompress ( ) )
775
+ break
713
776
}
714
777
715
778
stream . on ( 'data' , onchunk )
0 commit comments