Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harden stream processing close test #4263

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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