1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4 5if (!common.hasCrypto) { 6 common.skip('missing crypto'); 7} 8 9const { createServer } = require('https'); 10const { connect } = require('tls'); 11 12const fixtures = require('../common/fixtures'); 13 14const options = { 15 key: fixtures.readKey('agent1-key.pem'), 16 cert: fixtures.readKey('agent1-cert.pem') 17}; 18 19let connections = 0; 20 21const server = createServer(options, common.mustCall(function(req, res) { 22 res.writeHead(200, { Connection: 'keep-alive' }); 23 res.end(); 24}), { 25 headersTimeout: 0, 26 keepAliveTimeout: 0, 27 requestTimeout: common.platformTimeout(60000), 28}); 29 30server.on('connection', function() { 31 connections++; 32}); 33 34server.listen(0, function() { 35 const port = server.address().port; 36 37 // Create a first request but never finish it 38 const client1 = connect({ port, rejectUnauthorized: false }); 39 40 client1.on('connect', common.mustCall(() => { 41 // Create a second request, let it finish but leave the connection opened using HTTP keep-alive 42 const client2 = connect({ port, rejectUnauthorized: false }); 43 let response = ''; 44 45 client2.setEncoding('utf8'); 46 client2.on('data', common.mustCall((chunk) => { 47 response += chunk; 48 49 if (response.endsWith('0\r\n\r\n')) { 50 assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive')); 51 assert.strictEqual(connections, 2); 52 53 server.closeAllConnections(); 54 server.close(common.mustCall()); 55 56 // This timer should never go off as the server.close should shut everything down 57 setTimeout(common.mustNotCall(), common.platformTimeout(1500)).unref(); 58 } 59 })); 60 61 client2.on('close', common.mustCall()); 62 63 client2.write('GET / HTTP/1.1\r\n\r\n'); 64 })); 65 66 client1.on('close', common.mustCall()); 67 68 client1.on('error', () => {}); 69 70 client1.write('GET / HTTP/1.1'); 71}); 72