@@ -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,22 @@ describe('compression()', function () {
465
475
} )
466
476
} )
467
477
478
+ if ( hasBrotliSupport ) {
479
+ describe ( 'when "Accept-Encoding: br"' , function ( ) {
480
+ it ( 'should respond with br' , function ( done ) {
481
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
482
+ res . setHeader ( 'Content-Type' , 'text/plain' )
483
+ res . end ( 'hello, world' )
484
+ } )
485
+
486
+ request ( server )
487
+ . get ( '/' )
488
+ . set ( 'Accept-Encoding' , 'br' )
489
+ . expect ( 'Content-Encoding' , 'br' , done )
490
+ } )
491
+ } )
492
+ }
493
+
468
494
describe ( 'when "Accept-Encoding: gzip, deflate"' , function ( ) {
469
495
it ( 'should respond with gzip' , function ( done ) {
470
496
var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -493,6 +519,22 @@ describe('compression()', function () {
493
519
} )
494
520
} )
495
521
522
+ if ( hasBrotliSupport ) {
523
+ describe ( 'when "Accept-Encoding: deflate, br"' , function ( ) {
524
+ it ( 'should respond with br' , function ( done ) {
525
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
526
+ res . setHeader ( 'Content-Type' , 'text/plain' )
527
+ res . end ( 'hello, world' )
528
+ } )
529
+
530
+ request ( server )
531
+ . get ( '/' )
532
+ . set ( 'Accept-Encoding' , 'deflate, br' )
533
+ . expect ( 'Content-Encoding' , 'br' , done )
534
+ } )
535
+ } )
536
+ }
537
+
496
538
describe ( 'when "Cache-Control: no-transform" response header' , function ( ) {
497
539
it ( 'should not compress response' , function ( done ) {
498
540
var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -631,6 +673,34 @@ describe('compression()', function () {
631
673
. end ( )
632
674
} )
633
675
676
+ if ( hasBrotliSupport ) {
677
+ it ( 'should flush small chunks for brotli' , function ( done ) {
678
+ var chunks = 0
679
+ var next
680
+ var server = createServer ( { threshold : 0 } , function ( req , res ) {
681
+ next = writeAndFlush ( res , 2 , Buffer . from ( '..' ) )
682
+ res . setHeader ( 'Content-Type' , 'text/plain' )
683
+ next ( )
684
+ } )
685
+
686
+ function onchunk ( chunk ) {
687
+ assert . ok ( chunks ++ < 20 )
688
+ assert . strictEqual ( chunk . toString ( ) , '..' )
689
+ next ( )
690
+ }
691
+
692
+ request ( server )
693
+ . get ( '/' )
694
+ . set ( 'Accept-Encoding' , 'br' )
695
+ . request ( )
696
+ . on ( 'response' , unchunk ( 'br' , onchunk , function ( err ) {
697
+ if ( err ) return done ( err )
698
+ server . close ( done )
699
+ } ) )
700
+ . end ( )
701
+ } )
702
+ }
703
+
634
704
it ( 'should flush small chunks for deflate' , function ( done ) {
635
705
var chunks = 0
636
706
var next
@@ -710,6 +780,9 @@ function unchunk (encoding, onchunk, onend) {
710
780
case 'gzip' :
711
781
stream = res . pipe ( zlib . createGunzip ( ) )
712
782
break
783
+ case 'br' :
784
+ stream = res . pipe ( zlib . createBrotliDecompress ( ) )
785
+ break
713
786
}
714
787
715
788
stream . on ( 'data' , onchunk )
0 commit comments