0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-http-slow-headers-keepalive-multiple-requests.js
Alex R b149eefa82 http: fix incorrect headersTimeout measurement
For keep-alive connections, the headersTimeout may fire during
subsequent request because the measurement was reset after
a request and not before a request.

PR-URL: https://github.com/nodejs/node/pull/32329
Fixes: https://github.com/nodejs/node/issues/27363
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2020-04-02 14:50:20 +02:00

52 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
const http = require('http');
const net = require('net');
const { finished } = require('stream');
const headers =
'GET / HTTP/1.1\r\n' +
'Host: localhost\r\n' +
'Connection: keep-alive\r\n' +
'Agent: node\r\n';
const baseTimeout = 1000;
const server = http.createServer(common.mustCall((req, res) => {
req.resume();
res.writeHead(200);
res.end();
}, 2));
server.keepAliveTimeout = 10 * baseTimeout;
server.headersTimeout = baseTimeout;
server.once('timeout', common.mustNotCall((socket) => {
socket.destroy();
}));
server.listen(0, () => {
const client = net.connect(server.address().port);
// first request
client.write(headers);
client.write('\r\n');
setTimeout(() => {
// second request
client.write(headers);
// `headersTimeout` doesn't seem to fire if request
// is sent altogether.
setTimeout(() => {
client.write('\r\n');
client.end();
}, 10);
}, baseTimeout + 10);
client.resume();
finished(client, common.mustCall((err) => {
server.close();
}));
});