@@ -4,6 +4,7 @@ var Buffer = require('safe-buffer').Buffer
44var bytes = require ( 'bytes' )
55var crypto = require ( 'crypto' )
66var http = require ( 'http' )
7+ var http2 = require ( 'http2' )
78var request = require ( 'supertest' )
89var zlib = require ( 'zlib' )
910
@@ -305,6 +306,22 @@ describe('compression()', function () {
305306 . expect ( 200 , done )
306307 } )
307308
309+ it ( 'should work with http2 server' , function ( done ) {
310+ createHttp2Server ( { threshold : 0 } , function ( req , res ) {
311+ res . setHeader ( 'Content-Type' , 'text/plain' )
312+ res . end ( 'hello, world' )
313+ } )
314+
315+ var request = createHttp2Client ( ) . request ( {
316+ 'Accept-Encoding' : 'gzip'
317+ } )
318+ request . on ( 'response' , ( headers ) => {
319+ assert . equal ( headers [ 'content-encoding' ] , 'gzip' )
320+ done ( )
321+ } )
322+ request . end ( )
323+ } )
324+
308325 describe ( 'threshold' , function ( ) {
309326 it ( 'should not compress responses below the threshold size' , function ( done ) {
310327 var server = createServer ( { threshold : '1kb' } , function ( req , res ) {
@@ -674,6 +691,28 @@ function createServer (opts, fn) {
674691 } )
675692}
676693
694+ function createHttp2Server ( opts , fn ) {
695+ var _compression = compression ( opts )
696+ var server = http2 . createServer ( function ( req , res ) {
697+ _compression ( req , res , function ( err ) {
698+ if ( err ) {
699+ res . statusCode = err . status || 500
700+ res . end ( err . message )
701+ return
702+ }
703+
704+ fn ( req , res )
705+ } )
706+ } )
707+ server . listen ( 8443 , '127.0.0.1' )
708+ return server
709+ }
710+
711+ function createHttp2Client ( ) {
712+ var client = http2 . connect ( 'http://127.0.0.1:8443' )
713+ return client
714+ }
715+
677716function shouldHaveBodyLength ( length ) {
678717 return function ( res ) {
679718 assert . strictEqual ( res . text . length , length , 'should have body length of ' + length )
0 commit comments