Skip to content

Commit

Permalink
Fix cleanup to be consistent across node releases
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Aug 25, 2020
1 parent 919acb6 commit 5aa1573
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
13 changes: 4 additions & 9 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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() {

}
};


Expand Down
4 changes: 0 additions & 4 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ exports = module.exports = internals.Response = class extends Http.ServerRespons
this.emit('finish');
}

destroy() {

}

addTrailers(trailers) {

for (const key in trailers) {
Expand Down
26 changes: 26 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,32 @@ describe('_read()', () => {
expect(res.payload).to.equal(body);
});

it('supports async iteration', async () => {

const dispatch = async function (req, res) {

req.on('close', () => console.log('close'))
let buffer = '';
try {
for await (const chunk of req) {
console.log('chunk')
buffer = buffer + chunk.toString();
}
} catch (err) {
console.error(err)
}
console.log('done')

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) {
Expand Down

0 comments on commit 5aa1573

Please sign in to comment.