forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-http-keep-alive-empty-line.mjs
More file actions
55 lines (48 loc) · 1.43 KB
/
test-http-keep-alive-empty-line.mjs
File metadata and controls
55 lines (48 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as common from '../common/index.mjs';
import assert from 'node:assert';
import { createServer } from 'node:http';
import { connect } from 'node:net';
// This test ensures that data like an empty line (`\r\n`) recevied by the
// server after a request, does not reset the keep-alive timeout. See
// https://github.com/nodejs/node/issues/58140.
const server = createServer({
connectionsCheckingInterval: 100,
headersTimeout: 100,
keepAliveTimeout: 300
}, (req, res) => {
res.writeHead(404);
res.end();
req.socket.on('close', common.mustCall(() => {
server.close();
}));
});
server.listen(0, () => {
const client = connect({
host: 'localhost',
port: server.address().port,
}, () => {
client.write(
'GET / HTTP/1.1\r\n' +
'Host: localhost:3000\r\n' +
'Content-Length: 0\r\n' +
'\r\n'
);
let response = '';
let responseReceived = false;
client.setEncoding('utf-8');
client.on('data', (chunk) => {
response += chunk;
// Check if we've received the full header (ending with \r\n\r\n)
if (response.includes('\r\n\r\n')) {
responseReceived = true;
const statusLine = response.split('\r\n')[0];
const status = statusLine.split(' ')[1];
assert.strictEqual(status, '404');
client.write('\r\n');
}
});
client.on('end', common.mustCall(() => {
assert.ok(responseReceived);
}));
});
});