1'use strict'; 2 3const common = require('../common'); 4 5// This test verifies that `tls.connect()` honors the `allowHalfOpen` option. 6 7if (!common.hasCrypto) 8 common.skip('missing crypto'); 9 10const assert = require('assert'); 11const fixtures = require('../common/fixtures'); 12const tls = require('tls'); 13 14{ 15 const socket = tls.connect({ port: 42, lookup() {} }); 16 assert.strictEqual(socket.allowHalfOpen, false); 17} 18 19{ 20 const socket = tls.connect({ port: 42, allowHalfOpen: false, lookup() {} }); 21 assert.strictEqual(socket.allowHalfOpen, false); 22} 23 24const server = tls.createServer({ 25 key: fixtures.readKey('agent1-key.pem'), 26 cert: fixtures.readKey('agent1-cert.pem'), 27}, common.mustCall((socket) => { 28 server.close(); 29 30 let message = ''; 31 32 socket.setEncoding('utf8'); 33 socket.on('data', (chunk) => { 34 message += chunk; 35 36 if (message === 'Hello') { 37 socket.end(message); 38 message = ''; 39 } 40 }); 41 42 socket.on('end', common.mustCall(() => { 43 assert.strictEqual(message, 'Bye'); 44 })); 45})); 46 47server.listen(0, common.mustCall(() => { 48 const socket = tls.connect({ 49 port: server.address().port, 50 rejectUnauthorized: false, 51 allowHalfOpen: true, 52 }, common.mustCall(() => { 53 let message = ''; 54 55 socket.on('data', (chunk) => { 56 message += chunk; 57 }); 58 59 socket.on('end', common.mustCall(() => { 60 assert.strictEqual(message, 'Hello'); 61 62 setTimeout(() => { 63 assert(socket.writable); 64 assert(socket.write('Bye')); 65 socket.end(); 66 }, 50); 67 })); 68 69 socket.write('Hello'); 70 })); 71 72 socket.setEncoding('utf8'); 73})); 74