test: fix invalid common.mustSucceed() usage

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: https://github.com/nodejs/node/commit/3caa2c1a005652fdb3e8
PR-URL: https://github.com/nodejs/node/pull/56756
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Luigi Pinca
2025-01-27 11:09:37 +01:00
committed by GitHub
parent 1263efdfca
commit 04f147b024
6 changed files with 48 additions and 36 deletions

View File

@@ -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();

View File

@@ -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');

View File

@@ -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');
@@ -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);
}));

View File

@@ -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();

View File

@@ -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');

View File

@@ -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');