Skip to content

Commit

Permalink
test: fix invalid common.mustSucceed() usage
Browse files Browse the repository at this point in the history
By its own nature, the function returned by `common.mustSucceed()`
cannot be used as a listener for `'error'` events.

Write errors like `read ECONNRESET` or `write EPIPE`, should be ignored
because the socket might be closed by the other peer while the request
is sent.

Refs: 3caa2c1a005652fdb3e8
  • Loading branch information
lpinca committed Jan 25, 2025
1 parent 1760024 commit ec5b67f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ server.listen(0, common.mustCall(() => {
response += chunk;
}));

const errOrEnd = common.mustSucceed(function(err) {
client.on('error', () => {
// Ignore errors like 'write EPIPE' that might occur while the request is
// sent.
});

client.on('close', common.mustCall(() => {
assert.strictEqual(
response,
'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n'
);
server.close();
});

client.on('end', errOrEnd);
client.on('error', errOrEnd);
}));

client.resume();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ server.listen(0, common.mustCall(() => {
response += chunk;
}));

const errOrEnd = common.mustSucceed(function(err) {
client.on('error', () => {
// Ignore errors like 'write EPIPE' that might occur while the request is
// sent.
});

client.on('close', common.mustCall(() => {
assert.strictEqual(
response,
'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n'
);
server.close();
});

client.on('end', errOrEnd);
client.on('error', errOrEnd);
}));

client.resume();
client.write('GET / HTTP/1.1\r\n');
Expand Down
24 changes: 13 additions & 11 deletions test/parallel/test-http-server-request-timeout-delayed-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ server.listen(0, common.mustCall(() => {
response += chunk;
}));

client.on('error', () => {
// Ignore errors like 'write EPIPE' that might occur while the request is
// sent.
});

client.on('close', common.mustCall(() => {
assert.strictEqual(
response,
'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n'
);
server.close();
}));

client.resume();
client.write('POST / HTTP/1.1\r\n');
client.write('Host: example.com\r\n');
Expand All @@ -57,15 +70,4 @@ server.listen(0, common.mustCall(() => {
client.write('12345678901234567890\r\n\r\n');
}, common.platformTimeout(requestTimeout * 2)).unref();
});

const errOrEnd = common.mustSucceed(function(err) {
assert.strictEqual(
response,
'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n'
);
server.close();
});

client.on('end', errOrEnd);
client.on('error', errOrEnd);
}));
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ server.listen(0, common.mustCall(() => {
response += chunk;
}));

const errOrEnd = common.mustSucceed(function(err) {
client.on('error', () => {
// Ignore errors like 'write EPIPE' that might occur while the request is
// sent.
});

client.on('close', common.mustCall(() => {
assert.strictEqual(
response,
'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n'
);
server.close();
});

client.on('end', errOrEnd);
client.on('error', errOrEnd);
}));

client.resume();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ server.listen(0, common.mustCall(() => {
response += chunk;
}));

const errOrEnd = common.mustSucceed(function(err) {
client.on('error', () => {
// Ignore errors like 'write EPIPE' that might occur while the request is
// sent.
});

client.on('close', common.mustCall(() => {
assert.strictEqual(
response,
'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n'
);
server.close();
});

client.on('error', errOrEnd);
client.on('end', errOrEnd);
}));

client.resume();
client.write('POST / HTTP/1.1\r\n');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ server.listen(0, common.mustCall(() => {
response += chunk;
}));

const errOrEnd = common.mustSucceed(function(err) {
client.on('error', () => {
// Ignore errors like 'write EPIPE' that might occur while the request is
// sent.
});

client.on('close', common.mustCall(() => {
assert.strictEqual(
response,
'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n'
);
server.close();
});

client.on('end', errOrEnd);
client.on('error', errOrEnd);
}));

client.resume();
client.write('GET / HTTP/1.1\r\n');
Expand Down

0 comments on commit ec5b67f

Please sign in to comment.