Skip to content

Commit be3e3a2

Browse files
authored
Harden stream processing close test (hapijs#4263)
1 parent b1f6fd6 commit be3e3a2

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

test/transmit.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,38 +1190,45 @@ describe('transmission', () => {
11901190
await log;
11911191
});
11921192

1193-
it('stops processing the stream when the request closes', async () => {
1193+
it('stops processing the stream when the connection closes', async () => {
1194+
1195+
let stream;
11941196

11951197
const ErrStream = class extends Stream.Readable {
11961198

11971199
constructor(request) {
11981200

11991201
super();
12001202
this.request = request;
1203+
this.reads = 0;
12011204
}
12021205

12031206
_read(size) {
12041207

1205-
if (this.isDone) {
1206-
return;
1208+
if (this.reads === 0) {
1209+
this.push('here is the response');
1210+
this.request.raw.res.destroy();
12071211
}
1212+
else {
1213+
// "Inifitely" push more content
12081214

1209-
this.isDone = true;
1210-
this.push('here is the response');
1211-
process.nextTick(() => {
1212-
1213-
this.request.raw.req.emit('close');
12141215
process.nextTick(() => {
12151216

1216-
this.push(null);
1217+
this.push('.');
12171218
});
1218-
});
1219+
}
1220+
1221+
++this.reads;
12191222
}
12201223
};
12211224

12221225
const server = Hapi.server();
12231226
const log = server.events.once('response');
1224-
server.route({ method: 'GET', path: '/stream', handler: (request, h) => h.response(new ErrStream(request)).bytes(0) });
1227+
server.route({ method: 'GET', path: '/stream', handler: (request, h) => {
1228+
1229+
stream = new ErrStream(request);
1230+
return h.response(stream).bytes(0);
1231+
} });
12251232

12261233
const res = await server.inject({ url: '/stream', headers: { 'Accept-Encoding': 'gzip' } });
12271234
expect(res.statusCode).to.equal(499);
@@ -1234,6 +1241,8 @@ describe('transmission', () => {
12341241
expect(request.response.statusCode).to.equal(499);
12351242
expect(request.info.completed).to.be.above(0);
12361243
expect(request.info.responded).to.equal(0);
1244+
1245+
expect(stream.reads).to.equal(2);
12371246
});
12381247

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

0 commit comments

Comments
 (0)