node/test/parallel/test-http-keep-alive-timeout.js
Robert Nagy 849d9e7b90 http: provide keep-alive timeout response header
In http 1.1 persistent connection protocol there is a
timing race where the client sends the request and then
the server kills the connection (due to inactivity)
before receiving the client's request.

By providing a keep-alive header it is possible to provide
the client a hint of when idle timeout would occur and
avoid the race.

Fixes: https://github.com/nodejs/node/issues/34560

PR-URL: https://github.com/nodejs/node/pull/34561
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Pranshu Srivastava <rexagod@gmail.com>
2020-08-01 15:04:35 +02:00

29 lines
714 B
JavaScript

'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');
const server = http.createServer(common.mustCall((req, res) => {
const body = 'hello world\n';
res.writeHead(200, { 'Content-Length': body.length });
res.write(body);
res.end();
}));
server.keepAliveTimeout = 12000;
const agent = new http.Agent({ maxSockets: 1, keepAlive: true });
server.listen(0, common.mustCall(function() {
http.get({
path: '/', port: this.address().port, agent: agent
}, common.mustCall((response) => {
response.resume();
assert.strictEqual(
response.headers['keep-alive'], 'timeout=12');
server.close();
agent.destroy();
}));
}));