mqtt 2.18.9 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CONTRIBUTING.md +27 -27
  2. package/LICENSE.md +15 -15
  3. package/README.md +689 -612
  4. package/bin/pub.js +146 -146
  5. package/bin/sub.js +123 -123
  6. package/dist/mqtt.js +11916 -14116
  7. package/dist/mqtt.min.js +1 -1
  8. package/doc/help.txt +8 -8
  9. package/doc/publish.txt +26 -26
  10. package/doc/subscribe.txt +26 -26
  11. package/examples/client/secure-client.js +24 -24
  12. package/examples/client/simple-both.js +13 -13
  13. package/examples/client/simple-publish.js +7 -7
  14. package/examples/client/simple-subscribe.js +9 -9
  15. package/examples/tls client/crt.ca.cg.pem +34 -34
  16. package/examples/tls client/mqttclient.js +48 -48
  17. package/examples/tls client/tls-cert.pem +13 -13
  18. package/examples/tls client/tls-key.pem +15 -15
  19. package/examples/wss/client.js +49 -49
  20. package/examples/wss/client_with_proxy.js +58 -58
  21. package/lib/client.js +1460 -1129
  22. package/lib/connect/ali.js +130 -0
  23. package/lib/connect/index.js +157 -149
  24. package/lib/connect/tcp.js +19 -19
  25. package/lib/connect/tls.js +41 -41
  26. package/lib/connect/ws.js +92 -92
  27. package/lib/connect/wx.js +134 -110
  28. package/lib/store.js +140 -140
  29. package/lib/validations.js +52 -52
  30. package/mqtt.js +41 -41
  31. package/package.json +112 -107
  32. package/test/abstract_client.js +3084 -2667
  33. package/test/abstract_store.js +135 -135
  34. package/test/browser/server.js +132 -132
  35. package/test/browser/test.js +92 -92
  36. package/test/client.js +1129 -544
  37. package/test/helpers/private-csr.pem +16 -16
  38. package/test/helpers/private-key.pem +27 -27
  39. package/test/helpers/public-cert.pem +19 -19
  40. package/test/helpers/public-key.pem +9 -9
  41. package/test/helpers/server.js +52 -52
  42. package/test/helpers/server_process.js +9 -9
  43. package/test/helpers/tls-cert.pem +14 -14
  44. package/test/helpers/tls-csr.pem +11 -11
  45. package/test/helpers/tls-key.pem +15 -15
  46. package/test/helpers/wrong-cert.pem +13 -13
  47. package/test/helpers/wrong-csr.pem +11 -11
  48. package/test/helpers/wrong-key.pem +15 -15
  49. package/test/mocha.opts +4 -4
  50. package/test/mqtt.js +230 -230
  51. package/test/mqtt_store.js +9 -9
  52. package/test/secure_client.js +157 -157
  53. package/test/server.js +93 -65
  54. package/test/store.js +10 -10
  55. package/test/typescript/broker-connect-subscribe-and-publish.js +24 -0
  56. package/test/typescript/broker-connect-subscribe-and-publish.js.map +1 -0
  57. package/test/typescript/broker-connect-subscribe-and-publish.ts +22 -22
  58. package/test/typescript/tsconfig.json +14 -14
  59. package/test/util.js +13 -13
  60. package/test/websocket_client.js +144 -144
  61. package/types/index.d.ts +27 -27
  62. package/types/lib/client-options.d.ts +178 -137
  63. package/types/lib/client.d.ts +231 -196
  64. package/types/lib/connect/index.d.ts +10 -10
  65. package/types/lib/store-options.d.ts +6 -6
  66. package/types/lib/store.d.ts +46 -46
  67. package/test/browser/wx.js +0 -93
