@@ -5,6 +5,7 @@ const test = t.test
5
5
const zlib = require ( 'zlib' )
6
6
const fs = require ( 'fs' )
7
7
const JSONStream = require ( 'jsonstream' )
8
+ const { Readable, Writable } = require ( 'stream' )
8
9
const createReadStream = fs . createReadStream
9
10
const readFileSync = fs . readFileSync
10
11
const Fastify = require ( 'fastify' )
@@ -1692,3 +1693,45 @@ test('Should send data gzipped according to zlibOptions', t => {
1692
1693
t . same ( res . rawPayload , zlib . gzipSync ( fileBuffer , zlibOptions ) )
1693
1694
} )
1694
1695
} )
1696
+
1697
+ test ( 'stream onEnd handler should log an error if exists' , t => {
1698
+ t . plan ( 1 )
1699
+
1700
+ let actual = null
1701
+ const logger = new Writable ( {
1702
+ write ( chunk , encoding , callback ) {
1703
+ actual = JSON . parse ( chunk . toString ( ) )
1704
+ }
1705
+ } )
1706
+
1707
+ const fastify = Fastify ( {
1708
+ global : false ,
1709
+ logger : {
1710
+ level : 'error' ,
1711
+ stream : logger
1712
+ }
1713
+ } )
1714
+
1715
+ fastify . register ( compressPlugin )
1716
+
1717
+ const expect = new Error ( 'something wrong' )
1718
+
1719
+ fastify . get ( '/' , ( req , reply ) => {
1720
+ const stream = new Readable ( {
1721
+ read ( size ) {
1722
+ this . destroy ( expect )
1723
+ }
1724
+ } )
1725
+ reply . type ( 'text/plain' ) . compress ( stream )
1726
+ } )
1727
+
1728
+ fastify . inject ( {
1729
+ url : '/' ,
1730
+ method : 'GET' ,
1731
+ headers : {
1732
+ 'accept-encoding' : 'gzip'
1733
+ }
1734
+ } , ( _ , res ) => {
1735
+ t . equal ( actual . msg , expect . message )
1736
+ } )
1737
+ } )
0 commit comments