node/test/parallel/test-http-server-close-all.js
Anna Henningsen febb5390aa
test: remove unnecessary .toString() calls in HTTP tests
Let’s not have bad examples in our test suite and instead use the
proper way of converting stream data to UTF-8
(i.e. `stream.setEncoding('utf8')`) in all places.

PR-URL: https://github.com/nodejs/node/pull/43731
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-07-11 15:40:08 +01:00

62 lines
1.6 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { createServer } = require('http');
const { connect } = require('net');
let connections = 0;
const server = createServer(common.mustCall(function(req, res) {
res.writeHead(200, { Connection: 'keep-alive' });
res.end();
}), {
headersTimeout: 0,
keepAliveTimeout: 0,
requestTimeout: common.platformTimeout(60000),
});
server.on('connection', function() {
connections++;
});
server.listen(0, function() {
const port = server.address().port;
// Create a first request but never finish it
const client1 = connect(port);
client1.on('connect', common.mustCall(() => {
// Create a second request, let it finish but leave the connection opened using HTTP keep-alive
const client2 = connect(port);
let response = '';
client2.setEncoding('utf8');
client2.on('data', common.mustCall((chunk) => {
response += chunk;
if (response.endsWith('0\r\n\r\n')) {
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));
assert.strictEqual(connections, 2);
server.closeAllConnections();
server.close(common.mustCall());
// This timer should never go off as the server.close should shut everything down
setTimeout(common.mustNotCall(), common.platformTimeout(1500)).unref();
}
}));
client2.on('close', common.mustCall());
client2.write('GET / HTTP/1.1\r\n\r\n');
}));
client1.on('close', common.mustCall());
client1.on('error', () => {});
client1.write('GET / HTTP/1.1');
});