Skip to content

Commit

Permalink
Harden stream processing close test (#4263)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil authored Jun 23, 2021
1 parent b1f6fd6 commit be3e3a2
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions test/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1190,38 +1190,45 @@ describe('transmission', () => {
await log;
});

it('stops processing the stream when the request closes', async () => {
it('stops processing the stream when the connection closes', async () => {

let stream;

const ErrStream = class extends Stream.Readable {

constructor(request) {

super();
this.request = request;
this.reads = 0;
}

_read(size) {

if (this.isDone) {
return;
if (this.reads === 0) {
this.push('here is the response');
this.request.raw.res.destroy();
}
else {
// "Inifitely" push more content

this.isDone = true;
this.push('here is the response');
process.nextTick(() => {

this.request.raw.req.emit('close');
process.nextTick(() => {

this.push(null);
this.push('.');
});
});
}

++this.reads;
}
};

const server = Hapi.server();
const log = server.events.once('response');
server.route({ method: 'GET', path: '/stream', handler: (request, h) => h.response(new ErrStream(request)).bytes(0) });
server.route({ method: 'GET', path: '/stream', handler: (request, h) => {

stream = new ErrStream(request);
return h.response(stream).bytes(0);
} });

const res = await server.inject({ url: '/stream', headers: { 'Accept-Encoding': 'gzip' } });
expect(res.statusCode).to.equal(499);
Expand All @@ -1234,6 +1241,8 @@ describe('transmission', () => {
expect(request.response.statusCode).to.equal(499);
expect(request.info.completed).to.be.above(0);
expect(request.info.responded).to.equal(0);

expect(stream.reads).to.equal(2);
});

it('does not truncate the response when stream finishes before response is done', async () => {
Expand Down

0 comments on commit be3e3a2

Please sign in to comment.