Skip to content

Commit c96de7a

Browse files
committed
test: deflake test-http-keep-alive-empty-line
- Do not call `client.end()` to ensure that the socket is closed by the server. - Remove the timer and the send the empty line when the response is received. Fixes: #59577
1 parent 6fd67ec commit c96de7a

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

test/parallel/test-http-keep-alive-empty-line.mjs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import assert from 'node:assert';
33
import { createServer } from 'node:http';
44
import { connect } from 'node:net';
55

6+
// This test ensures that data like an empty line (`\r\n`) recevied by the
7+
// server after a request, does not reset the keep-alive timeout. See
8+
// https://github.com/nodejs/node/issues/58140.
9+
610
const server = createServer({
711
connectionsCheckingInterval: 100,
812
headersTimeout: 100,
@@ -28,23 +32,24 @@ server.listen(0, () => {
2832
'\r\n'
2933
);
3034

31-
setTimeout(() => {
32-
client.write('\r\n');
33-
}, 100);
34-
35-
let responseBuffer = '';
35+
let response = '';
36+
let responseReceived = false;
3637

38+
client.setEncoding('utf-8');
3739
client.on('data', (chunk) => {
38-
responseBuffer += chunk.toString();
40+
response += chunk;
3941

4042
// Check if we've received the full header (ending with \r\n\r\n)
41-
if (responseBuffer.includes('\r\n\r\n')) {
42-
const statusLine = responseBuffer.split('\r\n')[0];
43+
if (response.includes('\r\n\r\n')) {
44+
responseReceived = true;
45+
const statusLine = response.split('\r\n')[0];
4346
const status = statusLine.split(' ')[1];
4447
assert.strictEqual(status, '404');
45-
client.end();
48+
client.write('\r\n');
4649
}
4750
});
48-
client.on('end', common.mustCall());
51+
client.on('end', common.mustCall(() => {
52+
assert.ok(responseReceived);
53+
}));
4954
});
5055
});

0 commit comments

Comments
 (0)