1 |
|
2 |
|
3 |
|
4 | var should = require('should')
|
5 | , stream = require('./util').testStream;
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var Connection = require('../connection');
|
11 |
|
12 | module.exports = function() {
|
13 |
|
14 | describe('connect', function() {
|
15 | it('should fire a connect event (minimal)', function(done) {
|
16 | var expected = {
|
17 | cmd: "connect",
|
18 | retain: false,
|
19 | qos: 0,
|
20 | dup: false,
|
21 | length: 18,
|
22 | protocolId: "MQIsdp",
|
23 | protocolVersion: 3,
|
24 | clean: false,
|
25 | keepalive: 30,
|
26 | clientId: "test"
|
27 | };
|
28 |
|
29 | var fixture = [
|
30 | 16, 18,
|
31 | 0, 6,
|
32 | 77, 81, 73, 115, 100, 112,
|
33 | 3,
|
34 | 0,
|
35 | 0, 30,
|
36 | 0, 4,
|
37 | 116, 101, 115, 116
|
38 | ];
|
39 |
|
40 | this.stream.write(new Buffer(fixture));
|
41 |
|
42 | this.conn.once('connect', function(packet) {
|
43 | packet.should.eql(expected);
|
44 | done();
|
45 | });
|
46 |
|
47 | });
|
48 |
|
49 | it('should fire a connect event (maximal)', function(done) {
|
50 | var expected = {
|
51 | cmd: "connect",
|
52 | retain: false,
|
53 | qos: 0,
|
54 | dup: false,
|
55 | length: 54,
|
56 | protocolId: "MQIsdp",
|
57 | protocolVersion: 3,
|
58 | will: {
|
59 | retain: true,
|
60 | qos: 2,
|
61 | topic: "topic",
|
62 | payload: new Buffer("payload")
|
63 | },
|
64 | clean: true,
|
65 | keepalive: 30,
|
66 | clientId: "test",
|
67 | username: "username",
|
68 | password: new Buffer("password")
|
69 | };
|
70 | var fixture = [
|
71 | 16, 54,
|
72 | 0, 6,
|
73 | 77, 81, 73, 115, 100, 112,
|
74 | 3,
|
75 | 246,
|
76 | 0, 30,
|
77 | 0, 4,
|
78 | 116, 101, 115, 116,
|
79 | 0, 5,
|
80 | 116, 111, 112, 105, 99,
|
81 | 0, 7,
|
82 | 112, 97, 121, 108, 111, 97, 100,
|
83 | 0, 8,
|
84 | 117, 115, 101, 114, 110, 97, 109, 101,
|
85 | 0, 8,
|
86 | 112, 97, 115, 115, 119, 111, 114, 100
|
87 | ];
|
88 |
|
89 | this.stream.write(new Buffer(fixture));
|
90 |
|
91 | this.conn.once('connect', function(packet) {
|
92 | packet.should.eql(expected);
|
93 | done();
|
94 | });
|
95 | });
|
96 |
|
97 | describe('parse errors', function() {
|
98 | it('should say protocol not parseable', function(done) {
|
99 | var fixture = [
|
100 | 16, 4,
|
101 | 0, 6,
|
102 | 77, 81
|
103 | ];
|
104 |
|
105 | this.stream.write(new Buffer(fixture));
|
106 | this.conn.once('error', function(err) {
|
107 | err.message.should.match(/cannot parse protocol id/);
|
108 | done();
|
109 | });
|
110 | });
|
111 | });
|
112 | });
|
113 |
|
114 | describe('connack', function() {
|
115 | it('should fire a connack event (rc = 0)', function(done) {
|
116 | var expected = {
|
117 | cmd: 'connack',
|
118 | retain: false,
|
119 | qos: 0,
|
120 | dup: false,
|
121 | length: 2,
|
122 | returnCode: 0
|
123 | }
|
124 |
|
125 | var fixture = [32, 2, 0, 0];
|
126 |
|
127 | this.stream.write(new Buffer(fixture));
|
128 |
|
129 | this.conn.once('connack', function(packet) {
|
130 | packet.should.eql(expected);
|
131 | done();
|
132 | });
|
133 | });
|
134 |
|
135 | it('should fire a connack event (rc = 5)', function(done) {
|
136 | var expected = {
|
137 | cmd: 'connack',
|
138 | retain: false,
|
139 | qos: 0,
|
140 | dup: false,
|
141 | length: 2,
|
142 | returnCode: 5
|
143 | }
|
144 |
|
145 | var fixture = [32, 2, 0, 5];
|
146 |
|
147 | this.stream.write(new Buffer(fixture));
|
148 |
|
149 | this.conn.once('connack', function(packet) {
|
150 | packet.should.eql(expected);
|
151 | done();
|
152 | });
|
153 | });
|
154 | });
|
155 |
|
156 | describe('publish', function() {
|
157 | it('should fire a publish event (minimal)', function(done) {
|
158 | var expected = {
|
159 | cmd: "publish",
|
160 | retain: false,
|
161 | qos: 0,
|
162 | dup: false,
|
163 | length: 10,
|
164 | topic: "test",
|
165 | payload: new Buffer("test")
|
166 | };
|
167 |
|
168 | var fixture = [
|
169 | 48, 10,
|
170 | 0, 4,
|
171 | 116, 101, 115, 116,
|
172 | 116, 101, 115, 116
|
173 | ];
|
174 |
|
175 | this.stream.write(new Buffer(fixture));
|
176 |
|
177 | this.conn.once('publish', function(packet) {
|
178 | packet.should.eql(expected);
|
179 | done();
|
180 | });
|
181 | });
|
182 |
|
183 | it('should fire a publish event with 2KB payload', function(done) {
|
184 | var expected = {
|
185 | cmd: "publish",
|
186 | retain: false,
|
187 | qos: 0,
|
188 | dup: false,
|
189 | length: 2054,
|
190 | topic: "test",
|
191 | payload: new Buffer(2048)
|
192 | };
|
193 |
|
194 | var fixture = new Buffer([
|
195 | 48, 134, 16,
|
196 | 0, 4,
|
197 | 116, 101, 115, 116,
|
198 | ]);
|
199 |
|
200 | fixture = Buffer.concat([fixture, expected.payload]);
|
201 |
|
202 | var s = stream()
|
203 | var c = new Connection(s);
|
204 |
|
205 | s.write(fixture);
|
206 |
|
207 | c.once('publish', function(packet) {
|
208 | packet.should.eql(expected);
|
209 | done();
|
210 | });
|
211 | });
|
212 |
|
213 | it('should fire a publish event with 2MB payload', function(done) {
|
214 | var expected = {
|
215 | cmd: "publish",
|
216 | retain: false,
|
217 | qos: 0,
|
218 | dup: false,
|
219 | length: 6 + 2 * 1024 * 1024,
|
220 | topic: "test",
|
221 | payload: new Buffer(2 * 1024 * 1024)
|
222 | };
|
223 |
|
224 | var fixture = new Buffer([
|
225 | 48, 134, 128, 128, 1,
|
226 | 0, 4,
|
227 | 116, 101, 115, 116,
|
228 | ]);
|
229 |
|
230 | fixture = Buffer.concat([fixture, expected.payload]);
|
231 |
|
232 | var s = stream()
|
233 | var c = new Connection(s);
|
234 |
|
235 | s.write(fixture);
|
236 |
|
237 | c.once('publish', function(packet) {
|
238 | packet.should.eql(expected);
|
239 | done();
|
240 | });
|
241 | });
|
242 |
|
243 | it('should fire a publish event (maximal)', function (done) {
|
244 | var expected = {
|
245 | cmd:"publish",
|
246 | retain: true,
|
247 | qos: 2,
|
248 | length: 12,
|
249 | dup: true,
|
250 | topic: "test",
|
251 | messageId: 10,
|
252 | payload: new Buffer("test")
|
253 | };
|
254 |
|
255 | var fixture = [
|
256 | 61, 12,
|
257 | 0, 4,
|
258 | 116, 101, 115, 116,
|
259 | 0, 10,
|
260 | 116, 101, 115, 116
|
261 | ];
|
262 |
|
263 | this.stream.write(new Buffer(fixture));
|
264 |
|
265 | this.conn.once('publish', function(packet) {
|
266 | packet.should.eql(expected);
|
267 | done();
|
268 | });
|
269 | });
|
270 |
|
271 | it('should fire an empty publish', function(done) {
|
272 | var expected = {
|
273 | cmd: "publish",
|
274 | retain: false,
|
275 | qos: 0,
|
276 | dup: false,
|
277 | length: 6,
|
278 | topic: "test",
|
279 | payload: new Buffer(0)
|
280 | };
|
281 |
|
282 | var fixture = [
|
283 | 48, 6,
|
284 | 0, 4,
|
285 | 116, 101, 115, 116
|
286 |
|
287 | ];
|
288 |
|
289 | this.stream.write(new Buffer(fixture));
|
290 |
|
291 | this.conn.once('publish', function(packet) {
|
292 | packet.should.eql(expected);
|
293 | done();
|
294 | });
|
295 | });
|
296 |
|
297 | it('should parse a splitted publish', function(done) {
|
298 | var expected = {
|
299 | cmd: "publish",
|
300 | retain: false,
|
301 | qos: 0,
|
302 | dup: false,
|
303 | length: 10,
|
304 | topic: "test",
|
305 | payload: new Buffer("test")
|
306 | };
|
307 |
|
308 | var fixture1 = [
|
309 | 48, 10,
|
310 | 0, 4,
|
311 | 116, 101, 115, 116
|
312 | ];
|
313 |
|
314 | var fixture2 = [
|
315 | 116, 101, 115, 116
|
316 | ];
|
317 |
|
318 | this.stream.write(new Buffer(fixture1));
|
319 | this.stream.write(new Buffer(fixture2));
|
320 |
|
321 | this.conn.once('publish', function(packet) {
|
322 | packet.should.eql(expected);
|
323 | done();
|
324 | });
|
325 | });
|
326 | });
|
327 |
|
328 | describe('puback', function() {
|
329 | it('should fire a puback event', function(done) {
|
330 | var expected = {
|
331 | cmd: 'puback',
|
332 | retain: false,
|
333 | qos: 0,
|
334 | dup: false,
|
335 | length: 2,
|
336 | messageId: 2
|
337 | };
|
338 |
|
339 | var fixture = [
|
340 | 64, 2,
|
341 | 0, 2
|
342 | ];
|
343 |
|
344 | this.stream.write(new Buffer(fixture));
|
345 |
|
346 | this.conn.once('puback', function(packet) {
|
347 | packet.should.eql(expected);
|
348 | done();
|
349 | });
|
350 | });
|
351 | });
|
352 |
|
353 | describe('pubrec', function() {
|
354 | it('should fire a pubrec event', function(done) {
|
355 | var expected = {
|
356 | cmd: 'pubrec',
|
357 | retain: false,
|
358 | qos: 0,
|
359 | dup: false,
|
360 | length: 2,
|
361 | messageId: 3
|
362 | };
|
363 |
|
364 | var fixture = [
|
365 | 80, 2,
|
366 | 0, 3
|
367 | ];
|
368 |
|
369 | this.stream.write(new Buffer(fixture));
|
370 |
|
371 | this.conn.once('pubrec', function(packet) {
|
372 | packet.should.eql(expected);
|
373 | done();
|
374 | });
|
375 | });
|
376 | });
|
377 |
|
378 | describe('pubrel', function() {
|
379 | it('should fire a pubrel event', function(done) {
|
380 | var expected = {
|
381 | cmd: 'pubrel',
|
382 | retain: false,
|
383 | qos: 0,
|
384 | dup: false,
|
385 | length: 2,
|
386 | messageId: 4
|
387 | };
|
388 |
|
389 | var fixture = [
|
390 | 96, 2,
|
391 | 0, 4
|
392 | ];
|
393 |
|
394 | this.stream.write(new Buffer(fixture));
|
395 |
|
396 | this.conn.once('pubrel', function(packet) {
|
397 | packet.should.eql(expected);
|
398 | done();
|
399 | });
|
400 | });
|
401 | });
|
402 |
|
403 | describe('pubcomp', function() {
|
404 | it('should fire a pubcomp event', function(done) {
|
405 | var expected = {
|
406 | cmd: 'pubcomp',
|
407 | retain: false,
|
408 | qos: 0,
|
409 | dup: false,
|
410 | length: 2,
|
411 | messageId: 5
|
412 | };
|
413 |
|
414 | var fixture = [
|
415 | 112, 2,
|
416 | 0, 5
|
417 | ];
|
418 |
|
419 | this.stream.write(new Buffer(fixture));
|
420 |
|
421 | this.conn.once('pubcomp', function(packet) {
|
422 | packet.should.eql(expected);
|
423 | done();
|
424 | });
|
425 | });
|
426 | });
|
427 |
|
428 | describe('subscribe', function() {
|
429 | it('should fire a subscribe event (1 topic)', function (done) {
|
430 | var expected = {
|
431 | cmd: 'subscribe',
|
432 | retain: false,
|
433 | qos: 1,
|
434 | dup: false,
|
435 | length: 9,
|
436 | subscriptions: [
|
437 | {
|
438 | topic: "test",
|
439 | qos: 0
|
440 | }
|
441 | ],
|
442 | messageId: 6
|
443 | };
|
444 |
|
445 | var fixture = [
|
446 | 130, 9,
|
447 | 0, 6,
|
448 | 0, 4,
|
449 | 116, 101, 115, 116,
|
450 | 0
|
451 | ];
|
452 | this.stream.write(new Buffer(fixture));
|
453 |
|
454 | this.conn.once('subscribe', function(packet) {
|
455 | packet.should.eql(expected);
|
456 | done();
|
457 | });
|
458 | });
|
459 |
|
460 | it('should fire a subscribe event (3 topic)', function (done) {
|
461 | var expected = {
|
462 | cmd: 'subscribe',
|
463 | retain: false,
|
464 | qos: 1,
|
465 | dup: false,
|
466 | length: 23,
|
467 | subscriptions: [
|
468 | {
|
469 | topic: "test",
|
470 | qos: 0
|
471 | },{
|
472 | topic: "uest",
|
473 | qos: 1
|
474 | },{
|
475 | topic: "tfst",
|
476 | qos: 2
|
477 | }
|
478 | ],
|
479 | messageId: 6
|
480 | };
|
481 |
|
482 | var fixture = [
|
483 | 130, 23,
|
484 | 0, 6,
|
485 | 0, 4,
|
486 | 116, 101, 115, 116,
|
487 | 0,
|
488 | 0, 4,
|
489 | 117, 101, 115, 116,
|
490 | 1,
|
491 | 0, 4,
|
492 | 116, 102, 115, 116,
|
493 | 2
|
494 | ];
|
495 |
|
496 | this.stream.write(new Buffer(fixture));
|
497 |
|
498 | this.conn.once('subscribe', function(packet) {
|
499 | packet.should.eql(expected);
|
500 | done();
|
501 | });
|
502 | });
|
503 | });
|
504 |
|
505 | describe('suback', function() {
|
506 | it('should fire a suback event', function(done) {
|
507 | var expected = {
|
508 | cmd: 'suback',
|
509 | retain: false,
|
510 | qos: 0,
|
511 | dup: false,
|
512 | length: 6,
|
513 | granted: [0, 1, 2, 128],
|
514 | messageId: 6
|
515 | };
|
516 |
|
517 | var fixture = [
|
518 | 144, 6,
|
519 | 0, 6,
|
520 | 0, 1, 2, 128
|
521 | ];
|
522 |
|
523 | this.stream.write(new Buffer(fixture));
|
524 |
|
525 | this.conn.once('suback', function(packet) {
|
526 | packet.should.eql(expected);
|
527 | done();
|
528 | });
|
529 | });
|
530 | });
|
531 |
|
532 | describe('unsubscribe', function() {
|
533 | it('should fire an unsubscribe event', function(done) {
|
534 | var expected = {
|
535 | cmd: 'unsubscribe',
|
536 | retain: false,
|
537 | qos: 1,
|
538 | dup: false,
|
539 | length: 14,
|
540 | unsubscriptions: [
|
541 | 'tfst',
|
542 | 'test'
|
543 | ],
|
544 | messageId: 7
|
545 | }
|
546 |
|
547 | var fixture = [
|
548 | 162, 14,
|
549 | 0, 7,
|
550 | 0, 4,
|
551 | 116, 102, 115, 116,
|
552 | 0, 4,
|
553 | 116, 101, 115, 116,
|
554 | ];
|
555 |
|
556 | this.stream.write(new Buffer(fixture));
|
557 |
|
558 | this.conn.once('unsubscribe', function(packet) {
|
559 | packet.should.eql(expected);
|
560 | done();
|
561 | });
|
562 | });
|
563 | });
|
564 |
|
565 | describe('unsuback', function() {
|
566 | it('should fire a unsuback event', function(done) {
|
567 | var expected = {
|
568 | cmd: 'unsuback',
|
569 | retain: false,
|
570 | qos: 0,
|
571 | dup: false,
|
572 | length: 2,
|
573 | messageId: 8
|
574 | };
|
575 |
|
576 | var fixture = [
|
577 | 176, 2,
|
578 | 0, 8
|
579 | ];
|
580 |
|
581 | this.stream.write(new Buffer(fixture));
|
582 |
|
583 | this.conn.once('unsuback', function(packet) {
|
584 | packet.should.eql(expected);
|
585 | done();
|
586 | });
|
587 | });
|
588 | });
|
589 |
|
590 | describe('pingreq', function() {
|
591 | it('should fire a pingreq event', function(done) {
|
592 | var expected = {
|
593 | cmd: 'pingreq',
|
594 | retain: false,
|
595 | qos: 0,
|
596 | dup: false,
|
597 | length: 0,
|
598 | };
|
599 |
|
600 | var fixture = [
|
601 | 192, 0
|
602 | ];
|
603 |
|
604 | this.stream.write(new Buffer(fixture));
|
605 |
|
606 | this.conn.once('pingreq', function(packet) {
|
607 | packet.should.eql(expected);
|
608 | done();
|
609 | });
|
610 | });
|
611 | });
|
612 |
|
613 | describe('pingresp', function() {
|
614 | it('should fire a pingresp event', function(done) {
|
615 | var expected = {
|
616 | cmd: 'pingresp',
|
617 | retain: false,
|
618 | qos: 0,
|
619 | dup: false,
|
620 | length: 0,
|
621 | };
|
622 |
|
623 | var fixture = [
|
624 | 208, 0
|
625 | ];
|
626 |
|
627 | this.stream.write(new Buffer(fixture));
|
628 |
|
629 | this.conn.once('pingresp', function(packet) {
|
630 | packet.should.eql(expected);
|
631 | done();
|
632 | });
|
633 | });
|
634 | });
|
635 |
|
636 | describe('disconnect', function() {
|
637 | it('should fire a disconnect event', function(done) {
|
638 | var expected = {
|
639 | cmd: 'disconnect',
|
640 | retain: false,
|
641 | qos: 0,
|
642 | dup: false,
|
643 | length: 0,
|
644 | };
|
645 |
|
646 | var fixture = [
|
647 | 224, 0
|
648 | ];
|
649 |
|
650 | this.stream.write(new Buffer(fixture));
|
651 |
|
652 | this.conn.once('disconnect', function(packet) {
|
653 | packet.should.eql(expected);
|
654 | done();
|
655 | });
|
656 | });
|
657 | });
|
658 |
|
659 | describe('reserverd (15)', function() {
|
660 | it('should emit an error', function(done) {
|
661 | var fixture = [
|
662 | 240, 0
|
663 | ];
|
664 |
|
665 | this.stream.write(new Buffer(fixture));
|
666 |
|
667 | this.conn.once('error', function(err) {
|
668 | done();
|
669 | });
|
670 | });
|
671 | });
|
672 |
|
673 | describe('reserverd (0)', function() {
|
674 | it('should emit an error', function(done) {
|
675 | var fixture = [
|
676 | 0, 0
|
677 | ];
|
678 |
|
679 | this.stream.write(new Buffer(fixture));
|
680 |
|
681 | this.conn.once('error', function(err) {
|
682 | done();
|
683 | });
|
684 | });
|
685 | });
|
686 | };
|