@@ -9,6 +9,12 @@ var zlib = require('zlib')
99
1010var compression = require ( '..' )
1111
12+ /**
13+ * @const
14+ * whether current node version has brotli support
15+ */
16+ var hasBrotliSupport = process . versions ? 'brotli' in process . versions : false
17+
1218describe ( 'compression()' , function ( ) {
1319 it ( 'should skip HEAD' , function ( done ) {
1420 var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -465,6 +471,20 @@ describe('compression()', function () {
465471 } )
466472 } )
467473
474+ describe ( 'when "Accept-Encoding: br"' , function ( ) {
475+ ( hasBrotliSupport ? it . skip : 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+
468488 describe ( 'when "Accept-Encoding: gzip, deflate"' , function ( ) {
469489 it ( 'should respond with gzip' , function ( done ) {
470490 var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -493,6 +513,20 @@ describe('compression()', function () {
493513 } )
494514 } )
495515
516+ describe ( 'when "Accept-Encoding: deflate, br"' , function ( ) {
517+ ( hasBrotliSupport ? it . skip : 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+
496530 describe ( 'when "Cache-Control: no-transform" response header' , function ( ) {
497531 it ( 'should not compress response' , function ( done ) {
498532 var server = createServer ( { threshold : 0 } , function ( req , res ) {
@@ -631,6 +665,32 @@ describe('compression()', function () {
631665 . end ( )
632666 } )
633667
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+
634694 it ( 'should flush small chunks for deflate' , function ( done ) {
635695 var chunks = 0
636696 var next
@@ -710,6 +770,9 @@ function unchunk (encoding, onchunk, onend) {
710770 case 'gzip' :
711771 stream = res . pipe ( zlib . createGunzip ( ) )
712772 break
773+ case 'br' :
774+ stream = res . pipe ( zlib . createBrotliDecompress ( ) )
775+ break
713776 }
714777
715778 stream . on ( 'data' , onchunk )
0 commit comments