1 |
|
2 |
|
3 |
|
4 |
|
5 | var should = require('should')
|
6 | , stream = require('./util').testStream;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var Connection = require('../connection');
|
13 |
|
14 | module.exports = function() {
|
15 | beforeEach(function () {
|
16 | this.stream.removeAllListeners();
|
17 | });
|
18 |
|
19 | describe('#connect', function() {
|
20 | it('should send a connect packet (minimal)', function(done) {
|
21 | var expected = new Buffer([
|
22 | 16, 18,
|
23 | 0, 6, 77, 81, 73, 115, 100, 112,
|
24 | 3,
|
25 | 0,
|
26 | 0, 30,
|
27 | 0, 4,
|
28 | 116, 101, 115, 116
|
29 | ]);
|
30 |
|
31 | var fixture = {
|
32 | protocolId: 'MQIsdp',
|
33 | protocolVersion: 3,
|
34 | clientId: 'test',
|
35 | keepalive: 30,
|
36 | clean: false
|
37 | };
|
38 |
|
39 | this.conn.connect(fixture);
|
40 |
|
41 | var that = this;
|
42 | this.stream.on('readable', function() {
|
43 | var packet = that.stream.read();
|
44 | packet.should.eql(expected);
|
45 | done();
|
46 | });
|
47 | });
|
48 |
|
49 | it('should send a connect packet (maximal)', function(done) {
|
50 | var expected = new Buffer([
|
51 | 16, 54,
|
52 | 0, 6, 77, 81, 73, 115, 100, 112,
|
53 | 3,
|
54 | 246,
|
55 | 0, 30,
|
56 | 0, 4,
|
57 | 116, 101, 115, 116,
|
58 | 0, 5,
|
59 | 116, 111, 112, 105, 99,
|
60 | 0, 7,
|
61 | 112, 97, 121, 108, 111, 97, 100,
|
62 | 0, 8,
|
63 | 117, 115, 101, 114, 110, 97, 109, 101,
|
64 | 0, 8,
|
65 | 112, 97, 115, 115, 119, 111, 114, 100
|
66 | ]);
|
67 |
|
68 | var fixture = {
|
69 | protocolId: 'MQIsdp',
|
70 | protocolVersion: 3,
|
71 | clientId: 'test',
|
72 | keepalive: 30,
|
73 | will: {
|
74 | topic: 'topic',
|
75 | payload: 'payload',
|
76 | qos: 2,
|
77 | retain: true
|
78 | },
|
79 | clean: true,
|
80 | username: 'username',
|
81 | password: 'password'
|
82 | };
|
83 |
|
84 | this.conn.connect(fixture);
|
85 | var that = this;
|
86 | this.stream.once('readable', function() {
|
87 | that.stream.read(expected.length).should.eql(expected);
|
88 | done();
|
89 | });
|
90 | });
|
91 |
|
92 | it('should send a connect packet with binary username/password', function(done) {
|
93 | var expected = new Buffer([
|
94 | 16, 28,
|
95 | 0, 6, 77, 81, 73, 115, 100, 112,
|
96 | 3,
|
97 | 0x40 | 0x80,
|
98 | 0, 30,
|
99 | 0, 4,
|
100 | 116, 101, 115, 116,
|
101 | 0, 3,
|
102 | 12, 13, 14,
|
103 | 0, 3,
|
104 | 15, 16, 17
|
105 | ]);
|
106 |
|
107 | var fixture = {
|
108 | protocolId: 'MQIsdp',
|
109 | protocolVersion: 3,
|
110 | clientId: 'test',
|
111 | keepalive: 30,
|
112 | clean: false,
|
113 | username: new Buffer([12, 13, 14]),
|
114 | password: new Buffer([15, 16, 17])
|
115 | };
|
116 |
|
117 | var s = stream()
|
118 | var c = new Connection(s, { encoding: 'binary' });
|
119 |
|
120 | s.removeAllListeners();
|
121 | c.connect(fixture);
|
122 |
|
123 | s.on('readable', function() {
|
124 | var packet = s.read();
|
125 | packet.should.eql(expected);
|
126 | done();
|
127 | });
|
128 | });
|
129 |
|
130 | it('should send a connect packet with binary will payload', function(done) {
|
131 | var expected = new Buffer([
|
132 | 16, 50,
|
133 | 0, 6, 77, 81, 73, 115, 100, 112,
|
134 | 3,
|
135 | 246,
|
136 | 0, 30,
|
137 | 0, 4,
|
138 | 116, 101, 115, 116,
|
139 | 0, 5,
|
140 | 116, 111, 112, 105, 99,
|
141 | 0, 3,
|
142 | 18, 19, 20,
|
143 | 0, 8,
|
144 | 117, 115, 101, 114, 110, 97, 109, 101,
|
145 | 0, 8,
|
146 | 112, 97, 115, 115, 119, 111, 114, 100
|
147 | ]);
|
148 |
|
149 | var fixture = {
|
150 | protocolId: 'MQIsdp',
|
151 | protocolVersion: 3,
|
152 | clientId: 'test',
|
153 | keepalive: 30,
|
154 | will: {
|
155 | topic: 'topic',
|
156 | payload: new Buffer([18, 19, 20]),
|
157 | qos: 2,
|
158 | retain: true
|
159 | },
|
160 | clean: true,
|
161 | username: 'username',
|
162 | password: 'password'
|
163 | };
|
164 |
|
165 | var s = stream()
|
166 | var c = new Connection(s, { encoding: 'binary' });
|
167 |
|
168 | s.removeAllListeners();
|
169 | c.connect(fixture);
|
170 |
|
171 | s.on('readable', function() {
|
172 | var packet = s.read();
|
173 | packet.should.eql(expected);
|
174 | done();
|
175 | });
|
176 | });
|
177 |
|
178 | it('should send a connect packet with unicode will payload', function(done) {
|
179 | var expected = new Buffer([
|
180 | 16, 49,
|
181 | 0, 6, 77, 81, 73, 115, 100, 112,
|
182 | 3,
|
183 | 246,
|
184 | 0, 30,
|
185 | 0, 4,
|
186 | 116, 101, 115, 116,
|
187 | 0, 5,
|
188 | 116, 111, 112, 105, 99,
|
189 | 0, 2,
|
190 | 194, 167,
|
191 | 0, 8,
|
192 | 117, 115, 101, 114, 110, 97, 109, 101,
|
193 | 0, 8,
|
194 | 112, 97, 115, 115, 119, 111, 114, 100
|
195 | ]);
|
196 |
|
197 | var fixture = {
|
198 | protocolId: 'MQIsdp',
|
199 | protocolVersion: 3,
|
200 | clientId: 'test',
|
201 | keepalive: 30,
|
202 | will: {
|
203 | topic: 'topic',
|
204 | payload: '§',
|
205 | qos: 2,
|
206 | retain: true
|
207 | },
|
208 | clean: true,
|
209 | username: 'username',
|
210 | password: 'password'
|
211 | };
|
212 |
|
213 |
|
214 | var s = stream()
|
215 | var c = new Connection(s, { encoding: 'binary' });
|
216 |
|
217 | s.removeAllListeners();
|
218 | c.connect(fixture);
|
219 |
|
220 | s.on('readable', function() {
|
221 | var packet = s.read();
|
222 | packet.should.eql(expected);
|
223 | done();
|
224 | });
|
225 | });
|
226 |
|
227 | describe('invalid options', function () {
|
228 | describe('protocol id', function () {
|
229 | it('should reject non-string', function(done) {
|
230 | var fixture = {
|
231 | protocolId: 42,
|
232 | protocolVersion: 3,
|
233 | clientId: 'test',
|
234 | keepalive: 30
|
235 | }
|
236 |
|
237 | var expectedErr = 'Invalid protocol id';
|
238 |
|
239 | this.conn.once('error', function(error) {
|
240 | error.message.should.equal(expectedErr);
|
241 | done();
|
242 | });
|
243 |
|
244 | this.conn.connect(fixture);
|
245 | });
|
246 | });
|
247 |
|
248 | describe('protocol version', function() {
|
249 | it('should reject non-number', function(done) {
|
250 | var fixture = {
|
251 | protocolId: 'MQIsdp',
|
252 | protocolVersion: [],
|
253 | clientId: 'test',
|
254 | keepalive: 30
|
255 | };
|
256 |
|
257 | var expectedErr = 'Invalid protocol version';
|
258 |
|
259 | this.conn.once('error', function(error) {
|
260 | error.message.should.equal(expectedErr);
|
261 | done();
|
262 | });
|
263 |
|
264 | this.conn.connect(fixture);
|
265 | });
|
266 |
|
267 | it('should reject >255', function(done) {
|
268 | var fixture = {
|
269 | protocolId: 'MQIsdp',
|
270 | protocolVersion: 300,
|
271 | clientId: 'test',
|
272 | keepalive: 30
|
273 | };
|
274 |
|
275 | var expectedErr = 'Invalid protocol version';
|
276 |
|
277 | this.conn.once('error', function(error) {
|
278 | error.message.should.equal(expectedErr);
|
279 | done();
|
280 | });
|
281 |
|
282 | this.conn.connect(fixture);
|
283 | });
|
284 |
|
285 | it('should reject <0', function(done) {
|
286 | var fixture = {
|
287 | protocolId: 'MQIsdp',
|
288 | protocolVersion: -20,
|
289 | clientId: 'test',
|
290 | keepalive: 30
|
291 | };
|
292 |
|
293 | var expectedErr = 'Invalid protocol version';
|
294 |
|
295 | this.conn.once('error', function(error) {
|
296 | error.message.should.equal(expectedErr);
|
297 | done();
|
298 | });
|
299 |
|
300 | this.conn.connect(fixture);
|
301 | });
|
302 | });
|
303 |
|
304 | describe('client id', function() {
|
305 | it('should reject non-present', function(done) {
|
306 | var fixture = {
|
307 | protocolId: 'MQIsdp',
|
308 | protocolVersion: 3,
|
309 | keepalive: 30
|
310 | };
|
311 |
|
312 | var expectedErr = 'Invalid client id';
|
313 |
|
314 | this.conn.once('error', function(error) {
|
315 | error.message.should.equal(expectedErr);
|
316 | done();
|
317 | });
|
318 |
|
319 | this.conn.connect(fixture);
|
320 | });
|
321 |
|
322 | it('should reject empty', function(done) {
|
323 | var fixture = {
|
324 | protocolId: 'MQIsdp',
|
325 | protocolVersion: 3,
|
326 | clientId: '',
|
327 | keepalive: 30
|
328 | };
|
329 |
|
330 | var expectedErr = 'Invalid client id';
|
331 |
|
332 | this.conn.once('error', function(error) {
|
333 | error.message.should.equal(expectedErr);
|
334 | done();
|
335 | });
|
336 |
|
337 | this.conn.connect(fixture);
|
338 | });
|
339 |
|
340 | it('should reject non-string', function(done) {
|
341 | var fixture = {
|
342 | protocolId: 'MQIsdp',
|
343 | protocolVersion: 3,
|
344 | clientId: {},
|
345 | keepalive: 30
|
346 | };
|
347 |
|
348 | var expectedErr = 'Invalid client id';
|
349 |
|
350 | this.conn.once('error', function(error) {
|
351 | error.message.should.equal(expectedErr);
|
352 | done();
|
353 | });
|
354 |
|
355 | this.conn.connect(fixture);
|
356 | });
|
357 | });
|
358 |
|
359 | describe('keepalive', function() {
|
360 | it('should reject non-number', function(done) {
|
361 | var fixture = {
|
362 | protocolId: 'MQIsdp',
|
363 | protocolVersion: 3,
|
364 | clientId: 'test',
|
365 | keepalive: 'blah'
|
366 | };
|
367 |
|
368 | var expectedErr = 'Invalid keepalive';
|
369 |
|
370 | this.conn.once('error', function(error) {
|
371 | error.message.should.equal(expectedErr);
|
372 | done();
|
373 | });
|
374 |
|
375 | this.conn.connect(fixture);
|
376 | });
|
377 |
|
378 | it('should reject < 0', function(done) {
|
379 | var fixture = {
|
380 | protocolId: 'MQIsdp',
|
381 | protocolVersion: 3,
|
382 | clientId: 'test',
|
383 | keepalive: -2
|
384 | };
|
385 |
|
386 | var expectedErr = 'Invalid keepalive';
|
387 |
|
388 | this.conn.once('error', function(error) {
|
389 | error.message.should.equal(expectedErr);
|
390 | done();
|
391 | });
|
392 |
|
393 | this.conn.connect(fixture);
|
394 |
|
395 | });
|
396 |
|
397 | it('should reject > 65535', function(done) {
|
398 | var fixture = {
|
399 | protocolId: 'MQIsdp',
|
400 | protocolVersion: 3,
|
401 | clientId: 'test',
|
402 | keepalive: 65536
|
403 | };
|
404 |
|
405 | var expectedErr = 'Invalid keepalive';
|
406 |
|
407 | this.conn.once('error', function(error) {
|
408 | error.message.should.equal(expectedErr);
|
409 | done();
|
410 | });
|
411 |
|
412 | this.conn.connect(fixture);
|
413 |
|
414 | });
|
415 | });
|
416 |
|
417 | describe('will', function() {
|
418 | it('should reject non-object', function(done) {
|
419 | var fixture = {
|
420 | protocolId: 'MQIsdp',
|
421 | protocolVersion: 3,
|
422 | clientId: 'test',
|
423 | keepalive: 30,
|
424 | will: 'test'
|
425 | };
|
426 |
|
427 | var expectedErr = 'Invalid will';
|
428 |
|
429 | this.conn.once('error', function(error) {
|
430 | error.message.should.equal(expectedErr);
|
431 | done();
|
432 | });
|
433 |
|
434 | this.conn.connect(fixture);
|
435 |
|
436 | });
|
437 |
|
438 | it('should reject will without valid topic',
|
439 | function(done) {
|
440 | var fixture = {
|
441 | protocolId: 'MQIsdp',
|
442 | protocolVersion: 3,
|
443 | clientId: 'test',
|
444 | keepalive: 30,
|
445 | will: {
|
446 | topic: 0,
|
447 | payload: 'test',
|
448 | qos: 0,
|
449 | retain: false
|
450 | }
|
451 | };
|
452 |
|
453 | var expectedErr = 'Invalid will topic';
|
454 |
|
455 | this.conn.once('error', function(error) {
|
456 | error.message.should.equal(expectedErr);
|
457 | done();
|
458 | });
|
459 |
|
460 | this.conn.connect(fixture);
|
461 | });
|
462 |
|
463 | it('should reject will without valid payload',
|
464 | function(done) {
|
465 | var fixture = {
|
466 | protocolId: 'MQIsdp',
|
467 | protocolVersion: 3,
|
468 | clientId: 'test',
|
469 | keepalive: 30,
|
470 | will: {
|
471 | topic: 'test',
|
472 | payload: 42,
|
473 | qos: 0,
|
474 | retain: false
|
475 | }
|
476 | };
|
477 |
|
478 | var expectedErr = 'Invalid will payload';
|
479 |
|
480 | this.conn.once('error', function(error) {
|
481 | error.message.should.equal(expectedErr);
|
482 | done();
|
483 | });
|
484 |
|
485 | this.conn.connect(fixture);
|
486 | });
|
487 |
|
488 | it.skip('should reject will with invalid qos', function(done) {
|
489 | var fixture = {
|
490 | protocolId: 'MQIsdp',
|
491 | protocolVersion: 3,
|
492 | clientId: 'test',
|
493 | keepalive: 30,
|
494 | will: {
|
495 | topic: 'test',
|
496 | payload: 'test',
|
497 | qos: '',
|
498 | retain: false
|
499 | }
|
500 | };
|
501 |
|
502 | var expectedErr = 'Invalid will qos';
|
503 |
|
504 | this.conn.once('error', function(error) {
|
505 | error.message.should.equal(expectedErr);
|
506 | done();
|
507 | });
|
508 |
|
509 | this.conn.connect(fixture);
|
510 | });
|
511 | });
|
512 |
|
513 | describe('username', function() {
|
514 | it('should reject invalid username', function(done) {
|
515 | var fixture = {
|
516 | protocolId: 'MQIsdp',
|
517 | protocolVersion: 3,
|
518 | clientId: 'test',
|
519 | keepalive: 30,
|
520 | username: 30
|
521 | };
|
522 |
|
523 | var expectedErr = 'Invalid username';
|
524 |
|
525 | this.conn.once('error', function(error) {
|
526 | error.message.should.equal(expectedErr);
|
527 | done();
|
528 | });
|
529 |
|
530 | this.conn.connect(fixture);
|
531 | });
|
532 | });
|
533 |
|
534 | describe('password', function() {
|
535 | it('should reject invalid password', function(done) {
|
536 | var fixture = {
|
537 | protocolId: 'MQIsdp',
|
538 | protocolVersion: 3,
|
539 | clientId: 'test',
|
540 | keepalive: 30,
|
541 | password: 30
|
542 | };
|
543 |
|
544 | var expectedErr = 'Invalid password';
|
545 |
|
546 | this.conn.once('error', function(error) {
|
547 | error.message.should.equal(expectedErr);
|
548 | done();
|
549 | });
|
550 |
|
551 | this.conn.connect(fixture);
|
552 | });
|
553 | });
|
554 | });
|
555 | });
|
556 |
|
557 | describe('#connack', function() {
|
558 | it('should send a connack packet (rc = 0)', function(done) {
|
559 | var expected = new Buffer([
|
560 | 32, 2,
|
561 | 0, 0
|
562 | ]);
|
563 |
|
564 | var fixture = {
|
565 | returnCode: 0
|
566 | };
|
567 |
|
568 | this.conn.connack(fixture);
|
569 | var that = this;
|
570 | this.stream.once('readable', function() {
|
571 | that.stream.read(expected.length).should.eql(expected);
|
572 | done();
|
573 | });
|
574 | });
|
575 |
|
576 | it('should send a connack packet (rc = 4)', function(done) {
|
577 | var expected = new Buffer([
|
578 | 32, 2,
|
579 | 0, 4
|
580 | ]);
|
581 |
|
582 | var fixture = {
|
583 | returnCode: 4
|
584 | };
|
585 |
|
586 | this.conn.connack(fixture);
|
587 | var that = this;
|
588 | this.stream.once('readable', function() {
|
589 | that.stream.read(expected.length).should.eql(expected);
|
590 | done();
|
591 | });
|
592 | });
|
593 |
|
594 | it('should reject invalid rc', function (done) {
|
595 | this.conn.once('error', function(error) {
|
596 | error.message.should.equal('Invalid return code');
|
597 | done();
|
598 | });
|
599 | this.conn.connack({returnCode: 'asdf'});
|
600 | });
|
601 | });
|
602 |
|
603 | describe('#publish', function() {
|
604 | it('should send a publish packet (minimal)', function(done) {
|
605 | var expected = new Buffer([
|
606 | 48, 10,
|
607 | 0, 4,
|
608 | 116, 101, 115, 116,
|
609 | 116, 101, 115, 116,
|
610 | ]);
|
611 |
|
612 | var fixture = {
|
613 | topic: 'test',
|
614 | payload: 'test'
|
615 | };
|
616 |
|
617 | this.conn.publish(fixture);
|
618 | var that = this;
|
619 | this.stream.once('readable', function() {
|
620 | that.stream.read(expected.length).should.eql(expected);
|
621 | done();
|
622 | });
|
623 | });
|
624 |
|
625 | it('should send a publish packet (maximal)', function (done) {
|
626 | var expected = new Buffer([
|
627 | 61, 12,
|
628 | 0, 4,
|
629 | 116, 101, 115, 116,
|
630 | 0, 7,
|
631 | 116, 101, 115, 116,
|
632 | ]);
|
633 |
|
634 | var fixture = {
|
635 | topic: 'test',
|
636 | payload: 'test',
|
637 | qos: 2,
|
638 | retain: true,
|
639 | dup: true,
|
640 | messageId: 7
|
641 | };
|
642 |
|
643 | this.conn.publish(fixture);
|
644 | var that = this;
|
645 | this.stream.once('readable', function() {
|
646 | that.stream.read(expected.length).should.eql(expected);
|
647 | done();
|
648 | });
|
649 | });
|
650 |
|
651 | it('should send a publish packet (empty)', function(done) {
|
652 | var expected = new Buffer([
|
653 | 48, 6,
|
654 | 0, 4,
|
655 | 116, 101, 115, 116
|
656 |
|
657 | ]);
|
658 |
|
659 | var fixture = {
|
660 | topic: 'test'
|
661 | };
|
662 |
|
663 | this.conn.publish(fixture);
|
664 | var that = this;
|
665 | this.stream.once('readable', function() {
|
666 | that.stream.read(expected.length).should.eql(expected);
|
667 | done();
|
668 | });
|
669 | });
|
670 |
|
671 | it('should send a publish packet (buffer)', function(done) {
|
672 | var expected = new Buffer([
|
673 | 48, 10,
|
674 | 0, 4,
|
675 | 116, 101, 115, 116,
|
676 | 0, 0, 0, 0
|
677 | ]);
|
678 | var buf = new Buffer(4);
|
679 | buf.fill(0);
|
680 |
|
681 | var fixture = {
|
682 | topic: 'test',
|
683 | payload: buf
|
684 | }
|
685 |
|
686 | this.conn.publish(fixture);
|
687 | var that = this;
|
688 | this.stream.once('readable', function() {
|
689 | that.stream.read(expected.length).should.eql(expected);
|
690 | done();
|
691 | });
|
692 | });
|
693 |
|
694 | it('should send a publish packet of 2 KB', function(done) {
|
695 | var expected = new Buffer([
|
696 | 48, 134, 16,
|
697 | 0, 4,
|
698 | 116, 101, 115, 116
|
699 | ]);
|
700 | var payload = new Buffer(2048);
|
701 |
|
702 | expected = Buffer.concat([expected, payload]);
|
703 |
|
704 | var fixture = {
|
705 | topic: 'test',
|
706 | payload: payload
|
707 | };
|
708 |
|
709 | this.stream.on('readable', function() {
|
710 | var data = this.read();
|
711 | data.toString('hex').should.eql(expected.toString('hex'));
|
712 | done();
|
713 | });
|
714 |
|
715 | this.conn.publish(fixture);
|
716 | this.conn.end();
|
717 | });
|
718 |
|
719 | it('should send a publish packet of 2 MB', function(done) {
|
720 | var expected = new Buffer([
|
721 | 48, 134, 128, 128, 1,
|
722 | 0, 4,
|
723 | 116, 101, 115, 116
|
724 | ]);
|
725 | var payload = new Buffer(2 * 1024 * 1024);
|
726 |
|
727 | expected = Buffer.concat([expected, payload]);
|
728 |
|
729 | var fixture = {
|
730 | topic: 'test',
|
731 | payload: payload
|
732 | };
|
733 |
|
734 | this.conn.publish(fixture);
|
735 | this.conn.end();
|
736 |
|
737 | this.stream.on('readable', function() {
|
738 | var data = this.read();
|
739 | data.toString('hex').should.eql(expected.toString('hex'));
|
740 | done();
|
741 | });
|
742 | });
|
743 |
|
744 | it('should reject invalid topic', function (done) {
|
745 | var error = "Invalid topic";
|
746 |
|
747 | this.conn.once('error', function(err) {
|
748 | err.message.should.equal(error);
|
749 | done();
|
750 | });
|
751 | this.conn.publish({topic: 0});
|
752 | });
|
753 | it('should reject invalid payloads, maybe');
|
754 | it('should reject invalid mid', function(done) {
|
755 | this.conn.once('error', function(err) {
|
756 | err.message.should.equal('Invalid message id');
|
757 | done();
|
758 | });
|
759 | this.conn.publish({topic: 'test', messageId: '', qos:1});
|
760 | });
|
761 | });
|
762 |
|
763 | describe('#puback', function() {
|
764 | it('should send a puback packet', function(done) {
|
765 | var expected = new Buffer([
|
766 | 64, 2,
|
767 | 0, 30
|
768 | ]);
|
769 |
|
770 | var fixture = {
|
771 | messageId: 30
|
772 | };
|
773 |
|
774 | this.conn.puback(fixture);
|
775 | var that = this;
|
776 | this.stream.once('readable', function() {
|
777 | that.stream.read(expected.length).should.eql(expected);
|
778 | done();
|
779 | });
|
780 | });
|
781 |
|
782 | it('should reject invalid mid', function (done) {
|
783 | this.conn.once('error', function(error) {
|
784 | error.message.should.equal('Invalid message id');
|
785 | done();
|
786 | });
|
787 | this.conn.puback({messageId: ''});
|
788 | });
|
789 | });
|
790 |
|
791 | describe('#pubrec', function() {
|
792 | it('should send a pubrec packet', function(done) {
|
793 | var expected = new Buffer([
|
794 | 80, 2,
|
795 | 0, 3
|
796 | ]);
|
797 |
|
798 | var fixture = {
|
799 | messageId: 3
|
800 | };
|
801 |
|
802 | this.conn.pubrec(fixture);
|
803 | var that = this;
|
804 | this.stream.once('readable', function() {
|
805 | that.stream.read(expected.length).should.eql(expected);
|
806 | done();
|
807 | });
|
808 | });
|
809 |
|
810 | it('should reject invalid mid');
|
811 | });
|
812 |
|
813 | describe('#pubrel', function() {
|
814 | it('should send a pubrel packet', function(done) {
|
815 | var expected = new Buffer([
|
816 | 98, 2,
|
817 | 0, 6
|
818 | ]);
|
819 |
|
820 | var fixture = {
|
821 | messageId: 6
|
822 | };
|
823 |
|
824 | this.conn.pubrel(fixture);
|
825 | var that = this;
|
826 | this.stream.once('readable', function() {
|
827 | that.stream.read(expected.length).should.eql(expected);
|
828 | done();
|
829 | });
|
830 | });
|
831 |
|
832 | it('should reject invalid mid');
|
833 | });
|
834 |
|
835 | describe('#pubcomp', function() {
|
836 | it('should send a pubcomp packet', function(done) {
|
837 | var expected = new Buffer([
|
838 | 116, 2,
|
839 | 0, 9
|
840 | ]);
|
841 |
|
842 | var fixture = {
|
843 | messageId: 9
|
844 | };
|
845 |
|
846 | this.conn.pubcomp(fixture);
|
847 | var that = this;
|
848 | this.stream.once('readable', function() {
|
849 | that.stream.read(expected.length).should.eql(expected);
|
850 | done();
|
851 | });
|
852 | });
|
853 |
|
854 | it('should reject invalid mid');
|
855 | });
|
856 |
|
857 | describe('#subscribe', function() {
|
858 | it('should send a subscribe packet (single)', function(done) {
|
859 | var expected = new Buffer([
|
860 | 130, 9,
|
861 | 0, 7,
|
862 | 0, 4,
|
863 | 116, 101, 115, 116,
|
864 | 0
|
865 | ]);
|
866 |
|
867 | var fixture = {
|
868 | messageId: 7,
|
869 | subscriptions: [
|
870 | {
|
871 | topic: 'test',
|
872 | qos: 0
|
873 | }
|
874 | ]
|
875 | };
|
876 |
|
877 | this.conn.subscribe(fixture);
|
878 | var that = this;
|
879 | this.stream.once('readable', function() {
|
880 | that.stream.read(expected.length).should.eql(expected);
|
881 | done();
|
882 | });
|
883 | });
|
884 |
|
885 | it('should send subscribe packet (multiple)', function(done) {
|
886 | var expected = new Buffer([
|
887 | 130, 23,
|
888 | 0, 8,
|
889 | 0, 4,
|
890 | 116, 101, 115, 116,
|
891 | 0,
|
892 | 0, 4,
|
893 | 117, 101, 115, 116,
|
894 | 1,
|
895 | 0, 4,
|
896 | 116, 101, 115, 115,
|
897 | 2
|
898 | ]);
|
899 |
|
900 | var fixture = {
|
901 | messageId: 8,
|
902 | subscriptions: [
|
903 | {
|
904 | topic: 'test',
|
905 | qos: 0
|
906 | },{
|
907 | topic: 'uest',
|
908 | qos: 1
|
909 | },{
|
910 | topic: 'tess',
|
911 | qos: 2
|
912 | }
|
913 | ]
|
914 | };
|
915 |
|
916 | this.conn.subscribe(fixture);
|
917 | var that = this;
|
918 | this.stream.once('readable', function() {
|
919 | that.stream.read(expected.length).should.eql(expected);
|
920 | done();
|
921 | });
|
922 | });
|
923 | it('should reject invalid subscriptions', function (done) {
|
924 | this.conn.once('error', function(error) {
|
925 | error.message.should.equal('Invalid subscriptions');
|
926 | done();
|
927 | });
|
928 | this.conn.subscribe({
|
929 | messageId: 1, subscriptions: ''
|
930 | });
|
931 | });
|
932 |
|
933 | it('should reject invalid subscription objects');
|
934 | it('should reject invalid mid', function (done) {
|
935 | this.conn.once('error', function(error) {
|
936 | error.message.should.equal('Invalid message id');
|
937 | done();
|
938 | });
|
939 | this.conn.subscribe({
|
940 | messageId: '', subscriptions:[{topic: 'test', qos: 1}]
|
941 | });
|
942 | });
|
943 | });
|
944 |
|
945 | describe('#suback', function() {
|
946 | it('should send a suback packet', function(done) {
|
947 | var expected = new Buffer([
|
948 | 144, 5,
|
949 | 0, 4,
|
950 | 0,
|
951 | 1,
|
952 | 2,
|
953 | ]);
|
954 |
|
955 | var fixture = {
|
956 | granted: [0, 1, 2],
|
957 | messageId: 4
|
958 | };
|
959 |
|
960 | this.conn.suback(fixture);
|
961 | var that = this;
|
962 | this.stream.once('readable', function() {
|
963 | that.stream.read(expected.length).should.eql(expected);
|
964 | done();
|
965 | });
|
966 | });
|
967 |
|
968 | it('should reject invalid mid');
|
969 | it('should reject invalid qos vector', function (done) {
|
970 | this.conn.on('error', function(error) {
|
971 | error.message.should.equal('Invalid qos vector');
|
972 | done();
|
973 | });
|
974 | this.conn.suback({granted: '', messageId: 1});
|
975 | });
|
976 | });
|
977 |
|
978 | describe('#unsubscribe', function() {
|
979 | it('should send an unsubscribe packet', function(done) {
|
980 | var expected = new Buffer([
|
981 | 162, 14,
|
982 | 0, 6,
|
983 | 0, 4,
|
984 | 116, 101, 115, 116,
|
985 | 0, 4,
|
986 | 116, 115, 101, 116
|
987 | ]);
|
988 |
|
989 | var fixture = {
|
990 | messageId: 6,
|
991 | unsubscriptions: [
|
992 | 'test', 'tset'
|
993 | ]
|
994 | };
|
995 |
|
996 | this.conn.unsubscribe(fixture);
|
997 | var that = this;
|
998 | this.stream.once('readable', function() {
|
999 | that.stream.read(expected.length).should.eql(expected);
|
1000 | done();
|
1001 | });
|
1002 | });
|
1003 |
|
1004 | it('should reject invalid unsubs', function (done) {
|
1005 | this.conn.once('error', function(error) {
|
1006 | error.message.should.equal('Invalid unsubscriptions');
|
1007 | done();
|
1008 | });
|
1009 | this.conn.unsubscribe({
|
1010 | messageId: 1,
|
1011 | unsubscriptions: ''
|
1012 | });
|
1013 | });
|
1014 | it('should reject invalid mids');
|
1015 | });
|
1016 |
|
1017 | describe('#unsuback', function() {
|
1018 | it('should send a unsuback packet', function(done) {
|
1019 | var expected = new Buffer([
|
1020 | 176, 2,
|
1021 | 0, 8
|
1022 | ]);
|
1023 |
|
1024 | var fixture = {
|
1025 | messageId: 8
|
1026 | };
|
1027 |
|
1028 | this.conn.unsuback(fixture);
|
1029 | var that = this;
|
1030 | this.stream.once('readable', function() {
|
1031 | that.stream.read(expected.length).should.eql(expected);
|
1032 | done();
|
1033 | });
|
1034 | });
|
1035 |
|
1036 | it('should reject invalid mid');
|
1037 | });
|
1038 |
|
1039 | describe('#pingreq', function() {
|
1040 | it('should send a pingreq packet', function(done) {
|
1041 | var expected = new Buffer([
|
1042 | 192, 0
|
1043 | ]);
|
1044 |
|
1045 | var fixture = {
|
1046 | };
|
1047 |
|
1048 | this.conn.pingreq(fixture);
|
1049 | var that = this;
|
1050 | this.stream.once('readable', function() {
|
1051 | that.stream.read(expected.length).should.eql(expected);
|
1052 | done();
|
1053 | });
|
1054 | });
|
1055 | });
|
1056 |
|
1057 | describe('#pingresp', function() {
|
1058 | it('should send a pingresp packet', function(done) {
|
1059 | var expected = new Buffer([
|
1060 | 208, 0
|
1061 | ]);
|
1062 |
|
1063 | var fixture = {
|
1064 | };
|
1065 |
|
1066 | this.conn.pingresp(fixture);
|
1067 | var that = this;
|
1068 | this.stream.once('readable', function() {
|
1069 | that.stream.read(expected.length).should.eql(expected);
|
1070 | done();
|
1071 | });
|
1072 | });
|
1073 | });
|
1074 |
|
1075 | describe('#disconnect', function() {
|
1076 | it('should send a disconnect packet', function(done) {
|
1077 | var expected = new Buffer([
|
1078 | 224, 0
|
1079 | ]);
|
1080 |
|
1081 | var fixture = {
|
1082 | };
|
1083 |
|
1084 | this.conn.disconnect(fixture);
|
1085 | var that = this;
|
1086 | this.stream.once('readable', function() {
|
1087 | that.stream.read(expected.length).should.eql(expected);
|
1088 | done();
|
1089 | });
|
1090 | });
|
1091 |
|
1092 | it('should send a null disconnect packet', function(done) {
|
1093 | var expected = new Buffer([
|
1094 | 224, 0
|
1095 | ]);
|
1096 |
|
1097 | this.conn.disconnect();
|
1098 | var that = this;
|
1099 | this.stream.once('readable', function() {
|
1100 | that.stream.read(expected.length).should.eql(expected);
|
1101 | done();
|
1102 | });
|
1103 | });
|
1104 | });
|
1105 | };
|