UNPKG

5.74 kBJavaScriptView Raw
1'use strict'
2
3var fs = require('fs')
4var path = require('path')
5var mqtt = require('../')
6
7describe('mqtt', function () {
8 describe('#connect', function () {
9 var sslOpts, sslOpts2
10 it('should return an MqttClient when connect is called with mqtt:/ url', function () {
11 var c = mqtt.connect('mqtt://localhost:1883')
12
13 c.should.be.instanceOf(mqtt.MqttClient)
14 })
15
16 it('should throw an error when called with no protocol specified', function () {
17 (function () {
18 mqtt.connect('foo.bar.com')
19 }).should.throw('Missing protocol')
20 })
21
22 it('should throw an error when called with no protocol specified - with options', function () {
23 (function () {
24 mqtt.connect('tcp://foo.bar.com', { protocol: null })
25 }).should.throw('Missing protocol')
26 })
27
28 it('should return an MqttClient with username option set', function () {
29 var c = mqtt.connect('mqtt://user:pass@localhost:1883')
30
31 c.should.be.instanceOf(mqtt.MqttClient)
32 c.options.should.have.property('username', 'user')
33 c.options.should.have.property('password', 'pass')
34 })
35
36 it('should return an MqttClient with username and password options set', function () {
37 var c = mqtt.connect('mqtt://user@localhost:1883')
38
39 c.should.be.instanceOf(mqtt.MqttClient)
40 c.options.should.have.property('username', 'user')
41 })
42
43 it('should return an MqttClient with the clientid option set', function () {
44 var c = mqtt.connect('mqtt://user@localhost:1883?clientId=123')
45
46 c.should.be.instanceOf(mqtt.MqttClient)
47 c.options.should.have.property('clientId', '123')
48 })
49
50 it('should return an MqttClient when connect is called with tcp:/ url', function () {
51 var c = mqtt.connect('tcp://localhost')
52
53 c.should.be.instanceOf(mqtt.MqttClient)
54 })
55
56 it('should return an MqttClient with correct host when called with a host and port', function () {
57 var c = mqtt.connect('tcp://user:pass@localhost:1883')
58
59 c.options.should.have.property('hostname', 'localhost')
60 c.options.should.have.property('port', '1883')
61 })
62
63 sslOpts = {
64 keyPath: path.join(__dirname, 'helpers', 'private-key.pem'),
65 certPath: path.join(__dirname, 'helpers', 'public-cert.pem'),
66 caPaths: [path.join(__dirname, 'helpers', 'public-cert.pem')]
67 }
68
69 it('should return an MqttClient when connect is called with mqtts:/ url', function () {
70 var c = mqtt.connect('mqtts://localhost', sslOpts)
71
72 c.options.should.have.property('protocol', 'mqtts')
73
74 c.on('error', function () {})
75
76 c.should.be.instanceOf(mqtt.MqttClient)
77 })
78
79 it('should return an MqttClient when connect is called with ssl:/ url', function () {
80 var c = mqtt.connect('ssl://localhost', sslOpts)
81
82 c.options.should.have.property('protocol', 'ssl')
83
84 c.on('error', function () {})
85
86 c.should.be.instanceOf(mqtt.MqttClient)
87 })
88
89 it('should return an MqttClient when connect is called with ws:/ url', function () {
90 var c = mqtt.connect('ws://localhost', sslOpts)
91
92 c.options.should.have.property('protocol', 'ws')
93
94 c.on('error', function () {})
95
96 c.should.be.instanceOf(mqtt.MqttClient)
97 })
98
99 it('should return an MqttClient when connect is called with wss:/ url', function () {
100 var c = mqtt.connect('wss://localhost', sslOpts)
101
102 c.options.should.have.property('protocol', 'wss')
103
104 c.on('error', function () {})
105
106 c.should.be.instanceOf(mqtt.MqttClient)
107 })
108
109 sslOpts2 = {
110 key: fs.readFileSync(path.join(__dirname, 'helpers', 'private-key.pem')),
111 cert: fs.readFileSync(path.join(__dirname, 'helpers', 'public-cert.pem')),
112 ca: [fs.readFileSync(path.join(__dirname, 'helpers', 'public-cert.pem'))]
113 }
114
115 it('should throw an error when it is called with cert and key set but no protocol specified', function () {
116 // to do rewrite wrap function
117 (function () {
118 var c = mqtt.connect(sslOpts2)
119 c.end()
120 }).should.throw('Missing secure protocol key')
121 })
122
123 it('should throw an error when it is called with cert and key set and protocol other than allowed: mqtt,mqtts,ws,wss', function () {
124 (function () {
125 sslOpts2.protocol = 'UNKNOWNPROTOCOL'
126 var c = mqtt.connect(sslOpts2)
127 c.end()
128 }).should.throw()
129 })
130
131 it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtt', function () {
132 sslOpts2.protocol = 'mqtt'
133 var c = mqtt.connect(sslOpts2)
134
135 c.options.should.have.property('protocol', 'mqtts')
136
137 c.on('error', function () {})
138
139 c.should.be.instanceOf(mqtt.MqttClient)
140 })
141
142 it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtts', function () {
143 sslOpts2.protocol = 'mqtts'
144 var c = mqtt.connect(sslOpts2)
145
146 c.options.should.have.property('protocol', 'mqtts')
147
148 c.on('error', function () {})
149
150 c.should.be.instanceOf(mqtt.MqttClient)
151 })
152
153 it('should return a MqttClient with wss set when connect is called key and cert set and protocol ws', function () {
154 sslOpts2.protocol = 'ws'
155 var c = mqtt.connect(sslOpts2)
156
157 c.options.should.have.property('protocol', 'wss')
158
159 c.on('error', function () {})
160
161 c.should.be.instanceOf(mqtt.MqttClient)
162 })
163
164 it('should return a MqttClient with wss set when connect is called key and cert set and protocol wss', function () {
165 sslOpts2.protocol = 'wss'
166 var c = mqtt.connect(sslOpts2)
167
168 c.options.should.have.property('protocol', 'wss')
169
170 c.on('error', function () {})
171
172 c.should.be.instanceOf(mqtt.MqttClient)
173 })
174 })
175})