@@ -195,16 +195,21 @@ test('should support quality syntax', t => {
195
195
} )
196
196
} )
197
197
198
- test ( 'onSend hook should not double-compress Stream if already zipped ' , t => {
199
- t . plan ( 3 )
198
+ test ( 'onSend hook should not double-compress Stream if already gzipped ' , t => {
199
+ t . plan ( 4 )
200
200
const fastify = Fastify ( )
201
- fastify . register ( compressPlugin , { global : true } )
201
+ fastify . register ( compressPlugin , {
202
+ global : true ,
203
+ threshold : 0
204
+ } )
202
205
206
+ const file = readFileSync ( './package.json' , 'utf8' )
203
207
fastify . get ( '/' , ( req , reply ) => {
204
- reply . type ( 'text/plain' ) . compress (
205
- createReadStream ( './package.json' )
206
- . pipe ( zlib . createGzip ( ) )
207
- )
208
+ const payload = zlib . gzipSync ( file )
209
+ reply . type ( 'application/json' )
210
+ . header ( 'content-encoding' , 'gzip' )
211
+ . header ( 'content-length' , payload . length )
212
+ . send ( payload )
208
213
} )
209
214
210
215
fastify . inject ( {
@@ -216,12 +221,44 @@ test('onSend hook should not double-compress Stream if already zipped', t => {
216
221
} , ( err , res ) => {
217
222
t . error ( err )
218
223
t . equal ( res . headers [ 'content-encoding' ] , 'gzip' )
219
- const file = readFileSync ( './package.json' , 'utf8' )
224
+ t . equal ( res . headers [ 'content-length' ] , res . rawPayload . length )
220
225
const payload = zlib . gunzipSync ( res . rawPayload )
221
226
t . equal ( payload . toString ( 'utf-8' ) , file )
222
227
} )
223
228
} )
224
229
230
+ test ( 'onSend hook should not double-compress Stream if already brotli compressed' , t => {
231
+ t . plan ( 4 )
232
+ const fastify = Fastify ( )
233
+ fastify . register ( compressPlugin , {
234
+ global : true ,
235
+ threshold : 0
236
+ } )
237
+
238
+ const file = readFileSync ( './package.json' , 'utf8' )
239
+ fastify . get ( '/' , ( req , reply ) => {
240
+ const payload = zlib . brotliCompressSync ( file )
241
+ reply . type ( 'application/json' )
242
+ . header ( 'content-encoding' , 'br' )
243
+ . header ( 'content-length' , payload . length )
244
+ . send ( payload )
245
+ } )
246
+
247
+ fastify . inject ( {
248
+ url : '/' ,
249
+ method : 'GET' ,
250
+ headers : {
251
+ 'accept-encoding' : 'br,gzip,deflate'
252
+ }
253
+ } , ( err , res ) => {
254
+ t . error ( err )
255
+ t . equal ( res . headers [ 'content-encoding' ] , 'br' )
256
+ t . equal ( res . headers [ 'content-length' ] , res . rawPayload . length )
257
+ const payload = zlib . brotliDecompressSync ( res . rawPayload )
258
+ t . equal ( payload . toString ( 'utf-8' ) , file )
259
+ } )
260
+ } )
261
+
225
262
test ( 'should send a gzipped data for * header' , t => {
226
263
t . plan ( 3 )
227
264
const fastify = Fastify ( )
@@ -1732,6 +1769,7 @@ test('stream onEnd handler should log an error if exists', t => {
1732
1769
const logger = new Writable ( {
1733
1770
write ( chunk , encoding , callback ) {
1734
1771
actual = JSON . parse ( chunk . toString ( ) )
1772
+ callback ( )
1735
1773
}
1736
1774
} )
1737
1775
0 commit comments