@@ -126,6 +126,111 @@ describe('res.body=', () => {
126126 res . body = body
127127 assert . strictEqual ( body . listenerCount ( 'error' ) , 0 )
128128 } )
129+
130+ it ( 'should cleanup original stream when replaced by new stream' , ( ) => {
131+ const res = response ( )
132+ const stream1 = new Stream . PassThrough ( )
133+ const stream2 = new Stream . PassThrough ( )
134+
135+ res . body = stream1
136+ res . body = stream2
137+
138+ assert . strictEqual ( stream1 . destroyed , true )
139+ } )
140+
141+ it ( 'should cleanup original stream when replaced by null' , ( ) => {
142+ const res = response ( )
143+ const stream = new Stream . PassThrough ( )
144+
145+ res . body = stream
146+ res . body = null
147+
148+ assert . strictEqual ( stream . destroyed , true )
149+ } )
150+
151+ it ( 'should not throw unhandled errors when replacing failing stream' , async ( ) => {
152+ const res = response ( )
153+
154+ const stream1 = new Stream . Readable ( {
155+ read ( ) {
156+ }
157+ } )
158+
159+ const stream2 = new Stream . PassThrough ( )
160+
161+ res . body = stream1
162+ res . body = stream2
163+
164+ await new Promise ( ( resolve ) => {
165+ process . nextTick ( ( ) => {
166+ stream1 . emit ( 'error' , new Error ( 'stream1 error' ) )
167+ setTimeout ( resolve , 10 )
168+ } )
169+ } )
170+ } )
171+
172+ it ( 'should handle multiple sequential stream replacements' , ( ) => {
173+ const res = response ( )
174+ const stream1 = new Stream . PassThrough ( )
175+ const stream2 = new Stream . PassThrough ( )
176+ const stream3 = new Stream . PassThrough ( )
177+
178+ res . body = stream1
179+ res . body = stream2
180+ res . body = stream3
181+
182+ assert . strictEqual ( stream1 . destroyed , true )
183+ assert . strictEqual ( stream2 . destroyed , true )
184+ assert . strictEqual ( stream3 . destroyed , false )
185+ } )
186+
187+ it ( 'should handle four sequential stream replacements' , ( ) => {
188+ const res = response ( )
189+ const stream1 = new Stream . PassThrough ( )
190+ const stream2 = new Stream . PassThrough ( )
191+ const stream3 = new Stream . PassThrough ( )
192+ const stream4 = new Stream . PassThrough ( )
193+
194+ res . body = stream1
195+ res . body = stream2
196+ res . body = stream3
197+ res . body = stream4
198+
199+ assert . strictEqual ( stream1 . destroyed , true )
200+ assert . strictEqual ( stream2 . destroyed , true )
201+ assert . strictEqual ( stream3 . destroyed , true )
202+ assert . strictEqual ( stream4 . destroyed , false )
203+ } )
204+
205+ it ( 'should cleanup stream when replaced by string' , ( ) => {
206+ const res = response ( )
207+ const stream = new Stream . PassThrough ( )
208+
209+ res . body = stream
210+ res . body = 'hello'
211+
212+ assert . strictEqual ( stream . destroyed , true )
213+ } )
214+
215+ it ( 'should cleanup stream when replaced by buffer' , ( ) => {
216+ const res = response ( )
217+ const stream = new Stream . PassThrough ( )
218+
219+ res . body = stream
220+ res . body = Buffer . from ( 'hello' )
221+
222+ assert . strictEqual ( stream . destroyed , true )
223+ } )
224+
225+ it ( 'should cleanup stream when replaced by object' , ( ) => {
226+ const res = response ( )
227+ const stream = new Stream . PassThrough ( )
228+
229+ res . body = stream
230+ res . body = { foo : 'bar' }
231+
232+ assert . strictEqual ( stream . destroyed , true )
233+ } )
129234 } )
130235
131236 describe ( 'when a buffer is given' , ( ) => {
0 commit comments