From 74dc0036870f0e976f634c9abbd14e44035d8dd6 Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Tue, 25 Aug 2020 16:22:28 +0200 Subject: [PATCH] Fix cleanup to be consistent across node releases --- lib/request.js | 13 ++++--------- lib/response.js | 4 ---- test/index.js | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/request.js b/lib/request.js index e67c151..05d9e33 100755 --- a/lib/request.js +++ b/lib/request.js @@ -13,7 +13,10 @@ exports = module.exports = internals.Request = class extends Stream.Readable { constructor(options) { - super(); + super({ + emitClose: !!(options.simulate && options.simulate.close), + autoDestroy: true // This is the default in node 14+ + }); // options: method, url, payload, headers, remoteAddress @@ -136,19 +139,11 @@ exports = module.exports = internals.Request = class extends Stream.Readable { this.emit('error', new Error('Simulated')); } - if (this._shot.simulate.close) { - this.emit('close'); - } - if (this._shot.simulate.end !== false) { // 'end' defaults to true this.push(null); } }); } - - destroy() { - - } }; diff --git a/lib/response.js b/lib/response.js index 9678e23..5c97956 100755 --- a/lib/response.js +++ b/lib/response.js @@ -82,10 +82,6 @@ exports = module.exports = internals.Response = class extends Http.ServerRespons this.emit('finish'); } - destroy() { - - } - addTrailers(trailers) { for (const key in trailers) { diff --git a/test/index.js b/test/index.js index 3393523..f2a8724 100755 --- a/test/index.js +++ b/test/index.js @@ -562,6 +562,25 @@ describe('_read()', () => { expect(res.payload).to.equal(body); }); + it('supports async iteration', async () => { + + const dispatch = async function (req, res) { + + let buffer = ''; + for await (const chunk of req) { + buffer = buffer + chunk.toString(); + } + + res.writeHead(200, { 'Content-Length': 0 }); + res.end(buffer); + req.destroy(); + }; + + const body = 'something special just for you'; + const res = await Shot.inject(dispatch, { method: 'get', url: '/', payload: body }); + expect(res.payload).to.equal(body); + }); + it('simulates split', async () => { const dispatch = function (req, res) {