package/test/client.js CHANGED
@@ -1,544 +1,1129 @@
1
- 'use strict'
2
-
3
- var mqtt = require('..')
4
- var should = require('should')
5
- var fork = require('child_process').fork
6
- var path = require('path')
7
- var abstractClientTests = require('./abstract_client')
8
- var net = require('net')
9
- var eos = require('end-of-stream')
10
- var mqttPacket = require('mqtt-packet')
11
- var Buffer = require('safe-buffer').Buffer
12
- var Duplex = require('readable-stream').Duplex
13
- var Connection = require('mqtt-connection')
14
- var Server = require('./server')
15
- var port = 9876
16
- var server
17
-
18
- function connOnlyServer () {
19
- return new Server(function (client) {
20
- client.on('connect', function (packet) {
21
- client.connack({returnCode: 0})
22
- })
23
- })
24
- }
25
-
26
- /**
27
- * Test server
28
- */
29
- function buildServer () {
30
- return new Server(function (client) {
31
- client.on('connect', function (packet) {
32
- if (packet.clientId === 'invalid') {
33
- client.connack({returnCode: 2})
34
- } else {
35
- client.connack({returnCode: 0})
36
- }
37
- })
38
-
39
- client.on('publish', function (packet) {
40
- setImmediate(function () {
41
- switch (packet.qos) {
42
- case 0:
43
- break
44
- case 1:
45
- client.puback(packet)
46
- break
47
- case 2:
48
- client.pubrec(packet)
49
- break
50
- }
51
- })
52
- })
53
-
54
- client.on('pubrel', function (packet) {
55
- client.pubcomp(packet)
56
- })
57
-
58
- client.on('pubrec', function (packet) {
59
- client.pubrel(packet)
60
- })
61
-
62
- client.on('pubcomp', function () {
63
- // Nothing to be done
64
- })
65
-
66
- client.on('subscribe', function (packet) {
67
- client.suback({
68
- messageId: packet.messageId,
69
- granted: packet.subscriptions.map(function (e) {
70
- return e.qos
71
- })
72
- })
73
- })
74
-
75
- client.on('unsubscribe', function (packet) {
76
- client.unsuback(packet)
77
- })
78
-
79
- client.on('pingreq', function () {
80
- client.pingresp()
81
- })
82
- })
83
- }
84
-
85
- server = buildServer().listen(port)
86
-
87
- describe('MqttClient', function () {
88
- describe('creating', function () {
89
- it('should allow instantiation of MqttClient without the \'new\' operator', function (done) {
90
- should(function () {
91
- var client
92
- try {
93
- client = mqtt.MqttClient(function () {
94
- throw Error('break')
95
- }, {})
96
- client.end()
97
- } catch (err) {
98
- if (err.message !== 'break') {
99
- throw err
100
- }
101
- done()
102
- }
103
- }).not.throw('Object #<Object> has no method \'_setupStream\'')
104
- })
105
- })
106
-
107
- var config = { protocol: 'mqtt', port: port }
108
- abstractClientTests(server, config)
109
-
110
- describe('message ids', function () {
111
- it('should increment the message id', function () {
112
- var client = mqtt.connect(config)
113
- var currentId = client._nextId()
114
-
115
- client._nextId().should.equal(currentId + 1)
116
- client.end()
117
- })
118
-
119
- it('should return 1 once the internal counter reached limit', function () {
120
- var client = mqtt.connect(config)
121
- client.nextId = 65535
122
-
123
- client._nextId().should.equal(65535)
124
- client._nextId().should.equal(1)
125
- client.end()
126
- })
127
-
128
- it('should return 65535 for last message id once the internal counter reached limit', function () {
129
- var client = mqtt.connect(config)
130
- client.nextId = 65535
131
-
132
- client._nextId().should.equal(65535)
133
- client.getLastMessageId().should.equal(65535)
134
- client._nextId().should.equal(1)
135
- client.getLastMessageId().should.equal(1)
136
- client.end()
137
- })
138
-
139
- it('should not throw an error if packet\'s messageId is not found when receiving a pubrel packet', function (done) {
140
- var server2 = new Server(function (c) {
141
- c.on('connect', function (packet) {
142
- c.connack({returnCode: 0})
143
- c.pubrel({ messageId: Math.floor(Math.random() * 9000) + 1000 })
144
- })
145
- })
146
-
147
- server2.listen(port + 49, function () {
148
- var client = mqtt.connect({
149
- port: port + 49,
150
- host: 'localhost'
151
- })
152
-
153
- client.on('packetsend', function (packet) {
154
- if (packet.cmd === 'pubcomp') {
155
- client.end()
156
- server2.close()
157
- done()
158
- }
159
- })
160
- })
161
- })
162
-
163
- it('should not go overflow if the TCP frame contains a lot of PUBLISH packets', function (done) {
164
- var parser = mqttPacket.parser()
165
- var count = 0
166
- var max = 1000
167
- var duplex = new Duplex({
168
- read: function (n) {},
169
- write: function (chunk, enc, cb) {
170
- parser.parse(chunk)
171
- cb() // nothing to do
172
- }
173
- })
174
- var client = new mqtt.MqttClient(function () {
175
- return duplex
176
- }, {})
177
-
178
- client.on('message', function (t, p, packet) {
179
- if (++count === max) {
180
- done()
181
- }
182
- })
183
-
184
- parser.on('packet', function (packet) {
185
- var packets = []
186
-
187
- if (packet.cmd === 'connect') {
188
- duplex.push(mqttPacket.generate({
189
- cmd: 'connack',
190
- sessionPresent: false,
191
- returnCode: 0
192
- }))
193
-
194
- for (var i = 0; i < max; i++) {
195
- packets.push(mqttPacket.generate({
196
- cmd: 'publish',
197
- topic: Buffer.from('hello'),
198
- payload: Buffer.from('world'),
199
- retain: false,
200
- dup: false,
201
- messageId: i + 1,
202
- qos: 1
203
- }))
204
- }
205
-
206
- duplex.push(Buffer.concat(packets))
207
- }
208
- })
209
- })
210
- })
211
-
212
- describe('flushing', function () {
213
- it('should attempt to complete pending unsub and send on ping timeout', function (done) {
214
- this.timeout(10000)
215
- var server3 = connOnlyServer().listen(port + 72)
216
- var pubCallbackCalled = false
217
- var unsubscribeCallbackCalled = false
218
- var client = mqtt.connect({
219
- port: port + 72,
220
- host: 'localhost',
221
- keepalive: 1,
222
- connectTimeout: 350,
223
- reconnectPeriod: 0
224
- })
225
- client.once('connect', () => {
226
- client.publish('fakeTopic', 'fakeMessage', {qos: 1}, (err, result) => {
227
- should.exist(err)
228
- pubCallbackCalled = true
229
- })
230
- client.unsubscribe('fakeTopic', (err, result) => {
231
- should.exist(err)
232
- unsubscribeCallbackCalled = true
233
- })
234
- setTimeout(() => {
235
- client.end(() => {
236
- should.equal(pubCallbackCalled && unsubscribeCallbackCalled, true, 'callbacks not invoked')
237
- server3.close()
238
- done()
239
- })
240
- }, 5000)
241
- })
242
- })
243
- })
244
-
245
- describe('reconnecting', function () {
246
- it('should attempt to reconnect once server is down', function (done) {
247
- this.timeout(15000)
248
-
249
- var innerServer = fork(path.join(__dirname, 'helpers', 'server_process.js'))
250
- var client = mqtt.connect({ port: 3000, host: 'localhost', keepalive: 1 })
251
-
252
- client.once('connect', function () {
253
- innerServer.kill('SIGINT') // mocks server shutdown
254
-
255
- client.once('close', function () {
256
- should.exist(client.reconnectTimer)
257
- client.end()
258
- done()
259
- })
260
- })
261
- })
262
-
263
- it('should reconnect to multiple host-ports-protocol combinations if servers is passed', function (done) {
264
- this.timeout(15000)
265
-
266
- var server2 = buildServer().listen(port + 42)
267
-
268
- server2.on('listening', function () {
269
- var client = mqtt.connect({
270
- protocol: 'wss',
271
- servers: [
272
- { port: port + 42, host: 'localhost', protocol: 'ws' },
273
- { port: port, host: 'localhost' }
274
- ],
275
- keepalive: 50
276
- })
277
- server2.on('client', function (c) {
278
- should.equal(client.stream.socket.url, 'ws://localhost:9918/', 'Protocol for first connection should use ws.')
279
- c.stream.destroy()
280
- server2.close()
281
- })
282
-
283
- server.once('client', function () {
284
- should.equal(client.stream.socket.url, 'wss://localhost:9876/', 'Protocol for second client should use the default protocol: wss, on port: port + 42.')
285
- client.end()
286
- done()
287
- })
288
-
289
- client.once('connect', function () {
290
- client.stream.destroy()
291
- })
292
- })
293
- })
294
-
295
- it('should reconnect if a connack is not received in an interval', function (done) {
296
- this.timeout(2000)
297
-
298
- var server2 = net.createServer().listen(port + 43)
299
-
300
- server2.on('connection', function (c) {
301
- eos(c, function () {
302
- server2.close()
303
- })
304
- })
305
-
306
- server2.on('listening', function () {
307
- var client = mqtt.connect({
308
- servers: [
309
- { port: port + 43, host: 'localhost_fake' },
310
- { port: port, host: 'localhost' }
311
- ],
312
- connectTimeout: 500
313
- })
314
-
315
- server.once('client', function () {
316
- client.end()
317
- done()
318
- })
319
-
320
- client.once('connect', function () {
321
- client.stream.destroy()
322
- })
323
- })
324
- })
325
-
326
- it('should not be cleared by the connack timer', function (done) {
327
- this.timeout(4000)
328
-
329
- var server2 = net.createServer().listen(port + 44)
330
-
331
- server2.on('connection', function (c) {
332
- c.destroy()
333
- })
334
-
335
- server2.once('listening', function () {
336
- var reconnects = 0
337
- var connectTimeout = 1000
338
- var reconnectPeriod = 100
339
- var expectedReconnects = Math.floor(connectTimeout / reconnectPeriod)
340
- var client = mqtt.connect({
341
- port: port + 44,
342
- host: 'localhost',
343
- connectTimeout: connectTimeout,
344
- reconnectPeriod: reconnectPeriod
345
- })
346
-
347
- client.on('reconnect', function () {
348
- reconnects++
349
- if (reconnects >= expectedReconnects) {
350
- client.end()
351
- done()
352
- }
353
- })
354
- })
355
- })
356
-
357
- it('should not keep requeueing the first message when offline', function (done) {
358
- this.timeout(2500)
359
-
360
- var server2 = buildServer().listen(port + 45)
361
- var client = mqtt.connect({
362
- port: port + 45,
363
- host: 'localhost',
364
- connectTimeout: 350,
365
- reconnectPeriod: 300
366
- })
367
-
368
- server2.on('client', function (c) {
369
- client.publish('hello', 'world', { qos: 1 }, function () {
370
- c.destroy()
371
- server2.close()
372
- client.publish('hello', 'world', { qos: 1 })
373
- })
374
- })
375
-
376
- setTimeout(function () {
377
- if (client.queue.length === 0) {
378
- client.end(true)
379
- done()
380
- } else {
381
- client.end(true)
382
- }
383
- }, 2000)
384
- })
385
-
386
- it('should not send the same subscribe multiple times on a flaky connection', function (done) {
387
- this.timeout(3500)
388
-
389
- var KILL_COUNT = 4
390
- var killedConnections = 0
391
- var subIds = {}
392
- var client = mqtt.connect({
393
- port: port + 46,
394
- host: 'localhost',
395
- connectTimeout: 350,
396
- reconnectPeriod: 300
397
- })
398
-
399
- var server2 = new Server(function (client) {
400
- client.on('error', function () {})
401
- client.on('connect', function (packet) {
402
- if (packet.clientId === 'invalid') {
403
- client.connack({returnCode: 2})
404
- } else {
405
- client.connack({returnCode: 0})
406
- }
407
- })
408
- }).listen(port + 46)
409
-
410
- server2.on('client', function (c) {
411
- client.subscribe('topic', function () {
412
- done()
413
- client.end()
414
- c.destroy()
415
- server2.close()
416
- })
417
-
418
- c.on('subscribe', function (packet) {
419
- if (killedConnections < KILL_COUNT) {
420
- // Kill the first few sub attempts to simulate a flaky connection
421
- killedConnections++
422
- c.destroy()
423
- } else {
424
- // Keep track of acks
425
- if (!subIds[packet.messageId]) {
426
- subIds[packet.messageId] = 0
427
- }
428
- subIds[packet.messageId]++
429
- if (subIds[packet.messageId] > 1) {
430
- done(new Error('Multiple duplicate acked subscriptions received for messageId ' + packet.messageId))
431
- client.end(true)
432
- c.destroy()
433
- server2.destroy()
434
- }
435
-
436
- c.suback({
437
- messageId: packet.messageId,
438
- granted: packet.subscriptions.map(function (e) {
439
- return e.qos
440
- })
441
- })
442
- }
443
- })
444
- })
445
- })
446
-
447
- it('should not fill the queue of subscribes if it cannot connect', function (done) {
448
- this.timeout(2500)
449
-
450
- var port2 = port + 48
451
-
452
- var server2 = net.createServer(function (stream) {
453
- var client = new Connection(stream)
454
-
455
- client.on('error', function () {})
456
- client.on('connect', function (packet) {
457
- client.connack({returnCode: 0})
458
- client.destroy()
459
- })
460
- })
461
-
462
- server2.listen(port2, function () {
463
- var client = mqtt.connect({
464
- port: port2,
465
- host: 'localhost',
466
- connectTimeout: 350,
467
- reconnectPeriod: 300
468
- })
469
-
470
- client.subscribe('hello')
471
-
472
- setTimeout(function () {
473
- client.queue.length.should.equal(1)
474
- client.end()
475
- done()
476
- }, 1000)
477
- })
478
- })
479
-
480
- it('should not send the same publish multiple times on a flaky connection', function (done) {
481
- this.timeout(3500)
482
-
483
- var KILL_COUNT = 4
484
- var killedConnections = 0
485
- var pubIds = {}
486
- var client = mqtt.connect({
487
- port: port + 47,
488
- host: 'localhost',
489
- connectTimeout: 350,
490
- reconnectPeriod: 300
491
- })
492
-
493
- var server2 = net.createServer(function (stream) {
494
- var client = new Connection(stream)
495
- client.on('error', function () {})
496
- client.on('connect', function (packet) {
497
- if (packet.clientId === 'invalid') {
498
- client.connack({returnCode: 2})
499
- } else {
500
- client.connack({returnCode: 0})
501
- }
502
- })
503
-
504
- this.emit('client', client)
505
- }).listen(port + 47)
506
-
507
- server2.on('client', function (c) {
508
- client.publish('topic', 'data', { qos: 1 }, function () {
509
- done()
510
- client.end()
511
- c.destroy()
512
- server2.destroy()
513
- })
514
-
515
- c.on('publish', function onPublish (packet) {
516
- if (killedConnections < KILL_COUNT) {
517
- // Kill the first few pub attempts to simulate a flaky connection
518
- killedConnections++
519
- c.destroy()
520
-
521
- // to avoid receiving inflight messages
522
- c.removeListener('publish', onPublish)
523
- } else {
524
- // Keep track of acks
525
- if (!pubIds[packet.messageId]) {
526
- pubIds[packet.messageId] = 0
527
- }
528
-
529
- pubIds[packet.messageId]++
530
-
531
- if (pubIds[packet.messageId] > 1) {
532
- done(new Error('Multiple duplicate acked publishes received for messageId ' + packet.messageId))
533
- client.end(true)
534
- c.destroy()
535
- server2.destroy()
536
- }
537
-
538
- c.puback(packet)
539
- }
540
- })
541
- })
542
- })
543
- })
544
- })
1
+ 'use strict'
2
+
3
+ var mqtt = require('..')
4
+ var should = require('should')
5
+ var fork = require('child_process').fork
6
+ var path = require('path')
7
+ var abstractClientTests = require('./abstract_client')
8
+ var net = require('net')
9
+ var eos = require('end-of-stream')
10
+ var mqttPacket = require('mqtt-packet')
11
+ var Buffer = require('safe-buffer').Buffer
12
+ var Duplex = require('readable-stream').Duplex
13
+ var Connection = require('mqtt-connection')
14
+ var Server = require('./server')
15
+ var FastServer = require('./server').FastMqttServer
16
+ var port = 9876
17
+ var server
18
+
19
+ function connOnlyServer () {
20
+ return new Server(function (client) {
21
+ client.on('connect', function (packet) {
22
+ client.connack({returnCode: 0})
23
+ })
24
+ })
25
+ }
26
+
27
+ /**
28
+ * Test server
29
+ */
30
+ function buildServer (fastFlag) {
31
+ var handler = function (client) {
32
+ client.on('auth', function (packet) {
33
+ var rc = 'reasonCode'
34
+ var connack = {}
35
+ connack[rc] = 0
36
+ client.connack(connack)
37
+ })
38
+ client.on('connect', function (packet) {
39
+ var rc = 'returnCode'
40
+ var connack = {}
41
+ if (client.options && client.options.protocolVersion === 5) {
42
+ rc = 'reasonCode'
43
+ if (packet.clientId === 'invalid') {
44
+ connack[rc] = 128
45
+ } else {
46
+ connack[rc] = 0
47
+ }
48
+ } else {
49
+ if (packet.clientId === 'invalid') {
50
+ connack[rc] = 2
51
+ } else {
52
+ connack[rc] = 0
53
+ }
54
+ }
55
+ if (packet.properties && packet.properties.authenticationMethod) {
56
+ return false
57
+ } else {
58
+ client.connack(connack)
59
+ }
60
+ })
61
+
62
+ client.on('publish', function (packet) {
63
+ setImmediate(function () {
64
+ switch (packet.qos) {
65
+ case 0:
66
+ break
67
+ case 1:
68
+ client.puback(packet)
69
+ break
70
+ case 2:
71
+ client.pubrec(packet)
72
+ break
73
+ }
74
+ })
75
+ })
76
+
77
+ client.on('pubrel', function (packet) {
78
+ client.pubcomp(packet)
79
+ })
80
+
81
+ client.on('pubrec', function (packet) {
82
+ client.pubrel(packet)
83
+ })
84
+
85
+ client.on('pubcomp', function () {
86
+ // Nothing to be done
87
+ })
88
+
89
+ client.on('subscribe', function (packet) {
90
+ client.suback({
91
+ messageId: packet.messageId,
92
+ granted: packet.subscriptions.map(function (e) {
93
+ return e.qos
94
+ })
95
+ })
96
+ })
97
+
98
+ client.on('unsubscribe', function (packet) {
99
+ packet.granted = packet.unsubscriptions.map(function () { return 0 })
100
+ client.unsuback(packet)
101
+ })
102
+
103
+ client.on('pingreq', function () {
104
+ client.pingresp()
105
+ })
106
+ }
107
+ if (fastFlag) {
108
+ return new FastServer(handler)
109
+ } else {
110
+ return new Server(handler)
111
+ }
112
+ }
113
+
114
+ server = buildServer().listen(port)
115
+
116
+ describe('MqttClient', function () {
117
+ describe('creating', function () {
118
+ it('should allow instantiation of MqttClient without the \'new\' operator', function (done) {
119
+ should(function () {
120
+ var client
121
+ try {
122
+ client = mqtt.MqttClient(function () {
123
+ throw Error('break')
124
+ }, {})
125
+ client.end()
126
+ } catch (err) {
127
+ if (err.message !== 'break') {
128
+ throw err
129
+ }
130
+ done()
131
+ }
132
+ }).not.throw('Object #<Object> has no method \'_setupStream\'')
133
+ })
134
+ })
135
+
136
+ var config = { protocol: 'mqtt', port: port }
137
+ abstractClientTests(server, config)
138
+
139
+ describe('message ids', function () {
140
+ it('should increment the message id', function () {
141
+ var client = mqtt.connect(config)
142
+ var currentId = client._nextId()
143
+
144
+ client._nextId().should.equal(currentId + 1)
145
+ client.end()
146
+ })
147
+
148
+ it('should return 1 once the internal counter reached limit', function () {
149
+ var client = mqtt.connect(config)
150
+ client.nextId = 65535
151
+
152
+ client._nextId().should.equal(65535)
153
+ client._nextId().should.equal(1)
154
+ client.end()
155
+ })
156
+
157
+ it('should return 65535 for last message id once the internal counter reached limit', function () {
158
+ var client = mqtt.connect(config)
159
+ client.nextId = 65535
160
+
161
+ client._nextId().should.equal(65535)
162
+ client.getLastMessageId().should.equal(65535)
163
+ client._nextId().should.equal(1)
164
+ client.getLastMessageId().should.equal(1)
165
+ client.end()
166
+ })
167
+
168
+ it('should not throw an error if packet\'s messageId is not found when receiving a pubrel packet', function (done) {
169
+ var server2 = new Server(function (c) {
170
+ c.on('connect', function (packet) {
171
+ c.connack({returnCode: 0})
172
+ c.pubrel({ messageId: Math.floor(Math.random() * 9000) + 1000 })
173
+ })
174
+ })
175
+
176
+ server2.listen(port + 49, function () {
177
+ var client = mqtt.connect({
178
+ port: port + 49,
179
+ host: 'localhost'
180
+ })
181
+
182
+ client.on('packetsend', function (packet) {
183
+ if (packet.cmd === 'pubcomp') {
184
+ client.end()
185
+ server2.close()
186
+ done()
187
+ }
188
+ })
189
+ })
190
+ })
191
+
192
+ it('should not go overflow if the TCP frame contains a lot of PUBLISH packets', function (done) {
193
+ var parser = mqttPacket.parser()
194
+ var count = 0
195
+ var max = 1000
196
+ var duplex = new Duplex({
197
+ read: function (n) {},
198
+ write: function (chunk, enc, cb) {
199
+ parser.parse(chunk)
200
+ cb() // nothing to do
201
+ }
202
+ })
203
+ var client = new mqtt.MqttClient(function () {
204
+ return duplex
205
+ }, {})
206
+
207
+ client.on('message', function (t, p, packet) {
208
+ if (++count === max) {
209
+ done()
210
+ }
211
+ })
212
+
213
+ parser.on('packet', function (packet) {
214
+ var packets = []
215
+
216
+ if (packet.cmd === 'connect') {
217
+ duplex.push(mqttPacket.generate({
218
+ cmd: 'connack',
219
+ sessionPresent: false,
220
+ returnCode: 0
221
+ }))
222
+
223
+ for (var i = 0; i < max; i++) {
224
+ packets.push(mqttPacket.generate({
225
+ cmd: 'publish',
226
+ topic: Buffer.from('hello'),
227
+ payload: Buffer.from('world'),
228
+ retain: false,
229
+ dup: false,
230
+ messageId: i + 1,
231
+ qos: 1
232
+ }))
233
+ }
234
+
235
+ duplex.push(Buffer.concat(packets))
236
+ }
237
+ })
238
+ })
239
+ })
240
+
241
+ describe('flushing', function () {
242
+ it('should attempt to complete pending unsub and send on ping timeout', function (done) {
243
+ this.timeout(10000)
244
+ var server3 = connOnlyServer().listen(port + 72)
245
+ var pubCallbackCalled = false
246
+ var unsubscribeCallbackCalled = false
247
+ var client = mqtt.connect({
248
+ port: port + 72,
249
+ host: 'localhost',
250
+ keepalive: 1,
251
+ connectTimeout: 350,
252
+ reconnectPeriod: 0
253
+ })
254
+ client.once('connect', () => {
255
+ client.publish('fakeTopic', 'fakeMessage', {qos: 1}, (err, result) => {
256
+ should.exist(err)
257
+ pubCallbackCalled = true
258
+ })
259
+ client.unsubscribe('fakeTopic', (err, result) => {
260
+ should.exist(err)
261
+ unsubscribeCallbackCalled = true
262
+ })
263
+ setTimeout(() => {
264
+ client.end(() => {
265
+ should.equal(pubCallbackCalled && unsubscribeCallbackCalled, true, 'callbacks not invoked')
266
+ server3.close()
267
+ done()
268
+ })
269
+ }, 5000)
270
+ })
271
+ })
272
+ })
273
+
274
+ describe('reconnecting', function () {
275
+ it('should attempt to reconnect once server is down', function (done) {
276
+ this.timeout(15000)
277
+
278
+ var innerServer = fork(path.join(__dirname, 'helpers', 'server_process.js'))
279
+ var client = mqtt.connect({ port: 3000, host: 'localhost', keepalive: 1 })
280
+
281
+ client.once('connect', function () {
282
+ innerServer.kill('SIGINT') // mocks server shutdown
283
+
284
+ client.once('close', function () {
285
+ should.exist(client.reconnectTimer)
286
+ client.end()
287
+ done()
288
+ })
289
+ })
290
+ })
291
+
292
+ it('should reconnect to multiple host-ports-protocol combinations if servers is passed', function (done) {
293
+ this.timeout(15000)
294
+
295
+ var server = buildServer(true).listen(port + 41)
296
+ var server2 = buildServer(true).listen(port + 42)
297
+
298
+ server2.on('listening', function () {
299
+ var client = mqtt.connect({
300
+ protocol: 'wss',
301
+ servers: [
302
+ { port: port + 42, host: 'localhost', protocol: 'ws' },
303
+ { port: port + 41, host: 'localhost' }
304
+ ],
305
+ keepalive: 50
306
+ })
307
+ server2.on('client', function (c) {
308
+ should.equal(client.stream.socket.url, 'ws://localhost:9918/', 'Protocol for first connection should use ws.')
309
+ c.stream.destroy()
310
+ server2.close()
311
+ })
312
+
313
+ server.once('client', function () {
314
+ should.equal(client.stream.socket.url, 'wss://localhost:9917/', 'Protocol for second client should use the default protocol: wss, on port: port + 42.')
315
+ client.end()
316
+ done()
317
+ })
318
+
319
+ client.once('connect', function () {
320
+ client.stream.destroy()
321
+ })
322
+ })
323
+ })
324
+
325
+ it('should reconnect if a connack is not received in an interval', function (done) {
326
+ this.timeout(2000)
327
+
328
+ var server2 = net.createServer().listen(port + 43)
329
+
330
+ server2.on('connection', function (c) {
331
+ eos(c, function () {
332
+ server2.close()
333
+ })
334
+ })
335
+
336
+ server2.on('listening', function () {
337
+ var client = mqtt.connect({
338
+ servers: [
339
+ { port: port + 43, host: 'localhost_fake' },
340
+ { port: port, host: 'localhost' }
341
+ ],
342
+ connectTimeout: 500
343
+ })
344
+
345
+ server.once('client', function () {
346
+ client.end()
347
+ done()
348
+ })
349
+
350
+ client.once('connect', function () {
351
+ client.stream.destroy()
352
+ })
353
+ })
354
+ })
355
+
356
+ it('should not be cleared by the connack timer', function (done) {
357
+ this.timeout(4000)
358
+
359
+ var server2 = net.createServer().listen(port + 44)
360
+
361
+ server2.on('connection', function (c) {
362
+ c.destroy()
363
+ })
364
+
365
+ server2.once('listening', function () {
366
+ var reconnects = 0
367
+ var connectTimeout = 1000
368
+ var reconnectPeriod = 100
369
+ var expectedReconnects = Math.floor(connectTimeout / reconnectPeriod)
370
+ var client = mqtt.connect({
371
+ port: port + 44,
372
+ host: 'localhost',
373
+ connectTimeout: connectTimeout,
374
+ reconnectPeriod: reconnectPeriod
375
+ })
376
+
377
+ client.on('reconnect', function () {
378
+ reconnects++
379
+ if (reconnects >= expectedReconnects) {
380
+ client.end()
381
+ done()
382
+ }
383
+ })
384
+ })
385
+ })
386
+
387
+ it('should not keep requeueing the first message when offline', function (done) {
388
+ this.timeout(2500)
389
+
390
+ var server2 = buildServer().listen(port + 45)
391
+ var client = mqtt.connect({
392
+ port: port + 45,
393
+ host: 'localhost',
394
+ connectTimeout: 350,
395
+ reconnectPeriod: 300
396
+ })
397
+
398
+ server2.on('client', function (c) {
399
+ client.publish('hello', 'world', { qos: 1 }, function () {
400
+ c.destroy()
401
+ server2.close()
402
+ client.publish('hello', 'world', { qos: 1 })
403
+ })
404
+ })
405
+
406
+ setTimeout(function () {
407
+ if (client.queue.length === 0) {
408
+ client.end(true)
409
+ done()
410
+ } else {
411
+ client.end(true)
412
+ }
413
+ }, 2000)
414
+ })
415
+
416
+ it('should not send the same subscribe multiple times on a flaky connection', function (done) {
417
+ this.timeout(3500)
418
+
419
+ var KILL_COUNT = 4
420
+ var killedConnections = 0
421
+ var subIds = {}
422
+ var client = mqtt.connect({
423
+ port: port + 46,
424
+ host: 'localhost',
425
+ connectTimeout: 350,
426
+ reconnectPeriod: 300
427
+ })
428
+
429
+ var server2 = new Server(function (client) {
430
+ client.on('error', function () {})
431
+ client.on('connect', function (packet) {
432
+ if (packet.clientId === 'invalid') {
433
+ client.connack({returnCode: 2})
434
+ } else {
435
+ client.connack({returnCode: 0})
436
+ }
437
+ })
438
+ }).listen(port + 46)
439
+
440
+ server2.on('client', function (c) {
441
+ client.subscribe('topic', function () {
442
+ done()
443
+ client.end()
444
+ c.destroy()
445
+ server2.close()
446
+ })
447
+
448
+ c.on('subscribe', function (packet) {
449
+ if (killedConnections < KILL_COUNT) {
450
+ // Kill the first few sub attempts to simulate a flaky connection
451
+ killedConnections++
452
+ c.destroy()
453
+ } else {
454
+ // Keep track of acks
455
+ if (!subIds[packet.messageId]) {
456
+ subIds[packet.messageId] = 0
457
+ }
458
+ subIds[packet.messageId]++
459
+ if (subIds[packet.messageId] > 1) {
460
+ done(new Error('Multiple duplicate acked subscriptions received for messageId ' + packet.messageId))
461
+ client.end(true)
462
+ c.destroy()
463
+ server2.destroy()
464
+ }
465
+
466
+ c.suback({
467
+ messageId: packet.messageId,
468
+ granted: packet.subscriptions.map(function (e) {
469
+ return e.qos
470
+ })
471
+ })
472
+ }
473
+ })
474
+ })
475
+ })
476
+
477
+ it('should not fill the queue of subscribes if it cannot connect', function (done) {
478
+ this.timeout(2500)
479
+
480
+ var port2 = port + 48
481
+
482
+ var server2 = net.createServer(function (stream) {
483
+ var client = new Connection(stream)
484
+
485
+ client.on('error', function () {})
486
+ client.on('connect', function (packet) {
487
+ client.connack({returnCode: 0})
488
+ client.destroy()
489
+ })
490
+ })
491
+
492
+ server2.listen(port2, function () {
493
+ var client = mqtt.connect({
494
+ port: port2,
495
+ host: 'localhost',
496
+ connectTimeout: 350,
497
+ reconnectPeriod: 300
498
+ })
499
+
500
+ client.subscribe('hello')
501
+
502
+ setTimeout(function () {
503
+ client.queue.length.should.equal(1)
504
+ client.end()
505
+ done()
506
+ }, 1000)
507
+ })
508
+ })
509
+
510
+ it('should not send the same publish multiple times on a flaky connection', function (done) {
511
+ this.timeout(3500)
512
+
513
+ var KILL_COUNT = 4
514
+ var killedConnections = 0
515
+ var pubIds = {}
516
+ var client = mqtt.connect({
517
+ port: port + 47,
518
+ host: 'localhost',
519
+ connectTimeout: 350,
520
+ reconnectPeriod: 300
521
+ })
522
+
523
+ var server2 = net.createServer(function (stream) {
524
+ var client = new Connection(stream)
525
+ client.on('error', function () {})
526
+ client.on('connect', function (packet) {
527
+ if (packet.clientId === 'invalid') {
528
+ client.connack({returnCode: 2})
529
+ } else {
530
+ client.connack({returnCode: 0})
531
+ }
532
+ })
533
+
534
+ this.emit('client', client)
535
+ }).listen(port + 47)
536
+
537
+ server2.on('client', function (c) {
538
+ client.publish('topic', 'data', { qos: 1 }, function () {
539
+ done()
540
+ client.end()
541
+ c.destroy()
542
+ server2.destroy()
543
+ })
544
+
545
+ c.on('publish', function onPublish (packet) {
546
+ if (killedConnections < KILL_COUNT) {
547
+ // Kill the first few pub attempts to simulate a flaky connection
548
+ killedConnections++
549
+ c.destroy()
550
+
551
+ // to avoid receiving inflight messages
552
+ c.removeListener('publish', onPublish)
553
+ } else {
554
+ // Keep track of acks
555
+ if (!pubIds[packet.messageId]) {
556
+ pubIds[packet.messageId] = 0
557
+ }
558
+
559
+ pubIds[packet.messageId]++
560
+
561
+ if (pubIds[packet.messageId] > 1) {
562
+ done(new Error('Multiple duplicate acked publishes received for messageId ' + packet.messageId))
563
+ client.end(true)
564
+ c.destroy()
565
+ server2.destroy()
566
+ }
567
+
568
+ c.puback(packet)
569
+ }
570
+ })
571
+ })
572
+ })
573
+ })
574
+
575
+ it('check emit error on checkDisconnection w/o callback', function (done) {
576
+ this.timeout(15000)
577
+ var server118 = new Server(function (client) {
578
+ client.on('connect', function (packet) {
579
+ client.connack({
580
+ reasonCode: 0
581
+ })
582
+ })
583
+ client.on('publish', function (packet) {
584
+ setImmediate(function () {
585
+ packet.reasonCode = 0
586
+ client.puback(packet)
587
+ })
588
+ })
589
+ }).listen(port + 118)
590
+ var opts = {
591
+ host: 'localhost',
592
+ port: port + 118,
593
+ protocolVersion: 5
594
+ }
595
+ var client = mqtt.connect(opts)
596
+ client.on('error', function (error) {
597
+ should(error.message).be.equal('client disconnecting')
598
+ server118.close()
599
+ done()
600
+ })
601
+ client.on('connect', function () {
602
+ client.end(function () {
603
+ client._checkDisconnecting()
604
+ })
605
+ server118.close()
606
+ })
607
+ })
608
+
609
+ describe('MQTT 5.0', function () {
610
+ var server = buildServer().listen(port + 115)
611
+ var config = { protocol: 'mqtt', port: port + 115, protocolVersion: 5, properties: { maximumPacketSize: 200 } }
612
+ abstractClientTests(server, config)
613
+ it('should has Auth method with Auth data', function (done) {
614
+ this.timeout(5000)
615
+ var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { authenticationData: Buffer.from([1, 2, 3, 4]) }}
616
+ try {
617
+ mqtt.connect(opts)
618
+ } catch (error) {
619
+ should(error.message).be.equal('Packet has no Authentication Method')
620
+ }
621
+ done()
622
+ })
623
+ it('auth packet', function (done) {
624
+ this.timeout(15000)
625
+ server.once('client', function (client) {
626
+ client.on('auth', function (packet) {
627
+ done()
628
+ })
629
+ })
630
+ var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { authenticationMethod: 'json' }, authPacket: {}}
631
+ mqtt.connect(opts)
632
+ })
633
+ it('Maximum Packet Size', function (done) {
634
+ this.timeout(15000)
635
+ var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { maximumPacketSize: 1 }}
636
+ var client = mqtt.connect(opts)
637
+ client.on('error', function (error) {
638
+ should(error.message).be.equal('exceeding packets size connack')
639
+ done()
640
+ })
641
+ })
642
+ describe('Topic Alias', function () {
643
+ it('topicAlias > topicAliasMaximum', function (done) {
644
+ this.timeout(15000)
645
+ var maximum = 15
646
+ var current = 22
647
+ server.once('client', function (client) {
648
+ client.on('publish', function (packet) {
649
+ if (packet.properties && packet.properties.topicAlias) {
650
+ done(new Error('Packet should not have topicAlias'))
651
+ return false
652
+ }
653
+ done()
654
+ })
655
+ })
656
+ var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { topicAliasMaximum: maximum }}
657
+ var client = mqtt.connect(opts)
658
+ client.publish('t/h', 'Message', { properties: { topicAlias: current } })
659
+ })
660
+ it('topicAlias w/o topicAliasMaximum in settings', function (done) {
661
+ this.timeout(15000)
662
+ server.once('client', function (client) {
663
+ client.on('publish', function (packet) {
664
+ if (packet.properties && packet.properties.topicAlias) {
665
+ done(new Error('Packet should not have topicAlias'))
666
+ return false
667
+ }
668
+ done()
669
+ })
670
+ })
671
+ var opts = {host: 'localhost', port: port + 115, protocolVersion: 5}
672
+ var client = mqtt.connect(opts)
673
+ client.publish('t/h', 'Message', { properties: { topicAlias: 22 } })
674
+ })
675
+ })
676
+ it('Change values of some properties by server response', function (done) {
677
+ this.timeout(15000)
678
+ var server116 = new Server(function (client) {
679
+ client.on('connect', function (packet) {
680
+ client.connack({
681
+ reasonCode: 0,
682
+ properties: {
683
+ topicAliasMaximum: 15,
684
+ serverKeepAlive: 16,
685
+ maximumPacketSize: 95
686
+ }
687
+ })
688
+ })
689
+ }).listen(port + 116)
690
+ var opts = {
691
+ host: 'localhost',
692
+ port: port + 116,
693
+ protocolVersion: 5,
694
+ properties: {
695
+ topicAliasMaximum: 10,
696
+ serverKeepAlive: 11,
697
+ maximumPacketSize: 100
698
+ }
699
+ }
700
+ var client = mqtt.connect(opts)
701
+ client.on('connect', function () {
702
+ should(client.options.keepalive).be.equal(16)
703
+ should(client.options.properties.topicAliasMaximum).be.equal(15)
704
+ should(client.options.properties.maximumPacketSize).be.equal(95)
705
+ server116.close()
706
+ done()
707
+ })
708
+ })
709
+
710
+ it('should resubscribe when reconnecting with protocolVersion 5 and Session Present flag is false', function (done) {
711
+ this.timeout(15000)
712
+ var tryReconnect = true
713
+ var reconnectEvent = false
714
+ var server316 = new Server(function (client) {
715
+ client.on('connect', function (packet) {
716
+ client.connack({
717
+ reasonCode: 0,
718
+ sessionPresent: false
719
+ })
720
+ client.on('subscribe', function () {
721
+ if (!tryReconnect) {
722
+ client.end()
723
+ server316.close()
724
+ done()
725
+ }
726
+ })
727
+ })
728
+ }).listen(port + 316)
729
+ var opts = {
730
+ host: 'localhost',
731
+ port: port + 316,
732
+ protocolVersion: 5
733
+ }
734
+ var client = mqtt.connect(opts)
735
+
736
+ client.on('reconnect', function () {
737
+ reconnectEvent = true
738
+ })
739
+
740
+ client.on('connect', function (connack) {
741
+ should(connack.sessionPresent).be.equal(false)
742
+ if (tryReconnect) {
743
+ client.subscribe('hello', function () {
744
+ client.stream.end()
745
+ })
746
+
747
+ tryReconnect = false
748
+ } else {
749
+ reconnectEvent.should.equal(true)
750
+ }
751
+ })
752
+ })
753
+
754
+ it('should resubscribe when reconnecting with protocolVersion 5 and properties', function (done) {
755
+ this.timeout(15000)
756
+ var tryReconnect = true
757
+ var reconnectEvent = false
758
+ var server326 = new Server(function (client) {
759
+ client.on('connect', function (packet) {
760
+ client.on('subscribe', function (packet) {
761
+ if (!reconnectEvent) {
762
+ client.suback({
763
+ messageId: packet.messageId,
764
+ granted: packet.subscriptions.map(function (e) {
765
+ return e.qos
766
+ })
767
+ })
768
+ } else {
769
+ if (!tryReconnect) {
770
+ should(packet.properties.userProperties.test).be.equal('test')
771
+ client.end()
772
+ server326.close()
773
+ done()
774
+ }
775
+ }
776
+ })
777
+ client.connack({
778
+ reasonCode: 0,
779
+ sessionPresent: false
780
+ })
781
+ })
782
+ }).listen(port + 326)
783
+ var opts = {
784
+ host: 'localhost',
785
+ port: port + 326,
786
+ protocolVersion: 5
787
+ }
788
+ var client = mqtt.connect(opts)
789
+
790
+ client.on('reconnect', function () {
791
+ reconnectEvent = true
792
+ })
793
+
794
+ client.on('connect', function (connack) {
795
+ should(connack.sessionPresent).be.equal(false)
796
+ if (tryReconnect) {
797
+ client.subscribe('hello', { properties: { userProperties: { test: 'test' } } }, function () {
798
+ client.stream.end()
799
+ })
800
+
801
+ tryReconnect = false
802
+ } else {
803
+ reconnectEvent.should.equal(true)
804
+ }
805
+ })
806
+ })
807
+
808
+ var serverErr = new Server(function (client) {
809
+ client.on('connect', function (packet) {
810
+ client.connack({
811
+ reasonCode: 0
812
+ })
813
+ })
814
+ client.on('publish', function (packet) {
815
+ setImmediate(function () {
816
+ switch (packet.qos) {
817
+ case 0:
818
+ break
819
+ case 1:
820
+ packet.reasonCode = 142
821
+ delete packet.cmd
822
+ client.puback(packet)
823
+ break
824
+ case 2:
825
+ packet.reasonCode = 142
826
+ delete packet.cmd
827
+ client.pubrec(packet)
828
+ break
829
+ }
830
+ })
831
+ })
832
+
833
+ client.on('pubrel', function (packet) {
834
+ packet.reasonCode = 142
835
+ delete packet.cmd
836
+ client.pubcomp(packet)
837
+ })
838
+ })
839
+ it('Subscribe properties', function (done) {
840
+ this.timeout(15000)
841
+ var opts = {
842
+ host: 'localhost',
843
+ port: port + 119,
844
+ protocolVersion: 5
845
+ }
846
+ var subOptions = { properties: { subscriptionIdentifier: 1234 } }
847
+ var server119 = new Server(function (client) {
848
+ client.on('connect', function (packet) {
849
+ client.connack({
850
+ reasonCode: 0
851
+ })
852
+ })
853
+ client.on('subscribe', function (packet) {
854
+ should(packet.properties.subscriptionIdentifier).be.equal(subOptions.properties.subscriptionIdentifier)
855
+ server119.close()
856
+ done()
857
+ })
858
+ }).listen(port + 119)
859
+
860
+ var client = mqtt.connect(opts)
861
+ client.on('connect', function () {
862
+ client.subscribe('a/b', subOptions)
863
+ })
864
+ })
865
+
866
+ it('puback handling errors check', function (done) {
867
+ this.timeout(15000)
868
+ serverErr.listen(port + 117)
869
+ var opts = {
870
+ host: 'localhost',
871
+ port: port + 117,
872
+ protocolVersion: 5
873
+ }
874
+ var client = mqtt.connect(opts)
875
+ client.once('connect', () => {
876
+ client.publish('a/b', 'message', {qos: 1}, function (err, packet) {
877
+ should(err.message).be.equal('Publish error: Session taken over')
878
+ should(err.code).be.equal(142)
879
+ })
880
+ serverErr.close()
881
+ done()
882
+ })
883
+ })
884
+ it('pubrec handling errors check', function (done) {
885
+ this.timeout(15000)
886
+ serverErr.listen(port + 118)
887
+ var opts = {
888
+ host: 'localhost',
889
+ port: port + 118,
890
+ protocolVersion: 5
891
+ }
892
+ var client = mqtt.connect(opts)
893
+ client.once('connect', () => {
894
+ client.publish('a/b', 'message', {qos: 2}, function (err, packet) {
895
+ should(err.message).be.equal('Publish error: Session taken over')
896
+ should(err.code).be.equal(142)
897
+ })
898
+ serverErr.close()
899
+ done()
900
+ })
901
+ })
902
+ it('puback handling custom reason code', function (done) {
903
+ this.timeout(15000)
904
+ serverErr.listen(port + 117)
905
+ var opts = {
906
+ host: 'localhost',
907
+ port: port + 117,
908
+ protocolVersion: 5,
909
+ customHandleAcks: function (topic, message, packet, cb) {
910
+ var code = 0
911
+ if (topic === 'a/b') {
912
+ code = 128
913
+ }
914
+ cb(code)
915
+ }
916
+ }
917
+
918
+ serverErr.once('client', function (c) {
919
+ c.once('subscribe', function () {
920
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 1, messageId: 1 })
921
+ })
922
+
923
+ c.on('puback', function (packet) {
924
+ should(packet.reasonCode).be.equal(128)
925
+ client.end()
926
+ c.destroy()
927
+ serverErr.close()
928
+ done()
929
+ })
930
+ })
931
+
932
+ var client = mqtt.connect(opts)
933
+ client.once('connect', function () {
934
+ client.subscribe('a/b', {qos: 1})
935
+ })
936
+ })
937
+ it('server side disconnect', function (done) {
938
+ this.timeout(15000)
939
+ var server327 = new Server(function (client) {
940
+ client.on('connect', function (packet) {
941
+ client.connack({
942
+ reasonCode: 0
943
+ })
944
+ client.disconnect({reasonCode: 128})
945
+ server327.close()
946
+ })
947
+ })
948
+ server327.listen(port + 327)
949
+ var opts = {
950
+ host: 'localhost',
951
+ port: port + 327,
952
+ protocolVersion: 5
953
+ }
954
+
955
+ var client = mqtt.connect(opts)
956
+ client.once('disconnect', function (disconnectPacket) {
957
+ should(disconnectPacket.reasonCode).be.equal(128)
958
+ done()
959
+ })
960
+ })
961
+ it('pubrec handling custom reason code', function (done) {
962
+ this.timeout(15000)
963
+ serverErr.listen(port + 117)
964
+ var opts = {
965
+ host: 'localhost',
966
+ port: port + 117,
967
+ protocolVersion: 5,
968
+ customHandleAcks: function (topic, message, packet, cb) {
969
+ var code = 0
970
+ if (topic === 'a/b') {
971
+ code = 128
972
+ }
973
+ cb(code)
974
+ }
975
+ }
976
+
977
+ serverErr.once('client', function (c) {
978
+ c.once('subscribe', function () {
979
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 2, messageId: 1 })
980
+ })
981
+
982
+ c.on('pubrec', function (packet) {
983
+ should(packet.reasonCode).be.equal(128)
984
+ client.end()
985
+ c.destroy()
986
+ serverErr.close()
987
+ done()
988
+ })
989
+ })
990
+
991
+ var client = mqtt.connect(opts)
992
+ client.once('connect', function () {
993
+ client.subscribe('a/b', {qos: 1})
994
+ })
995
+ })
996
+ it('puback handling custom reason code with error', function (done) {
997
+ this.timeout(15000)
998
+ serverErr.listen(port + 117)
999
+ var opts = {
1000
+ host: 'localhost',
1001
+ port: port + 117,
1002
+ protocolVersion: 5,
1003
+ customHandleAcks: function (topic, message, packet, cb) {
1004
+ var code = 0
1005
+ if (topic === 'a/b') {
1006
+ cb(new Error('a/b is not valid'))
1007
+ }
1008
+ cb(code)
1009
+ }
1010
+ }
1011
+
1012
+ serverErr.once('client', function (c) {
1013
+ c.once('subscribe', function () {
1014
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 1, messageId: 1 })
1015
+ })
1016
+ })
1017
+
1018
+ var client = mqtt.connect(opts)
1019
+ client.on('error', function (error) {
1020
+ should(error.message).be.equal('a/b is not valid')
1021
+ client.end()
1022
+ serverErr.close()
1023
+ done()
1024
+ })
1025
+ client.once('connect', function () {
1026
+ client.subscribe('a/b', {qos: 1})
1027
+ })
1028
+ })
1029
+ it('pubrec handling custom reason code with error', function (done) {
1030
+ this.timeout(15000)
1031
+ serverErr.listen(port + 117)
1032
+ var opts = {
1033
+ host: 'localhost',
1034
+ port: port + 117,
1035
+ protocolVersion: 5,
1036
+ customHandleAcks: function (topic, message, packet, cb) {
1037
+ var code = 0
1038
+ if (topic === 'a/b') {
1039
+ cb(new Error('a/b is not valid'))
1040
+ }
1041
+ cb(code)
1042
+ }
1043
+ }
1044
+
1045
+ serverErr.once('client', function (c) {
1046
+ c.once('subscribe', function () {
1047
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 2, messageId: 1 })
1048
+ })
1049
+ })
1050
+
1051
+ var client = mqtt.connect(opts)
1052
+ client.on('error', function (error) {
1053
+ should(error.message).be.equal('a/b is not valid')
1054
+ client.end()
1055
+ serverErr.close()
1056
+ done()
1057
+ })
1058
+ client.once('connect', function () {
1059
+ client.subscribe('a/b', {qos: 1})
1060
+ })
1061
+ })
1062
+ it('puback handling custom invalid reason code', function (done) {
1063
+ this.timeout(15000)
1064
+ serverErr.listen(port + 117)
1065
+ var opts = {
1066
+ host: 'localhost',
1067
+ port: port + 117,
1068
+ protocolVersion: 5,
1069
+ customHandleAcks: function (topic, message, packet, cb) {
1070
+ var code = 0
1071
+ if (topic === 'a/b') {
1072
+ code = 124124
1073
+ }
1074
+ cb(code)
1075
+ }
1076
+ }
1077
+
1078
+ serverErr.once('client', function (c) {
1079
+ c.once('subscribe', function () {
1080
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 1, messageId: 1 })
1081
+ })
1082
+ })
1083
+
1084
+ var client = mqtt.connect(opts)
1085
+ client.on('error', function (error) {
1086
+ should(error.message).be.equal('Wrong reason code for puback')
1087
+ client.end()
1088
+ serverErr.close()
1089
+ done()
1090
+ })
1091
+ client.once('connect', function () {
1092
+ client.subscribe('a/b', {qos: 1})
1093
+ })
1094
+ })
1095
+ it('pubrec handling custom invalid reason code', function (done) {
1096
+ this.timeout(15000)
1097
+ serverErr.listen(port + 117)
1098
+ var opts = {
1099
+ host: 'localhost',
1100
+ port: port + 117,
1101
+ protocolVersion: 5,
1102
+ customHandleAcks: function (topic, message, packet, cb) {
1103
+ var code = 0
1104
+ if (topic === 'a/b') {
1105
+ code = 34535
1106
+ }
1107
+ cb(code)
1108
+ }
1109
+ }
1110
+
1111
+ serverErr.once('client', function (c) {
1112
+ c.once('subscribe', function () {
1113
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 2, messageId: 1 })
1114
+ })
1115
+ })
1116
+
1117
+ var client = mqtt.connect(opts)
1118
+ client.on('error', function (error) {
1119
+ should(error.message).be.equal('Wrong reason code for pubrec')
1120
+ client.end()
1121
+ serverErr.close()
1122
+ done()
1123
+ })
1124
+ client.once('connect', function () {
1125
+ client.subscribe('a/b', {qos: 1})
1126
+ })
1127
+ })
1128
+ })
1129
+ })