@@ -127,15 +127,16 @@ describe('res.body=', () => {
127127 assert . strictEqual ( body . listenerCount ( 'error' ) , 0 )
128128 } )
129129
130- it ( 'should cleanup original stream when replaced by new stream' , ( ) => {
130+ it ( 'should NOT cleanup original stream when replaced by new stream (to support wrapping middleware) ' , ( ) => {
131131 const res = response ( )
132132 const stream1 = new Stream . PassThrough ( )
133133 const stream2 = new Stream . PassThrough ( )
134134
135135 res . body = stream1
136136 res . body = stream2
137137
138- assert . strictEqual ( stream1 . destroyed , true )
138+ assert . strictEqual ( stream1 . destroyed , false )
139+ assert . strictEqual ( stream2 . destroyed , false )
139140 } )
140141
141142 it ( 'should cleanup original stream when replaced by null' , ( ) => {
@@ -179,8 +180,8 @@ describe('res.body=', () => {
179180 res . body = stream2
180181 res . body = stream3
181182
182- assert . strictEqual ( stream1 . destroyed , true )
183- assert . strictEqual ( stream2 . destroyed , true )
183+ assert . strictEqual ( stream1 . destroyed , false )
184+ assert . strictEqual ( stream2 . destroyed , false )
184185 assert . strictEqual ( stream3 . destroyed , false )
185186 } )
186187
@@ -196,9 +197,9 @@ describe('res.body=', () => {
196197 res . body = stream3
197198 res . body = stream4
198199
199- assert . strictEqual ( stream1 . destroyed , true )
200- assert . strictEqual ( stream2 . destroyed , true )
201- assert . strictEqual ( stream3 . destroyed , true )
200+ assert . strictEqual ( stream1 . destroyed , false )
201+ assert . strictEqual ( stream2 . destroyed , false )
202+ assert . strictEqual ( stream3 . destroyed , false )
202203 assert . strictEqual ( stream4 . destroyed , false )
203204 } )
204205
@@ -231,6 +232,32 @@ describe('res.body=', () => {
231232
232233 assert . strictEqual ( stream . destroyed , true )
233234 } )
235+
236+ it ( 'should support wrapping stream middleware (like koa-compress)' , async ( ) => {
237+ const res = response ( )
238+ const sourceStream = new Stream . Readable ( {
239+ read ( ) {
240+ this . push ( 'hello world' )
241+ this . push ( null )
242+ }
243+ } )
244+
245+ res . body = sourceStream
246+
247+ const wrappedStream = new Stream . PassThrough ( )
248+ sourceStream . pipe ( wrappedStream )
249+ res . body = wrappedStream
250+
251+ assert . strictEqual ( sourceStream . destroyed , false )
252+ assert . strictEqual ( wrappedStream . destroyed , false )
253+
254+ const chunks = [ ]
255+ for await ( const chunk of wrappedStream ) {
256+ chunks . push ( chunk )
257+ }
258+ const result = Buffer . concat ( chunks ) . toString ( )
259+ assert . strictEqual ( result , 'hello world' )
260+ } )
234261 } )
235262
236263 describe ( 'when a buffer is given' , ( ) => {
0 commit comments