|
| 1 | +'use strict' |
| 2 | +import * as tls from 'tls' |
| 3 | +import * as net from 'net' |
| 4 | + |
| 5 | +describe('tls option', () => { |
| 6 | + describe('Standalone', () => { |
| 7 | + it('supports tls', (done) => { |
| 8 | + let redis |
| 9 | + |
| 10 | + stub(tls, 'connect', (op) => { |
| 11 | + expect(op.ca).to.eql('123') |
| 12 | + expect(op.port).to.eql(6379) |
| 13 | + const stream = net.createConnection(op) |
| 14 | + stream.on('connect', data => { |
| 15 | + stream.emit('secureConnect', data) |
| 16 | + }) |
| 17 | + return stream |
| 18 | + }) |
| 19 | + |
| 20 | + redis = new Redis({tls: {ca: '123'}}) |
| 21 | + redis.on('ready', () => { |
| 22 | + redis.disconnect() |
| 23 | + tls.connect.restore() |
| 24 | + redis.on('end', () => done()) |
| 25 | + }) |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + describe('Sentinel', () => { |
| 30 | + it('does not use tls option by default', (done) => { |
| 31 | + new MockServer(27379, function (argv) { |
| 32 | + if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') { |
| 33 | + return ['127.0.0.1', '6379'] |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + stub(tls, 'connect', () => { |
| 38 | + throw new Error('called') |
| 39 | + }) |
| 40 | + |
| 41 | + const redis = new Redis({sentinels: [{port: 27379}], name: 'my', tls: {ca: '123'}}) |
| 42 | + redis.on('ready', () => { |
| 43 | + redis.disconnect() |
| 44 | + tls.connect.restore() |
| 45 | + done() |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + it('can be enabled by `enableTLSForSentinelMode`', (done) => { |
| 50 | + new MockServer(27379, function (argv) { |
| 51 | + if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') { |
| 52 | + return ['127.0.0.1', '6379'] |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + let redis |
| 57 | + |
| 58 | + stub(tls, 'connect', (op) => { |
| 59 | + expect(op.ca).to.eql('123') |
| 60 | + redis.disconnect() |
| 61 | + tls.connect.restore() |
| 62 | + process.nextTick(done) |
| 63 | + return tls.connect(op) |
| 64 | + }) |
| 65 | + |
| 66 | + redis = new Redis({sentinels: [{port: 27379}], name: 'my', tls: {ca: '123'}, enableTLSForSentinelMode: true}) |
| 67 | + }) |
| 68 | + |
| 69 | + it('supports sentinelTLS', (done) => { |
| 70 | + new MockServer(27379, function (argv) { |
| 71 | + if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') { |
| 72 | + return ['127.0.0.1', '6379'] |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + let redis |
| 77 | + |
| 78 | + stub(tls, 'connect', (op) => { |
| 79 | + expect(op.ca).to.eql('123') |
| 80 | + expect(op.port).to.eql(27379) |
| 81 | + const stream = net.createConnection(op) |
| 82 | + stream.on('connect', data => { |
| 83 | + stream.emit('secureConnect', data) |
| 84 | + }) |
| 85 | + return stream |
| 86 | + }) |
| 87 | + |
| 88 | + redis = new Redis({sentinels: [{port: 27379}], name: 'my', sentinelTLS: {ca: '123'}}) |
| 89 | + redis.on('ready', () => { |
| 90 | + redis.disconnect() |
| 91 | + tls.connect.restore() |
| 92 | + redis.on('end', () => done()) |
| 93 | + }) |
| 94 | + }) |
| 95 | + }) |
| 96 | +}) |
0 commit comments