0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-http-server-request-timeout-keepalive.js
wwwzbwcom ed3604cd64 http: server check Host header, to meet RFC 7230 5.4 requirement
PR-URL: https://github.com/nodejs/node/pull/45597
Fixes: https://github.com/nodejs/node/issues/39033
Co-authored-by: Luigi Pinca <luigipinca@gmail.com>
Co-authored-by: mscdex <mscdex@users.noreply.github.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2022-11-28 18:35:31 +01:00

98 lines
2.5 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { createServer } = require('http');
const { connect } = require('net');
// This test validates that the server returns 408
// after server.requestTimeout if the client
// does not complete a request, and that keep alive
// works properly.
function performRequestWithDelay(client, firstDelay, secondDelay, closeAfter) {
client.resume();
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n');
setTimeout(() => {
client.write('Connection: ');
}, firstDelay).unref();
// Complete the request
setTimeout(() => {
client.write(`${closeAfter ? 'close' : 'keep-alive'}\r\n\r\n`);
}, firstDelay + secondDelay).unref();
}
const requestTimeout = common.platformTimeout(5000);
const server = createServer({
headersTimeout: 0,
requestTimeout,
keepAliveTimeout: 0,
connectionsCheckingInterval: requestTimeout / 4
}, common.mustCallAtLeast((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end();
}));
assert.strictEqual(server.requestTimeout, requestTimeout);
// Make sure keepAliveTimeout is big enough for the requestTimeout.
server.keepAliveTimeout = 0;
server.listen(0, common.mustCall(() => {
const client = connect(server.address().port);
let second = false;
let response = '';
client.setEncoding('utf8');
client.on('data', common.mustCallAtLeast((chunk) => {
response += chunk;
// First response has ended
if (!second && response.endsWith('\r\n\r\n')) {
assert.strictEqual(
response.split('\r\n')[0],
'HTTP/1.1 200 OK'
);
const defer = requestTimeout * 1.5;
// Wait some time to make sure requestTimeout
// does not interfere with keep alive
setTimeout(() => {
response = '';
second = true;
// Perform a second request expected to finish after requestTimeout
performRequestWithDelay(
client,
requestTimeout / 5,
requestTimeout * 2,
true
);
}, defer).unref();
}
}, 1));
const errOrEnd = common.mustCall(function(err) {
assert.strictEqual(second, true);
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);
// Perform a first request which is completed immediately
performRequestWithDelay(
client,
requestTimeout / 5,
requestTimeout / 5,
false
);
}));