0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-tls-connect-allow-half-open-option.js
Luigi Pinca c3b8e50143 tls: allow client-side sockets to be half-opened
Make `tls.connect()` support an `allowHalfOpen` option which specifies
whether or not to allow the connection to be half-opened when the
`socket` option is not specified.

PR-URL: https://github.com/nodejs/node/pull/27836
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ouyang Yadong <oyydoibh@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
2019-08-17 06:46:31 +02:00

74 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
// This test verifies that `tls.connect()` honors the `allowHalfOpen` option.
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const fixtures = require('../common/fixtures');
const tls = require('tls');
{
const socket = tls.connect({ lookup() {} });
assert.strictEqual(socket.allowHalfOpen, false);
}
{
const socket = tls.connect({ allowHalfOpen: false, lookup() {} });
assert.strictEqual(socket.allowHalfOpen, false);
}
const server = tls.createServer({
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem'),
}, common.mustCall((socket) => {
server.close();
let message = '';
socket.setEncoding('utf8');
socket.on('data', (chunk) => {
message += chunk;
if (message === 'Hello') {
socket.end(message);
message = '';
}
});
socket.on('end', common.mustCall(() => {
assert.strictEqual(message, 'Bye');
}));
}));
server.listen(0, common.mustCall(() => {
const socket = tls.connect({
port: server.address().port,
rejectUnauthorized: false,
allowHalfOpen: true,
}, common.mustCall(() => {
let message = '';
socket.on('data', (chunk) => {
message += chunk;
});
socket.on('end', common.mustCall(() => {
assert.strictEqual(message, 'Hello');
setTimeout(() => {
assert(socket.writable);
assert(socket.write('Bye'));
socket.end();
}, 50);
}));
socket.write('Hello');
}));
socket.setEncoding('utf8');
}));