@@ -5,6 +5,7 @@ const test = t.test
55const brotli = require ( 'iltorb' )
66const zlib = require ( 'zlib' )
77const fs = require ( 'fs' )
8+ const JSONStream = require ( 'jsonstream' )
89const createReadStream = fs . createReadStream
910const readFileSync = fs . readFileSync
1011const Fastify = require ( 'fastify' )
@@ -639,3 +640,55 @@ test('identity header (hook)', t => {
639640 t . deepEqual ( { hello : 'world' } , payload )
640641 } )
641642} )
643+
644+ test ( 'should support stream1 (reply compress)' , t => {
645+ t . plan ( 3 )
646+ const fastify = Fastify ( )
647+ fastify . register ( compressPlugin , { global : false } )
648+
649+ fastify . get ( '/' , ( req , reply ) => {
650+ const stream = JSONStream . stringify ( )
651+ reply . type ( 'text/plain' ) . compress ( stream )
652+ stream . write ( { hello : 'world' } )
653+ stream . end ( { a : 42 } )
654+ } )
655+
656+ fastify . inject ( {
657+ url : '/' ,
658+ method : 'GET' ,
659+ headers : {
660+ 'accept-encoding' : 'gzip'
661+ }
662+ } , ( err , res ) => {
663+ t . error ( err )
664+ t . strictEqual ( res . headers [ 'content-encoding' ] , 'gzip' )
665+ const payload = zlib . gunzipSync ( res . rawPayload )
666+ t . deepEqual ( JSON . parse ( payload . toString ( ) ) , [ { hello : 'world' } , { a : 42 } ] )
667+ } )
668+ } )
669+
670+ test ( 'should support stream1 (global hook)' , t => {
671+ t . plan ( 3 )
672+ const fastify = Fastify ( )
673+ fastify . register ( compressPlugin , { threshold : 0 } )
674+
675+ fastify . get ( '/' , ( req , reply ) => {
676+ const stream = JSONStream . stringify ( )
677+ reply . type ( 'text/plain' ) . send ( stream )
678+ stream . write ( { hello : 'world' } )
679+ stream . end ( { a : 42 } )
680+ } )
681+
682+ fastify . inject ( {
683+ url : '/' ,
684+ method : 'GET' ,
685+ headers : {
686+ 'accept-encoding' : 'gzip'
687+ }
688+ } , ( err , res ) => {
689+ t . error ( err )
690+ t . strictEqual ( res . headers [ 'content-encoding' ] , 'gzip' )
691+ const payload = zlib . gunzipSync ( res . rawPayload )
692+ t . deepEqual ( JSON . parse ( payload . toString ( ) ) , [ { hello : 'world' } , { a : 42 } ] )
693+ } )
694+ } )
0 commit comments