1
1
import { PROPERTY } from '../../../es6/enums' ;
2
2
import { PACKET_TYPE } from '../../enums' ;
3
3
import { PacketConnect } from '../connect' ;
4
+ import { MqttDecoder } from '../../MqttDecoder' ;
4
5
5
6
test ( 'can create a packet' , ( ) => {
6
- const packet = PacketConnect . create ( 5 , 0 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
7
+ const packet = PacketConnect . create ( 5 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
7
8
expect ( packet . b >> 4 ) . toBe ( PACKET_TYPE . CONNECT ) ;
8
9
expect ( packet . id ) . toBe ( 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
9
10
expect ( packet . v ) . toBe ( 5 ) ;
@@ -13,7 +14,7 @@ test('can create a packet', () => {
13
14
} ) ;
14
15
15
16
test ( 'can set username' , ( ) => {
16
- const packet = PacketConnect . create ( 5 , 0 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
17
+ const packet = PacketConnect . create ( 5 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
17
18
expect ( packet . userNameFlag ( ) ) . toBe ( false ) ;
18
19
expect ( packet . usr ) . toBe ( undefined ) ;
19
20
packet . setUserName ( 'streamich' ) ;
@@ -28,7 +29,7 @@ test('can set username', () => {
28
29
} ) ;
29
30
30
31
test ( 'can remove username' , ( ) => {
31
- const packet = PacketConnect . create ( 5 , 0 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
32
+ const packet = PacketConnect . create ( 5 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
32
33
expect ( packet . userNameFlag ( ) ) . toBe ( false ) ;
33
34
expect ( packet . usr ) . toBe ( undefined ) ;
34
35
packet . removeUserName ( ) ;
@@ -43,7 +44,7 @@ test('can remove username', () => {
43
44
} ) ;
44
45
45
46
test ( 'can set password' , ( ) => {
46
- const packet = PacketConnect . create ( 5 , 0 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
47
+ const packet = PacketConnect . create ( 5 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
47
48
expect ( packet . passwordFlag ( ) ) . toBe ( false ) ;
48
49
expect ( packet . usr ) . toBe ( undefined ) ;
49
50
packet . setPassword ( Buffer . from ( [ 1 , 2 , 3 ] ) ) ;
@@ -58,7 +59,7 @@ test('can set password', () => {
58
59
} ) ;
59
60
60
61
test ( 'can remove password' , ( ) => {
61
- const packet = PacketConnect . create ( 5 , 0 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
62
+ const packet = PacketConnect . create ( 5 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
62
63
expect ( packet . passwordFlag ( ) ) . toBe ( false ) ;
63
64
expect ( packet . pwd ) . toBe ( undefined ) ;
64
65
packet . removePassword ( ) ;
@@ -73,7 +74,7 @@ test('can remove password', () => {
73
74
} ) ;
74
75
75
76
test ( 'can change Clean Start flag' , ( ) => {
76
- const packet = PacketConnect . create ( 5 , 0 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
77
+ const packet = PacketConnect . create ( 5 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
77
78
expect ( packet . cleanStart ( ) ) . toBe ( false ) ;
78
79
packet . setCleanStart ( false ) ;
79
80
expect ( packet . cleanStart ( ) ) . toBe ( false ) ;
@@ -86,7 +87,7 @@ test('can change Clean Start flag', () => {
86
87
} ) ;
87
88
88
89
test ( 'can set will' , ( ) => {
89
- const packet = PacketConnect . create ( 5 , 0 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
90
+ const packet = PacketConnect . create ( 5 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
90
91
expect ( packet . willFlag ( ) ) . toBe ( false ) ;
91
92
expect ( packet . willQos ( ) ) . toBe ( 0 ) ;
92
93
expect ( packet . willRetain ( ) ) . toBe ( false ) ;
@@ -110,7 +111,7 @@ test('can set will', () => {
110
111
} ) ;
111
112
112
113
test ( 'can remove will' , ( ) => {
113
- const packet = PacketConnect . create ( 5 , 0 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
114
+ const packet = PacketConnect . create ( 5 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
114
115
packet . removeWill ( ) ;
115
116
expect ( packet . willFlag ( ) ) . toBe ( false ) ;
116
117
expect ( packet . willQos ( ) ) . toBe ( 0 ) ;
@@ -142,7 +143,7 @@ test('can remove will', () => {
142
143
} ) ;
143
144
144
145
test ( 'can manipulate multiple properties simultaneously' , ( ) => {
145
- const packet = PacketConnect . create ( 5 , 0 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
146
+ const packet = PacketConnect . create ( 5 , 30 , { } , 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ;
146
147
147
148
expect ( packet . qualityOfService ( ) ) . toBe ( 0 ) ;
148
149
packet . setQualityOfService ( 2 ) ;
@@ -214,3 +215,166 @@ test('can manipulate multiple properties simultaneously', () => {
214
215
expect ( packet . qualityOfService ( ) ) . toBe ( 2 ) ;
215
216
expect ( packet . usr ) . toBe ( undefined ) ;
216
217
} ) ;
218
+
219
+ test ( 'can serialize packet' , ( ) => {
220
+ const packet1 = PacketConnect . create ( 5 , 30 , { } , '' ) ;
221
+ const buf = packet1 . toBuffer ( ) ;
222
+ const decoder = new MqttDecoder ( ) ;
223
+ decoder . version = 5 ;
224
+ decoder . push ( buf ) ;
225
+ const packet2 = decoder . parse ( ) ! as PacketConnect ;
226
+ expect ( packet2 . b ) . toBe ( packet1 . b ) ;
227
+ expect ( packet2 . l ) . toBe ( packet1 . l ) ;
228
+ expect ( packet2 . v ) . toBe ( packet1 . v ) ;
229
+ expect ( packet2 . v ) . toBe ( 5 ) ;
230
+ expect ( packet2 . f ) . toBe ( packet1 . f ) ;
231
+ expect ( packet2 . k ) . toBe ( packet1 . k ) ;
232
+ expect ( packet2 . k ) . toBe ( 30 ) ;
233
+ expect ( packet2 . id ) . toBe ( packet1 . id ) ;
234
+ expect ( packet2 . id ) . toBe ( '' ) ;
235
+ expect ( packet2 . p ) . toEqual ( packet1 . p ) ;
236
+ expect ( packet2 . p ) . toEqual ( { } ) ;
237
+ expect ( packet2 . w ) . toEqual ( undefined ) ;
238
+ expect ( packet2 . wp ) . toEqual ( undefined ) ;
239
+ expect ( packet2 . wt ) . toEqual ( undefined ) ;
240
+ expect ( packet2 . usr ) . toEqual ( undefined ) ;
241
+ expect ( packet2 . pwd ) . toEqual ( undefined ) ;
242
+ expect ( packet2 ) . toEqual ( packet1 ) ;
243
+ } ) ;
244
+
245
+ test ( 'can serialize packet in MQTT 3.1.1' , ( ) => {
246
+ const packet1 = PacketConnect . create ( 4 , 30 , { } , '' ) ;
247
+ const buf = packet1 . toBuffer ( ) ;
248
+ const decoder = new MqttDecoder ( ) ;
249
+ decoder . version = 4 ;
250
+ decoder . push ( buf ) ;
251
+ const packet2 = decoder . parse ( ) ! as PacketConnect ;
252
+ expect ( packet2 . b ) . toBe ( packet1 . b ) ;
253
+ expect ( packet2 . l ) . toBe ( packet1 . l ) ;
254
+ expect ( packet2 . v ) . toBe ( packet1 . v ) ;
255
+ expect ( packet2 . v ) . toBe ( 4 ) ;
256
+ expect ( packet2 . f ) . toBe ( packet1 . f ) ;
257
+ expect ( packet2 . k ) . toBe ( packet1 . k ) ;
258
+ expect ( packet2 . k ) . toBe ( 30 ) ;
259
+ expect ( packet2 . id ) . toBe ( packet1 . id ) ;
260
+ expect ( packet2 . id ) . toBe ( '' ) ;
261
+ expect ( packet2 . p ) . toEqual ( packet1 . p ) ;
262
+ expect ( packet2 . p ) . toEqual ( { } ) ;
263
+ expect ( packet2 . w ) . toEqual ( undefined ) ;
264
+ expect ( packet2 . wp ) . toEqual ( undefined ) ;
265
+ expect ( packet2 . wt ) . toEqual ( undefined ) ;
266
+ expect ( packet2 . usr ) . toEqual ( undefined ) ;
267
+ expect ( packet2 . pwd ) . toEqual ( undefined ) ;
268
+ expect ( packet2 ) . toEqual ( packet1 ) ;
269
+ } ) ;
270
+
271
+ test ( 'can serialize properties' , ( ) => {
272
+ const packet1 = PacketConnect . create ( 5 , 30 , {
273
+ [ PROPERTY . ContentType ] : 'test' ,
274
+ [ PROPERTY . UserProperty ] : [
275
+ [ 'test' , 'test' ] ,
276
+ ] ,
277
+ } , '' ) ;
278
+ const buf = packet1 . toBuffer ( ) ;
279
+ const decoder = new MqttDecoder ( ) ;
280
+ decoder . version = 5 ;
281
+ decoder . push ( buf ) ;
282
+ const packet2 = decoder . parse ( ) ! as PacketConnect ;
283
+ expect ( packet2 . p ) . toEqual ( {
284
+ [ PROPERTY . ContentType ] : 'test' ,
285
+ [ PROPERTY . UserProperty ] : [
286
+ [ 'test' , 'test' ] ,
287
+ ] ,
288
+ } ) ;
289
+ } ) ;
290
+
291
+ test ( 'can serialize will' , ( ) => {
292
+ const packet1 = PacketConnect . create ( 5 , 30 , { } , '' ) ;
293
+ packet1 . setWill ( Buffer . from ( [ 1 , 2 , 3 , 4 , 5 ] ) , 'aha' , {
294
+ [ PROPERTY . ContentType ] : 'test' ,
295
+ [ PROPERTY . UserProperty ] : [
296
+ [ 'test' , 'test' ] ,
297
+ ] ,
298
+ } , 2 , true )
299
+ const buf = packet1 . toBuffer ( ) ;
300
+ const decoder = new MqttDecoder ( ) ;
301
+ decoder . version = 5 ;
302
+ decoder . push ( buf ) ;
303
+ const packet2 = decoder . parse ( ) ! as PacketConnect ;
304
+ expect ( packet2 . willFlag ( ) ) . toBe ( true ) ;
305
+ expect ( packet2 . willQos ( ) ) . toBe ( 2 ) ;
306
+ expect ( packet2 . willRetain ( ) ) . toBe ( true ) ;
307
+ expect ( packet2 . w ) . toEqual ( Buffer . from ( [ 1 , 2 , 3 , 4 , 5 ] ) ) ;
308
+ expect ( packet2 . wt ) . toBe ( 'aha' ) ;
309
+ expect ( packet2 . wp ) . toEqual ( {
310
+ [ PROPERTY . ContentType ] : 'test' ,
311
+ [ PROPERTY . UserProperty ] : [
312
+ [ 'test' , 'test' ] ,
313
+ ] ,
314
+ } ) ;
315
+ } ) ;
316
+
317
+ test ( 'can serialize will and packet properties' , ( ) => {
318
+ const packet1 = PacketConnect . create ( 5 , 30 , {
319
+ [ PROPERTY . ContentType ] : 'test2' ,
320
+ [ PROPERTY . UserProperty ] : [
321
+ [ 'test2' , 'test2' ] ,
322
+ ] ,
323
+ } , '' ) ;
324
+ packet1 . setWill ( Buffer . from ( [ 1 , 2 , 3 , 4 , 5 ] ) , 'aha' , {
325
+ [ PROPERTY . ContentType ] : 'test' ,
326
+ [ PROPERTY . UserProperty ] : [
327
+ [ 'test' , 'test' ] ,
328
+ ] ,
329
+ } , 0 , false )
330
+ const buf = packet1 . toBuffer ( ) ;
331
+ const decoder = new MqttDecoder ( ) ;
332
+ decoder . version = 5 ;
333
+ decoder . push ( buf ) ;
334
+ const packet2 = decoder . parse ( ) ! as PacketConnect ;
335
+ expect ( packet2 . p ) . toEqual ( {
336
+ [ PROPERTY . ContentType ] : 'test2' ,
337
+ [ PROPERTY . UserProperty ] : [
338
+ [ 'test2' , 'test2' ] ,
339
+ ] ,
340
+ } ) ;
341
+ expect ( packet2 . willFlag ( ) ) . toBe ( true ) ;
342
+ expect ( packet2 . willQos ( ) ) . toBe ( 0 ) ;
343
+ expect ( packet2 . willRetain ( ) ) . toBe ( false ) ;
344
+ expect ( packet2 . w ) . toEqual ( Buffer . from ( [ 1 , 2 , 3 , 4 , 5 ] ) ) ;
345
+ expect ( packet2 . wt ) . toBe ( 'aha' ) ;
346
+ expect ( packet2 . wp ) . toEqual ( {
347
+ [ PROPERTY . ContentType ] : 'test' ,
348
+ [ PROPERTY . UserProperty ] : [
349
+ [ 'test' , 'test' ] ,
350
+ ] ,
351
+ } ) ;
352
+ } ) ;
353
+
354
+ test ( 'can serialize password' , ( ) => {
355
+ const packet1 = PacketConnect . create ( 5 , 30 , { } , 'client-id' ) ;
356
+ packet1 . setPassword ( Buffer . from ( 'password' ) ) ;
357
+ const buf = packet1 . toBuffer ( ) ;
358
+ const decoder = new MqttDecoder ( ) ;
359
+ decoder . version = 5 ;
360
+ decoder . push ( buf ) ;
361
+ const packet2 = decoder . parse ( ) ! as PacketConnect ;
362
+ expect ( packet2 ) . toEqual ( packet1 ) ;
363
+ expect ( packet2 . pwd ) . toEqual ( Buffer . from ( 'password' ) ) ;
364
+ expect ( packet2 . id ) . toBe ( 'client-id' ) ;
365
+ } ) ;
366
+
367
+ test ( 'can serialize username' , ( ) => {
368
+ const packet1 = PacketConnect . create ( 5 , 30 , { } , 'client-id' ) ;
369
+ packet1 . setUserName ( 'streamich' )
370
+ packet1 . setPassword ( Buffer . from ( 'password' ) ) ;
371
+ const buf = packet1 . toBuffer ( ) ;
372
+ const decoder = new MqttDecoder ( ) ;
373
+ decoder . version = 5 ;
374
+ decoder . push ( buf ) ;
375
+ const packet2 = decoder . parse ( ) ! as PacketConnect ;
376
+ expect ( packet2 . usr ) . toBe ( 'streamich' ) ;
377
+ expect ( packet2 ) . toEqual ( packet1 ) ;
378
+ expect ( packet2 . pwd ) . toEqual ( Buffer . from ( 'password' ) ) ;
379
+ expect ( packet2 . id ) . toBe ( 'client-id' ) ;
380
+ } ) ;
0 commit comments