mqtt 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,83 +47,81 @@ describe MQTT::Packet do
47
47
  end
48
48
 
49
49
  describe "when setting packet parameters" do
50
- before(:each) do
51
- @packet = MQTT::Packet.new(
50
+ let(:packet) {
51
+ MQTT::Packet.new(
52
52
  :duplicate => false,
53
53
  :qos => 0,
54
54
  :retain => false
55
55
  )
56
- end
56
+ }
57
57
 
58
58
  it "should have a type_id method to get the integer ID of the packet type" do
59
- @packet = MQTT::Packet::Pingreq.new
60
- @packet.type_id.should == 12
59
+ packet = MQTT::Packet::Pingreq.new
60
+ packet.type_id.should == 12
61
61
  end
62
62
 
63
63
  it "should let you change the dup flag of a packet" do
64
- @packet.duplicate = true
65
- @packet.duplicate.should be_true
64
+ packet.duplicate = true
65
+ packet.duplicate.should be_true
66
66
  end
67
67
 
68
68
  it "should let you change the dup flag of a packet using an integer" do
69
- @packet.duplicate = 1
70
- @packet.duplicate.should be_true
69
+ packet.duplicate = 1
70
+ packet.duplicate.should be_true
71
71
  end
72
72
 
73
73
  it "should let you change the retain flag of a packet" do
74
- @packet.retain = true
75
- @packet.retain.should be_true
74
+ packet.retain = true
75
+ packet.retain.should be_true
76
76
  end
77
77
 
78
78
  it "should let you change the retain flag of a packet using an integer" do
79
- @packet.retain = 1
80
- @packet.retain.should be_true
79
+ packet.retain = 1
80
+ packet.retain.should be_true
81
81
  end
82
82
  end
83
83
 
84
84
  it "should let you attributes using the update_attributes method" do
85
- @packet = MQTT::Packet.new(:qos => 1)
86
- @packet.update_attributes(:qos => 2)
87
- @packet.qos.should == 2
85
+ packet = MQTT::Packet.new(:qos => 1)
86
+ packet.update_attributes(:qos => 2)
87
+ packet.qos.should == 2
88
88
  end
89
89
 
90
90
  describe "protected methods" do
91
- before(:each) do
92
- @packet = MQTT::Packet.new
93
- end
91
+ let(:packet) { MQTT::Packet.new }
94
92
 
95
93
  it "should provide a encode_bytes method to get some bytes as Integers" do
96
- data = @packet.send(:encode_bytes, 0x48, 0x65, 0x6c, 0x6c, 'o'.unpack('C1')[0])
94
+ data = packet.send(:encode_bytes, 0x48, 0x65, 0x6c, 0x6c, 'o'.unpack('C1')[0])
97
95
  data.should == 'Hello'
98
96
  end
99
97
 
100
98
  it "should provide a add_short method to get a big-endian unsigned 16-bit integer" do
101
- data = @packet.send(:encode_short, 1024)
99
+ data = packet.send(:encode_short, 1024)
102
100
  data.should == "\x04\x00"
103
101
  data.encoding.to_s.should == "ASCII-8BIT"
104
102
  end
105
103
 
106
104
  it "should provide a add_string method to get a string preceeded by its length" do
107
- data = @packet.send(:encode_string, 'quack')
105
+ data = packet.send(:encode_string, 'quack')
108
106
  data.should == "\x00\x05quack"
109
107
  data.encoding.to_s.should == "ASCII-8BIT"
110
108
  end
111
109
 
112
110
  it "should provide a shift_short method to get a 16-bit unsigned integer" do
113
111
  buffer = "\x22\x8Bblahblah"
114
- @packet.send(:shift_short,buffer).should == 8843
112
+ packet.send(:shift_short,buffer).should == 8843
115
113
  buffer.should == 'blahblah'
116
114
  end
117
115
 
118
116
  it "should provide a shift_byte method to get one byte as integers" do
119
117
  buffer = "\x01blahblah"
120
- @packet.send(:shift_byte,buffer).should == 1
118
+ packet.send(:shift_byte,buffer).should == 1
121
119
  buffer.should == 'blahblah'
122
120
  end
123
121
 
124
122
  it "should provide a shift_string method to get a string preceeded by its length" do
125
123
  buffer = "\x00\x05Hello World"
126
- @packet.send(:shift_string,buffer).should == "Hello"
124
+ packet.send(:shift_string,buffer).should == "Hello"
127
125
  buffer.should == ' World'
128
126
  end
129
127
  end
@@ -190,66 +188,62 @@ describe MQTT::Packet::Publish do
190
188
  end
191
189
 
192
190
  describe "when parsing a packet with QOS 0" do
193
- before(:each) do
194
- @packet = MQTT::Packet.parse( "\x30\x11\x00\x04testhello world" )
195
- end
191
+ let(:packet) { MQTT::Packet.parse( "\x30\x11\x00\x04testhello world" ) }
196
192
 
197
193
  it "should correctly create the right type of packet object" do
198
- @packet.class.should == MQTT::Packet::Publish
194
+ packet.class.should == MQTT::Packet::Publish
199
195
  end
200
196
 
201
197
  it "should set the QOS level correctly" do
202
- @packet.qos.should == 0
198
+ packet.qos.should == 0
203
199
  end
204
200
 
205
201
  it "should set the RETAIN flag correctly" do
206
- @packet.retain.should be_false
202
+ packet.retain.should be_false
207
203
  end
208
204
 
209
205
  it "should set the DUP flag correctly" do
210
- @packet.duplicate.should be_false
206
+ packet.duplicate.should be_false
211
207
  end
212
208
 
213
209
  it "should set the topic name correctly" do
214
- @packet.topic.should == 'test'
215
- @packet.topic.encoding.to_s.should == 'UTF-8'
210
+ packet.topic.should == 'test'
211
+ packet.topic.encoding.to_s.should == 'UTF-8'
216
212
  end
217
213
 
218
214
  it "should set the payload correctly" do
219
- @packet.payload.should == 'hello world'
220
- @packet.payload.encoding.to_s.should == 'ASCII-8BIT'
215
+ packet.payload.should == 'hello world'
216
+ packet.payload.encoding.to_s.should == 'ASCII-8BIT'
221
217
  end
222
218
  end
223
219
 
224
220
  describe "when parsing a packet with QOS 2 and retain and dup flags set" do
225
- before(:each) do
226
- @packet = MQTT::Packet.parse( "\x3D\x12\x00\x03c/d\x00\x05hello world" )
227
- end
221
+ let(:packet) { MQTT::Packet.parse( "\x3D\x12\x00\x03c/d\x00\x05hello world" ) }
228
222
 
229
223
  it "should correctly create the right type of packet object" do
230
- @packet.class.should == MQTT::Packet::Publish
224
+ packet.class.should == MQTT::Packet::Publish
231
225
  end
232
226
 
233
227
  it "should set the QOS level correctly" do
234
- @packet.qos.should == 2
228
+ packet.qos.should == 2
235
229
  end
236
230
 
237
231
  it "should set the RETAIN flag correctly" do
238
- @packet.retain.should be_true
232
+ packet.retain.should be_true
239
233
  end
240
234
 
241
235
  it "should set the DUP flag correctly" do
242
- @packet.duplicate.should be_true
236
+ packet.duplicate.should be_true
243
237
  end
244
238
 
245
239
  it "should set the topic name correctly" do
246
- @packet.topic.should == 'c/d'
247
- @packet.topic.encoding.to_s.should == 'UTF-8'
240
+ packet.topic.should == 'c/d'
241
+ packet.topic.encoding.to_s.should == 'UTF-8'
248
242
  end
249
243
 
250
244
  it "should set the payload correctly" do
251
- @packet.payload.should == 'hello world'
252
- @packet.payload.encoding.to_s.should == 'ASCII-8BIT'
245
+ packet.payload.should == 'hello world'
246
+ packet.payload.encoding.to_s.should == 'ASCII-8BIT'
253
247
  end
254
248
  end
255
249
 
@@ -264,119 +258,117 @@ describe MQTT::Packet::Publish do
264
258
  end
265
259
 
266
260
  describe "when parsing a packet with a body of 314 bytes" do
267
- before(:each) do
261
+ let(:packet) {
268
262
  # 0x30 = publish
269
263
  # 0xC1 = (65 * 1)
270
264
  # 0x02 = (2 * 128)
271
- @packet = MQTT::Packet.parse( "\x30\xC1\x02\x00\x05topic" + ('x' * 314) )
272
- end
265
+ MQTT::Packet.parse( "\x30\xC1\x02\x00\x05topic" + ('x' * 314) )
266
+ }
273
267
 
274
268
  it "should parse the packet type correctly" do
275
- @packet.class.should == MQTT::Packet::Publish
269
+ packet.class.should == MQTT::Packet::Publish
276
270
  end
277
271
 
278
272
  it "should get the topic name correctly" do
279
- @packet.topic.should == 'topic'
273
+ packet.topic.should == 'topic'
280
274
  end
281
275
 
282
276
  it "should get the body length correctly" do
283
- @packet.payload.bytesize.should == 314
277
+ packet.payload.bytesize.should == 314
284
278
  end
285
279
  end
286
280
 
287
281
  describe "when parsing a packet with a body of 16kbytes" do
288
- before(:each) do
282
+ let(:packet) do
289
283
  # 0x30 = publish
290
284
  # 0x87 = (7 * 1)
291
285
  # 0x80 = (0 * 128)
292
286
  # 0x01 = (1 * 16384)
293
- @packet = MQTT::Packet.parse( "\x30\x87\x80\x01\x00\x05topic" + ('x'*16384) )
287
+ MQTT::Packet.parse( "\x30\x87\x80\x01\x00\x05topic" + ('x'*16384) )
294
288
  end
295
289
 
296
290
  it "should parse the packet type correctly" do
297
- @packet.class.should == MQTT::Packet::Publish
291
+ packet.class.should == MQTT::Packet::Publish
298
292
  end
299
293
 
300
294
  it "should get the topic name correctly" do
301
- @packet.topic.should == 'topic'
295
+ packet.topic.should == 'topic'
302
296
  end
303
297
 
304
298
  it "should get the body length correctly" do
305
- @packet.payload.bytesize.should == 16384
299
+ packet.payload.bytesize.should == 16384
306
300
  end
307
301
  end
308
302
 
309
303
  describe "processing a packet containing UTF-8 character" do
310
- before(:each) do
311
- @packet = MQTT::Packet::Publish.new(
304
+ let(:packet) do
305
+ MQTT::Packet::Publish.new(
312
306
  :topic => "Test ①".force_encoding("UTF-8"),
313
307
  :payload => "Snowman: ☃".force_encoding("UTF-8")
314
308
  )
315
309
  end
316
310
 
317
311
  it "should have the correct topic byte length" do
318
- @packet.topic.bytesize.should == 8
312
+ packet.topic.bytesize.should == 8
319
313
  end
320
314
 
321
315
  it "should have the correct topic string length", :unless => RUBY_VERSION =~ /^1\.8/ do
322
316
  # Ruby 1.8 doesn't support UTF-8 properly
323
- @packet.topic.length.should == 6
317
+ packet.topic.length.should == 6
324
318
  end
325
319
 
326
320
  it "should have the correct payload byte length" do
327
- @packet.payload.bytesize.should == 12
321
+ packet.payload.bytesize.should == 12
328
322
  end
329
323
 
330
324
  it "should have the correct payload string length", :unless => RUBY_VERSION =~ /^1\.8/ do
331
325
  # Ruby 1.8 doesn't support UTF-8 properly
332
- @packet.payload.length.should == 10
326
+ packet.payload.length.should == 10
333
327
  end
334
328
 
335
329
  it "should encode to MQTT packet correctly" do
336
- @packet.to_s.should == "\x30\x16\x00\x08Test \xE2\x91\xA0Snowman: \xE2\x98\x83".force_encoding('BINARY')
330
+ packet.to_s.should == "\x30\x16\x00\x08Test \xE2\x91\xA0Snowman: \xE2\x98\x83".force_encoding('BINARY')
337
331
  end
338
332
 
339
333
  it "should parse the serialised packet" do
340
- packet2 = MQTT::Packet.parse( @packet.to_s )
334
+ packet2 = MQTT::Packet.parse( packet.to_s )
341
335
  packet2.topic.should == "Test ①".force_encoding('UTF-8')
342
336
  packet2.payload.should == "Snowman: ☃".force_encoding('BINARY')
343
337
  end
344
338
  end
345
339
 
346
340
  describe "reading a packet from a socket" do
347
- before(:each) do
348
- @socket = StringIO.new("\x30\x11\x00\x04testhello world")
349
- @packet = MQTT::Packet.read(@socket)
350
- end
341
+ let(:socket) { StringIO.new("\x30\x11\x00\x04testhello world") }
342
+ let(:packet) { MQTT::Packet.read(socket) }
351
343
 
352
344
  it "should correctly create the right type of packet object" do
353
- @packet.class.should == MQTT::Packet::Publish
345
+ packet.class.should == MQTT::Packet::Publish
354
346
  end
355
347
 
356
348
  it "should set the body length is read correctly" do
357
- @packet.body_length.should == 17
349
+ packet.body_length.should == 17
358
350
  end
359
351
 
360
352
  it "should set the QOS level correctly" do
361
- @packet.qos.should == 0
353
+ packet.qos.should == 0
362
354
  end
363
355
 
364
356
  it "should set the RETAIN flag correctly" do
365
- @packet.retain.should be_false
357
+ packet.retain.should be_false
366
358
  end
367
359
 
368
360
  it "should set the DUP flag correctly" do
369
- @packet.duplicate.should be_false
361
+ packet.duplicate.should be_false
370
362
  end
371
363
 
372
364
  it "should set the topic name correctly" do
373
- @packet.topic.should == 'test'
374
- @packet.topic.encoding.to_s.should == 'UTF-8'
365
+ packet.topic.should == 'test'
366
+ packet.topic.encoding.to_s.should == 'UTF-8'
375
367
  end
376
368
 
377
369
  it "should set the payload correctly" do
378
- @packet.payload.should == 'hello world'
379
- @packet.payload.encoding.to_s.should == 'ASCII-8BIT'
370
+ packet.payload.should == 'hello world'
371
+ packet.payload.encoding.to_s.should == 'ASCII-8BIT'
380
372
  end
381
373
  end
382
374
 
@@ -483,118 +475,118 @@ describe MQTT::Packet::Connect do
483
475
  end
484
476
 
485
477
  describe "when parsing a simple Connect packet" do
486
- before(:each) do
487
- @packet = MQTT::Packet.parse(
478
+ let(:packet) do
479
+ MQTT::Packet.parse(
488
480
  "\x10\x16\x00\x06MQIsdp\x03\x00\x00\x0a\x00\x08myclient"
489
481
  )
490
482
  end
491
483
 
492
484
  it "should correctly create the right type of packet object" do
493
- @packet.class.should == MQTT::Packet::Connect
485
+ packet.class.should == MQTT::Packet::Connect
494
486
  end
495
487
 
496
488
  it "should set the QOS of the packet correctly" do
497
- @packet.qos.should == 0
489
+ packet.qos.should == 0
498
490
  end
499
491
 
500
492
  it "should set the Protocol Name of the packet correctly" do
501
- @packet.protocol_name.should == 'MQIsdp'
502
- @packet.protocol_name.encoding.to_s.should == 'UTF-8'
493
+ packet.protocol_name.should == 'MQIsdp'
494
+ packet.protocol_name.encoding.to_s.should == 'UTF-8'
503
495
  end
504
496
 
505
497
  it "should set the Protocol Version of the packet correctly" do
506
- @packet.protocol_version.should == 3
498
+ packet.protocol_version.should == 3
507
499
  end
508
500
 
509
501
  it "should set the Client Identifier of the packet correctly" do
510
- @packet.client_id.should == 'myclient'
511
- @packet.client_id.encoding.to_s.should == 'UTF-8'
502
+ packet.client_id.should == 'myclient'
503
+ packet.client_id.encoding.to_s.should == 'UTF-8'
512
504
  end
513
505
 
514
506
  it "should set the Keep Alive timer of the packet correctly" do
515
- @packet.keep_alive.should == 10
507
+ packet.keep_alive.should == 10
516
508
  end
517
509
 
518
510
  it "should set not have the clean session flag set" do
519
- @packet.clean_session.should be_false
511
+ packet.clean_session.should be_false
520
512
  end
521
513
 
522
514
  it "should set the the username field of the packet to nil" do
523
- @packet.username.should be_nil
515
+ packet.username.should be_nil
524
516
  end
525
517
 
526
518
  it "should set the the password field of the packet to nil" do
527
- @packet.password.should be_nil
519
+ packet.password.should be_nil
528
520
  end
529
521
  end
530
522
 
531
523
  describe "when parsing a Connect packet with the clean session flag set" do
532
- before(:each) do
533
- @packet = MQTT::Packet.parse(
524
+ let(:packet) do
525
+ MQTT::Packet.parse(
534
526
  "\x10\x16\x00\x06MQIsdp\x03\x02\x00\x0a\x00\x08myclient"
535
527
  )
536
528
  end
537
529
 
538
530
  it "should set the clean session flag" do
539
- @packet.clean_session.should be_true
531
+ packet.clean_session.should be_true
540
532
  end
541
533
  end
542
534
 
543
535
  describe "when parsing a Connect packet with a Will and Testament" do
544
- before(:each) do
545
- @packet = MQTT::Packet.parse(
536
+ let(:packet) do
537
+ MQTT::Packet.parse(
546
538
  "\x10\x24\x00\x06MQIsdp\x03\x0e\x00\x0a\x00\x08myclient\x00\x05topic\x00\x05hello"
547
539
  )
548
540
  end
549
541
 
550
542
  it "should correctly create the right type of packet object" do
551
- @packet.class.should == MQTT::Packet::Connect
543
+ packet.class.should == MQTT::Packet::Connect
552
544
  end
553
545
 
554
546
  it "should set the QOS of the packet correctly" do
555
- @packet.qos.should == 0
547
+ packet.qos.should == 0
556
548
  end
557
549
 
558
550
  it "should set the Protocol Name of the packet correctly" do
559
- @packet.protocol_name.should == 'MQIsdp'
560
- @packet.protocol_name.encoding.to_s.should == 'UTF-8'
551
+ packet.protocol_name.should == 'MQIsdp'
552
+ packet.protocol_name.encoding.to_s.should == 'UTF-8'
561
553
  end
562
554
 
563
555
  it "should set the Protocol Version of the packet correctly" do
564
- @packet.protocol_version.should == 3
556
+ packet.protocol_version.should == 3
565
557
  end
566
558
 
567
559
  it "should set the Client Identifier of the packet correctly" do
568
- @packet.client_id.should == 'myclient'
569
- @packet.client_id.encoding.to_s.should == 'UTF-8'
560
+ packet.client_id.should == 'myclient'
561
+ packet.client_id.encoding.to_s.should == 'UTF-8'
570
562
  end
571
563
 
572
564
  it "should set the clean session flag should be set" do
573
- @packet.clean_session.should be_true
565
+ packet.clean_session.should be_true
574
566
  end
575
567
 
576
568
  it "should set the QOS of the Will should be 1" do
577
- @packet.will_qos.should == 1
569
+ packet.will_qos.should == 1
578
570
  end
579
571
 
580
572
  it "should set the Will retain flag should be false" do
581
- @packet.will_retain.should be_false
573
+ packet.will_retain.should be_false
582
574
  end
583
575
 
584
576
  it "should set the Will topic of the packet correctly" do
585
- @packet.will_topic.should == 'topic'
586
- @packet.will_topic.encoding.to_s.should == 'UTF-8'
577
+ packet.will_topic.should == 'topic'
578
+ packet.will_topic.encoding.to_s.should == 'UTF-8'
587
579
  end
588
580
 
589
581
  it "should set the Will payload of the packet correctly" do
590
- @packet.will_payload.should == 'hello'
591
- @packet.will_payload.encoding.to_s.should == 'UTF-8'
582
+ packet.will_payload.should == 'hello'
583
+ packet.will_payload.encoding.to_s.should == 'UTF-8'
592
584
  end
593
585
  end
594
586
 
595
587
  describe "when parsing a Connect packet with a username and password" do
596
- before(:each) do
597
- @packet = MQTT::Packet.parse(
588
+ let(:packet) do
589
+ MQTT::Packet.parse(
598
590
  "\x10\x2A"+
599
591
  "\x00\x06MQIsdp"+
600
592
  "\x03\xC0\x00\x0a"+
@@ -605,95 +597,95 @@ describe MQTT::Packet::Connect do
605
597
  end
606
598
 
607
599
  it "should correctly create the right type of packet object" do
608
- @packet.class.should == MQTT::Packet::Connect
600
+ packet.class.should == MQTT::Packet::Connect
609
601
  end
610
602
 
611
603
  it "should set the QOS of the packet correctly" do
612
- @packet.qos.should == 0
604
+ packet.qos.should == 0
613
605
  end
614
606
 
615
607
  it "should set the Protocol Name of the packet correctly" do
616
- @packet.protocol_name.should == 'MQIsdp'
617
- @packet.protocol_name.encoding.to_s.should == 'UTF-8'
608
+ packet.protocol_name.should == 'MQIsdp'
609
+ packet.protocol_name.encoding.to_s.should == 'UTF-8'
618
610
  end
619
611
 
620
612
  it "should set the Protocol Version of the packet correctly" do
621
- @packet.protocol_version.should == 3
613
+ packet.protocol_version.should == 3
622
614
  end
623
615
 
624
616
  it "should set the Client Identifier of the packet correctly" do
625
- @packet.client_id.should == 'myclient'
626
- @packet.client_id.encoding.to_s.should == 'UTF-8'
617
+ packet.client_id.should == 'myclient'
618
+ packet.client_id.encoding.to_s.should == 'UTF-8'
627
619
  end
628
620
 
629
621
  it "should set the Keep Alive Timer of the packet correctly" do
630
- @packet.keep_alive.should == 10
622
+ packet.keep_alive.should == 10
631
623
  end
632
624
 
633
625
  it "should set the Username of the packet correctly" do
634
- @packet.username.should == 'username'
635
- @packet.username.encoding.to_s.should == 'UTF-8'
626
+ packet.username.should == 'username'
627
+ packet.username.encoding.to_s.should == 'UTF-8'
636
628
  end
637
629
 
638
630
  it "should set the Username of the packet correctly" do
639
- @packet.password.should == 'password'
640
- @packet.password.encoding.to_s.should == 'UTF-8'
631
+ packet.password.should == 'password'
632
+ packet.password.encoding.to_s.should == 'UTF-8'
641
633
  end
642
634
  end
643
635
 
644
636
  describe "when parsing a Connect that has a username but no password" do
645
- before(:each) do
646
- @packet = MQTT::Packet.parse(
637
+ let(:packet) do
638
+ MQTT::Packet.parse(
647
639
  "\x10\x20\x00\x06MQIsdp\x03\x80\x00\x0a\x00\x08myclient\x00\x08username"
648
640
  )
649
641
  end
650
642
 
651
643
  it "should set the Username of the packet correctly" do
652
- @packet.username.should == 'username'
653
- @packet.username.encoding.to_s.should == 'UTF-8'
644
+ packet.username.should == 'username'
645
+ packet.username.encoding.to_s.should == 'UTF-8'
654
646
  end
655
647
 
656
648
  it "should set the Username of the packet correctly" do
657
- @packet.password.should be_nil
649
+ packet.password.should be_nil
658
650
  end
659
651
  end
660
652
 
661
653
  describe "when parsing a Connect that has a password but no username" do
662
- before(:each) do
663
- @packet = MQTT::Packet.parse(
654
+ let(:packet) do
655
+ MQTT::Packet.parse(
664
656
  "\x10\x20\x00\x06MQIsdp\x03\x40\x00\x0a\x00\x08myclient\x00\x08password"
665
657
  )
666
658
  end
667
659
 
668
660
  it "should set the Username of the packet correctly" do
669
- @packet.username.should be_nil
661
+ packet.username.should be_nil
670
662
  end
671
663
 
672
664
  it "should set the Username of the packet correctly" do
673
- @packet.password.should == 'password'
674
- @packet.password.encoding.to_s.should == 'UTF-8'
665
+ packet.password.should == 'password'
666
+ packet.password.encoding.to_s.should == 'UTF-8'
675
667
  end
676
668
  end
677
669
 
678
670
  describe "when parsing a Connect packet has the username and password flags set but doesn't have the fields" do
679
- before(:each) do
680
- @packet = MQTT::Packet.parse(
671
+ let(:packet) do
672
+ MQTT::Packet.parse(
681
673
  "\x10\x16\x00\x06MQIsdp\x03\xC0\x00\x0a\x00\x08myclient"
682
674
  )
683
675
  end
684
676
 
685
677
  it "should set the Username of the packet correctly" do
686
- @packet.username.should be_nil
678
+ packet.username.should be_nil
687
679
  end
688
680
 
689
681
  it "should set the Username of the packet correctly" do
690
- @packet.password.should be_nil
682
+ packet.password.should be_nil
691
683
  end
692
684
  end
693
685
 
694
686
  describe "when parsing a Connect packet with every option set" do
695
- before(:each) do
696
- @packet = MQTT::Packet.parse(
687
+ let(:packet) do
688
+ MQTT::Packet.parse(
697
689
  "\x10\x5F"+ # fixed header (2)
698
690
  "\x00\x06MQIsdp"+ # protocol name (8)
699
691
  "\x03\xf6"+ # protocol version + flags (2)
@@ -707,57 +699,57 @@ describe MQTT::Packet::Connect do
707
699
  end
708
700
 
709
701
  it "should correctly create the right type of packet object" do
710
- @packet.class.should == MQTT::Packet::Connect
702
+ packet.class.should == MQTT::Packet::Connect
711
703
  end
712
704
 
713
705
  it "should set the QOS of the packet correctly" do
714
- @packet.qos.should == 0
706
+ packet.qos.should == 0
715
707
  end
716
708
 
717
709
  it "should set the Protocol Name of the packet correctly" do
718
- @packet.protocol_name.should == 'MQIsdp'
719
- @packet.protocol_name.encoding.to_s.should == 'UTF-8'
710
+ packet.protocol_name.should == 'MQIsdp'
711
+ packet.protocol_name.encoding.to_s.should == 'UTF-8'
720
712
  end
721
713
 
722
714
  it "should set the Protocol Version of the packet correctly" do
723
- @packet.protocol_version.should == 3
715
+ packet.protocol_version.should == 3
724
716
  end
725
717
 
726
718
  it "should set the Keep Alive Timer of the packet correctly" do
727
- @packet.keep_alive.should == 65535
719
+ packet.keep_alive.should == 65535
728
720
  end
729
721
 
730
722
  it "should set the Client Identifier of the packet correctly" do
731
- @packet.client_id.should == '12345678901234567890123'
732
- @packet.client_id.encoding.to_s.should == 'UTF-8'
723
+ packet.client_id.should == '12345678901234567890123'
724
+ packet.client_id.encoding.to_s.should == 'UTF-8'
733
725
  end
734
726
 
735
727
  it "should set the Will QoS of the packet correctly" do
736
- @packet.will_qos.should == 2
728
+ packet.will_qos.should == 2
737
729
  end
738
730
 
739
731
  it "should set the Will retain flag of the packet correctly" do
740
- @packet.will_retain.should be_true
732
+ packet.will_retain.should be_true
741
733
  end
742
734
 
743
735
  it "should set the Will topic of the packet correctly" do
744
- @packet.will_topic.should == 'will_topic'
745
- @packet.will_topic.encoding.to_s.should == 'UTF-8'
736
+ packet.will_topic.should == 'will_topic'
737
+ packet.will_topic.encoding.to_s.should == 'UTF-8'
746
738
  end
747
739
 
748
740
  it "should set the Will payload of the packet correctly" do
749
- @packet.will_payload.should == 'will_message'
750
- @packet.will_payload.encoding.to_s.should == 'UTF-8'
741
+ packet.will_payload.should == 'will_message'
742
+ packet.will_payload.encoding.to_s.should == 'UTF-8'
751
743
  end
752
744
 
753
745
  it "should set the Username of the packet correctly" do
754
- @packet.username.should == 'user0123456789'
755
- @packet.username.encoding.to_s.should == 'UTF-8'
746
+ packet.username.should == 'user0123456789'
747
+ packet.username.encoding.to_s.should == 'UTF-8'
756
748
  end
757
749
 
758
750
  it "should set the Username of the packet correctly" do
759
- @packet.password.should == 'pass0123456789'
760
- @packet.password.encoding.to_s.should == 'UTF-8'
751
+ packet.password.should == 'pass0123456789'
752
+ packet.password.encoding.to_s.should == 'UTF-8'
761
753
  end
762
754
  end
763
755
 
@@ -814,139 +806,135 @@ describe MQTT::Packet::Connack do
814
806
  end
815
807
 
816
808
  describe "when parsing a successful Connection Accepted packet" do
817
- before(:each) do
818
- @packet = MQTT::Packet.parse( "\x20\x02\x00\x00" )
809
+ let(:packet) do
810
+ MQTT::Packet.parse( "\x20\x02\x00\x00" )
819
811
  end
820
812
 
821
813
  it "should correctly create the right type of packet object" do
822
- @packet.class.should == MQTT::Packet::Connack
814
+ packet.class.should == MQTT::Packet::Connack
823
815
  end
824
816
 
825
817
  it "should set the QOS of the packet correctly" do
826
- @packet.qos.should == 0
818
+ packet.qos.should == 0
827
819
  end
828
820
 
829
821
  it "should set the return code of the packet correctly" do
830
- @packet.return_code.should == 0x00
822
+ packet.return_code.should == 0x00
831
823
  end
832
824
 
833
825
  it "should set the return message of the packet correctly" do
834
- @packet.return_msg.should match(/Connection Accepted/i)
826
+ packet.return_msg.should match(/Connection Accepted/i)
835
827
  end
836
828
  end
837
829
 
838
830
  describe "when parsing a unacceptable protocol version packet" do
839
- before(:each) do
840
- @packet = MQTT::Packet.parse( "\x20\x02\x00\x01" )
831
+ let(:packet) do
832
+ MQTT::Packet.parse( "\x20\x02\x00\x01" )
841
833
  end
842
834
 
843
835
  it "should correctly create the right type of packet object" do
844
- @packet.class.should == MQTT::Packet::Connack
836
+ packet.class.should == MQTT::Packet::Connack
845
837
  end
846
838
 
847
839
  it "should set the return code of the packet correctly" do
848
- @packet.return_code.should == 0x01
840
+ packet.return_code.should == 0x01
849
841
  end
850
842
 
851
843
  it "should set the return message of the packet correctly" do
852
- @packet.return_msg.should match(/unacceptable protocol version/i)
844
+ packet.return_msg.should match(/unacceptable protocol version/i)
853
845
  end
854
846
  end
855
847
 
856
848
  describe "when parsing a client identifier rejected packet" do
857
- before(:each) do
858
- @packet = MQTT::Packet.parse( "\x20\x02\x00\x02" )
859
- end
849
+ let(:packet) { MQTT::Packet.parse( "\x20\x02\x00\x02" ) }
860
850
 
861
851
  it "should correctly create the right type of packet object" do
862
- @packet.class.should == MQTT::Packet::Connack
852
+ packet.class.should == MQTT::Packet::Connack
863
853
  end
864
854
 
865
855
  it "should set the return code of the packet correctly" do
866
- @packet.return_code.should == 0x02
856
+ packet.return_code.should == 0x02
867
857
  end
868
858
 
869
859
  it "should set the return message of the packet correctly" do
870
- @packet.return_msg.should match(/client identifier rejected/i)
860
+ packet.return_msg.should match(/client identifier rejected/i)
871
861
  end
872
862
  end
873
863
 
874
864
  describe "when parsing a broker unavailable packet" do
875
- before(:each) do
876
- @packet = MQTT::Packet.parse( "\x20\x02\x00\x03" )
865
+ let(:packet) do
866
+ MQTT::Packet.parse( "\x20\x02\x00\x03" )
877
867
  end
878
868
 
879
869
  it "should correctly create the right type of packet object" do
880
- @packet.class.should == MQTT::Packet::Connack
870
+ packet.class.should == MQTT::Packet::Connack
881
871
  end
882
872
 
883
873
  it "should set the return code of the packet correctly" do
884
- @packet.return_code.should == 0x03
874
+ packet.return_code.should == 0x03
885
875
  end
886
876
 
887
877
  it "should set the return message of the packet correctly" do
888
- @packet.return_msg.should match(/broker unavailable/i)
878
+ packet.return_msg.should match(/broker unavailable/i)
889
879
  end
890
880
  end
891
881
 
892
882
  describe "when parsing a broker unavailable packet" do
893
- before(:each) do
894
- @packet = MQTT::Packet.parse( "\x20\x02\x00\x04" )
883
+ let(:packet) do
884
+ MQTT::Packet.parse( "\x20\x02\x00\x04" )
895
885
  end
896
886
 
897
887
  it "should correctly create the right type of packet object" do
898
- @packet.class.should == MQTT::Packet::Connack
888
+ packet.class.should == MQTT::Packet::Connack
899
889
  end
900
890
 
901
891
  it "should set the return code of the packet correctly" do
902
- @packet.return_code.should == 0x04
892
+ packet.return_code.should == 0x04
903
893
  end
904
894
 
905
895
  it "should set the return message of the packet correctly" do
906
- @packet.return_msg.should match(/bad user name or password/i)
896
+ packet.return_msg.should match(/bad user name or password/i)
907
897
  end
908
898
  end
909
899
 
910
900
  describe "when parsing a broker unavailable packet" do
911
- before(:each) do
912
- @packet = MQTT::Packet.parse( "\x20\x02\x00\x05" )
901
+ let(:packet) do
902
+ MQTT::Packet.parse( "\x20\x02\x00\x05" )
913
903
  end
914
904
 
915
905
  it "should correctly create the right type of packet object" do
916
- @packet.class.should == MQTT::Packet::Connack
906
+ packet.class.should == MQTT::Packet::Connack
917
907
  end
918
908
 
919
909
  it "should set the return code of the packet correctly" do
920
- @packet.return_code.should == 0x05
910
+ packet.return_code.should == 0x05
921
911
  end
922
912
 
923
913
  it "should set the return message of the packet correctly" do
924
- @packet.return_msg.should match(/not authorised/i)
914
+ packet.return_msg.should match(/not authorised/i)
925
915
  end
926
916
  end
927
917
 
928
918
  describe "when parsing an unknown connection refused packet" do
929
- before(:each) do
930
- @packet = MQTT::Packet.parse( "\x20\x02\x00\x10" )
931
- end
919
+ let(:packet) { MQTT::Packet.parse( "\x20\x02\x00\x10" ) }
932
920
 
933
921
  it "should correctly create the right type of packet object" do
934
- @packet.class.should == MQTT::Packet::Connack
922
+ packet.class.should == MQTT::Packet::Connack
935
923
  end
936
924
 
937
925
  it "should set the return code of the packet correctly" do
938
- @packet.return_code.should == 0x10
926
+ packet.return_code.should == 0x10
939
927
  end
940
928
 
941
929
  it "should set the return message of the packet correctly" do
942
- @packet.return_msg.should match(/Connection refused: error code 16/i)
930
+ packet.return_msg.should match(/Connection refused: error code 16/i)
943
931
  end
944
932
  end
945
933
 
946
934
  describe "when parsing packet with extra bytes on the end" do
947
935
  it "should throw an exception" do
948
936
  lambda {
949
- @packet = MQTT::Packet.parse( "\x20\x03\x00\x00\x00" )
937
+ packet = MQTT::Packet.parse( "\x20\x03\x00\x00\x00" )
950
938
  }.should raise_error(
951
939
  MQTT::ProtocolException,
952
940
  "Extra bytes at end of Connect Acknowledgment packet"
@@ -975,23 +963,21 @@ describe MQTT::Packet::Puback do
975
963
  end
976
964
 
977
965
  describe "when parsing a packet" do
978
- before(:each) do
979
- @packet = MQTT::Packet.parse( "\x40\x02\x12\x34" )
980
- end
966
+ let(:packet) { MQTT::Packet.parse( "\x40\x02\x12\x34" ) }
981
967
 
982
968
  it "should correctly create the right type of packet object" do
983
- @packet.class.should == MQTT::Packet::Puback
969
+ packet.class.should == MQTT::Packet::Puback
984
970
  end
985
971
 
986
972
  it "should set the message id of the packet correctly" do
987
- @packet.message_id.should == 0x1234
973
+ packet.message_id.should == 0x1234
988
974
  end
989
975
  end
990
976
 
991
977
  describe "when parsing packet with extra bytes on the end" do
992
978
  it "should throw an exception" do
993
979
  lambda {
994
- @packet = MQTT::Packet.parse( "\x40\x03\x12\x34\x00" )
980
+ packet = MQTT::Packet.parse( "\x40\x03\x12\x34\x00" )
995
981
  }.should raise_error(
996
982
  MQTT::ProtocolException,
997
983
  "Extra bytes at end of Publish Acknowledgment packet"
@@ -1014,23 +1000,21 @@ describe MQTT::Packet::Pubrec do
1014
1000
  end
1015
1001
 
1016
1002
  describe "when parsing a packet" do
1017
- before(:each) do
1018
- @packet = MQTT::Packet.parse( "\x50\x02\x12\x34" )
1019
- end
1003
+ let(:packet) { MQTT::Packet.parse( "\x50\x02\x12\x34" ) }
1020
1004
 
1021
1005
  it "should correctly create the right type of packet object" do
1022
- @packet.class.should == MQTT::Packet::Pubrec
1006
+ packet.class.should == MQTT::Packet::Pubrec
1023
1007
  end
1024
1008
 
1025
1009
  it "should set the message id of the packet correctly" do
1026
- @packet.message_id.should == 0x1234
1010
+ packet.message_id.should == 0x1234
1027
1011
  end
1028
1012
  end
1029
1013
 
1030
1014
  describe "when parsing packet with extra bytes on the end" do
1031
1015
  it "should throw an exception" do
1032
1016
  lambda {
1033
- @packet = MQTT::Packet.parse( "\x50\x03\x12\x34\x00" )
1017
+ packet = MQTT::Packet.parse( "\x50\x03\x12\x34\x00" )
1034
1018
  }.should raise_error(
1035
1019
  MQTT::ProtocolException,
1036
1020
  "Extra bytes at end of Publish Received packet"
@@ -1053,23 +1037,21 @@ describe MQTT::Packet::Pubrel do
1053
1037
  end
1054
1038
 
1055
1039
  describe "when parsing a packet" do
1056
- before(:each) do
1057
- @packet = MQTT::Packet.parse( "\x60\x02\x12\x34" )
1058
- end
1040
+ let(:packet) { MQTT::Packet.parse( "\x60\x02\x12\x34" ) }
1059
1041
 
1060
1042
  it "should correctly create the right type of packet object" do
1061
- @packet.class.should == MQTT::Packet::Pubrel
1043
+ packet.class.should == MQTT::Packet::Pubrel
1062
1044
  end
1063
1045
 
1064
1046
  it "should set the message id of the packet correctly" do
1065
- @packet.message_id.should == 0x1234
1047
+ packet.message_id.should == 0x1234
1066
1048
  end
1067
1049
  end
1068
1050
 
1069
1051
  describe "when parsing packet with extra bytes on the end" do
1070
1052
  it "should throw an exception" do
1071
1053
  lambda {
1072
- @packet = MQTT::Packet.parse( "\x60\x03\x12\x34\x00" )
1054
+ packet = MQTT::Packet.parse( "\x60\x03\x12\x34\x00" )
1073
1055
  }.should raise_error(
1074
1056
  MQTT::ProtocolException,
1075
1057
  "Extra bytes at end of Publish Release packet"
@@ -1092,23 +1074,21 @@ describe MQTT::Packet::Pubcomp do
1092
1074
  end
1093
1075
 
1094
1076
  describe "when parsing a packet" do
1095
- before(:each) do
1096
- @packet = MQTT::Packet.parse( "\x70\x02\x12\x34" )
1097
- end
1077
+ let(:packet) { MQTT::Packet.parse( "\x70\x02\x12\x34" ) }
1098
1078
 
1099
1079
  it "should correctly create the right type of packet object" do
1100
- @packet.class.should == MQTT::Packet::Pubcomp
1080
+ packet.class.should == MQTT::Packet::Pubcomp
1101
1081
  end
1102
1082
 
1103
1083
  it "should set the message id of the packet correctly" do
1104
- @packet.message_id.should == 0x1234
1084
+ packet.message_id.should == 0x1234
1105
1085
  end
1106
1086
  end
1107
1087
 
1108
1088
  describe "when parsing packet with extra bytes on the end" do
1109
1089
  it "should throw an exception" do
1110
1090
  lambda {
1111
- @packet = MQTT::Packet.parse( "\x70\x03\x12\x34\x00" )
1091
+ MQTT::Packet.parse( "\x70\x03\x12\x34\x00" )
1112
1092
  }.should raise_error(
1113
1093
  MQTT::ProtocolException,
1114
1094
  "Extra bytes at end of Publish Complete packet"
@@ -1124,38 +1104,36 @@ end
1124
1104
 
1125
1105
  describe MQTT::Packet::Subscribe do
1126
1106
  describe "setting the packet's topics" do
1127
- before(:each) do
1128
- @packet = MQTT::Packet::Subscribe.new
1129
- end
1107
+ let(:packet) { MQTT::Packet::Subscribe.new }
1130
1108
 
1131
1109
  it "should be able to set the topics from a String 'a/b'" do
1132
- @packet.topics = 'a/b'
1133
- @packet.topics.should == [["a/b", 0]]
1110
+ packet.topics = 'a/b'
1111
+ packet.topics.should == [["a/b", 0]]
1134
1112
  end
1135
1113
 
1136
1114
  it "should be able to set the multiple topics from an array ['a/b', 'b/c']" do
1137
- @packet.topics = ['a/b', 'b/c']
1138
- @packet.topics.should == [["a/b", 0], ['b/c', 0]]
1115
+ packet.topics = ['a/b', 'b/c']
1116
+ packet.topics.should == [["a/b", 0], ['b/c', 0]]
1139
1117
  end
1140
1118
 
1141
1119
  it "should be able to set the topics from a Hash {'a/b' => 0, 'b/c' => 1}" do
1142
- @packet.topics = {'a/b' => 0, 'b/c' => 1}
1143
- @packet.topics.should == [["a/b", 0], ["b/c", 1]]
1120
+ packet.topics = {'a/b' => 0, 'b/c' => 1}
1121
+ packet.topics.should == [["a/b", 0], ["b/c", 1]]
1144
1122
  end
1145
1123
 
1146
1124
  it "should be able to set the topics from a single level array ['a/b', 0]" do
1147
- @packet.topics = ['a/b', 0]
1148
- @packet.topics.should == [["a/b", 0]]
1125
+ packet.topics = ['a/b', 0]
1126
+ packet.topics.should == [["a/b", 0]]
1149
1127
  end
1150
1128
 
1151
1129
  it "should be able to set the topics from a two level array [['a/b' => 0], ['b/c' => 1]]" do
1152
- @packet.topics = [['a/b', 0], ['b/c', 1]]
1153
- @packet.topics.should == [['a/b', 0], ['b/c', 1]]
1130
+ packet.topics = [['a/b', 0], ['b/c', 1]]
1131
+ packet.topics.should == [['a/b', 0], ['b/c', 1]]
1154
1132
  end
1155
1133
 
1156
1134
  it "should throw an exception when setting topic with a non-string" do
1157
1135
  lambda {
1158
- @packet.topics = 56
1136
+ packet.topics = 56
1159
1137
  }.should raise_error(
1160
1138
  'Invalid topics input: 56'
1161
1139
  )
@@ -1183,46 +1161,42 @@ describe MQTT::Packet::Subscribe do
1183
1161
  end
1184
1162
 
1185
1163
  describe "when parsing a packet with a single topic" do
1186
- before(:each) do
1187
- @packet = MQTT::Packet.parse( "\x82\x08\x00\x01\x00\x03a/b\x00" )
1188
- end
1164
+ let(:packet) { MQTT::Packet.parse( "\x82\x08\x00\x01\x00\x03a/b\x00" ) }
1189
1165
 
1190
1166
  it "should correctly create the right type of packet object" do
1191
- @packet.class.should == MQTT::Packet::Subscribe
1167
+ packet.class.should == MQTT::Packet::Subscribe
1192
1168
  end
1193
1169
 
1194
1170
  it "should set the QOS level correctly" do
1195
- @packet.qos.should == 1
1171
+ packet.qos.should == 1
1196
1172
  end
1197
1173
 
1198
1174
  it "should set the Message ID correctly" do
1199
- @packet.message_id.should == 1
1175
+ packet.message_id.should == 1
1200
1176
  end
1201
1177
 
1202
1178
  it "should set the topic name correctly" do
1203
- @packet.topics.should == [['a/b',0]]
1179
+ packet.topics.should == [['a/b',0]]
1204
1180
  end
1205
1181
  end
1206
1182
 
1207
1183
  describe "when parsing a packet with a two topics" do
1208
- before(:each) do
1209
- @packet = MQTT::Packet.parse( "\x82\x0e\000\x06\x00\x03a/b\x00\x00\x03c/d\x01" )
1210
- end
1184
+ let(:packet) { MQTT::Packet.parse( "\x82\x0e\000\x06\x00\x03a/b\x00\x00\x03c/d\x01" ) }
1211
1185
 
1212
1186
  it "should correctly create the right type of packet object" do
1213
- @packet.class.should == MQTT::Packet::Subscribe
1187
+ packet.class.should == MQTT::Packet::Subscribe
1214
1188
  end
1215
1189
 
1216
1190
  it "should set the QOS level correctly" do
1217
- @packet.qos.should == 1
1191
+ packet.qos.should == 1
1218
1192
  end
1219
1193
 
1220
1194
  it "should set the Message ID correctly" do
1221
- @packet.message_id.should == 6
1195
+ packet.message_id.should == 6
1222
1196
  end
1223
1197
 
1224
1198
  it "should set the topic name correctly" do
1225
- @packet.topics.should == [['a/b',0],['c/d',1]]
1199
+ packet.topics.should == [['a/b',0],['c/d',1]]
1226
1200
  end
1227
1201
  end
1228
1202
 
@@ -1269,38 +1243,34 @@ describe MQTT::Packet::Suback do
1269
1243
  end
1270
1244
 
1271
1245
  describe "when parsing a packet with a single QOS value of 0" do
1272
- before(:each) do
1273
- @packet = MQTT::Packet.parse( "\x90\x03\x12\x34\x00" )
1274
- end
1246
+ let(:packet) { MQTT::Packet.parse( "\x90\x03\x12\x34\x00" ) }
1275
1247
 
1276
1248
  it "should correctly create the right type of packet object" do
1277
- @packet.class.should == MQTT::Packet::Suback
1249
+ packet.class.should == MQTT::Packet::Suback
1278
1250
  end
1279
1251
 
1280
1252
  it "should set the message id of the packet correctly" do
1281
- @packet.message_id.should == 0x1234
1253
+ packet.message_id.should == 0x1234
1282
1254
  end
1283
1255
 
1284
1256
  it "should set the Granted QOS of the packet correctly" do
1285
- @packet.granted_qos.should == [0]
1257
+ packet.granted_qos.should == [0]
1286
1258
  end
1287
1259
  end
1288
1260
 
1289
1261
  describe "when parsing a packet with two QOS values" do
1290
- before(:each) do
1291
- @packet = MQTT::Packet.parse( "\x90\x04\x12\x34\x01\x01" )
1292
- end
1262
+ let(:packet) { MQTT::Packet.parse( "\x90\x04\x12\x34\x01\x01" ) }
1293
1263
 
1294
1264
  it "should correctly create the right type of packet object" do
1295
- @packet.class.should == MQTT::Packet::Suback
1265
+ packet.class.should == MQTT::Packet::Suback
1296
1266
  end
1297
1267
 
1298
1268
  it "should set the message id of the packet correctly" do
1299
- @packet.message_id.should == 0x1234
1269
+ packet.message_id.should == 0x1234
1300
1270
  end
1301
1271
 
1302
1272
  it "should set the Granted QOS of the packet correctly" do
1303
- @packet.granted_qos.should == [1,1]
1273
+ packet.granted_qos.should == [1,1]
1304
1274
  end
1305
1275
  end
1306
1276
 
@@ -1339,20 +1309,18 @@ describe MQTT::Packet::Unsubscribe do
1339
1309
  end
1340
1310
 
1341
1311
  describe "when parsing a packet" do
1342
- before(:each) do
1343
- @packet = MQTT::Packet.parse( "\xa2\f\000\005\000\003a/b\000\003c/d" )
1344
- end
1312
+ let(:packet) { MQTT::Packet.parse( "\xa2\f\000\005\000\003a/b\000\003c/d" ) }
1345
1313
 
1346
1314
  it "should correctly create the right type of packet object" do
1347
- @packet.class.should == MQTT::Packet::Unsubscribe
1315
+ packet.class.should == MQTT::Packet::Unsubscribe
1348
1316
  end
1349
1317
 
1350
1318
  it "should set the QOS level correctly" do
1351
- @packet.qos.should == 1
1319
+ packet.qos.should == 1
1352
1320
  end
1353
1321
 
1354
1322
  it "should set the topic name correctly" do
1355
- @packet.topics.should == ['a/b','c/d']
1323
+ packet.topics.should == ['a/b','c/d']
1356
1324
  end
1357
1325
  end
1358
1326
 
@@ -1378,23 +1346,23 @@ describe MQTT::Packet::Unsuback do
1378
1346
  end
1379
1347
 
1380
1348
  describe "when parsing a packet" do
1381
- before(:each) do
1382
- @packet = MQTT::Packet.parse( "\xB0\x02\x12\x34" )
1349
+ let(:packet) do
1350
+ MQTT::Packet.parse( "\xB0\x02\x12\x34" )
1383
1351
  end
1384
1352
 
1385
1353
  it "should correctly create the right type of packet object" do
1386
- @packet.class.should == MQTT::Packet::Unsuback
1354
+ packet.class.should == MQTT::Packet::Unsuback
1387
1355
  end
1388
1356
 
1389
1357
  it "should set the message id of the packet correctly" do
1390
- @packet.message_id.should == 0x1234
1358
+ packet.message_id.should == 0x1234
1391
1359
  end
1392
1360
  end
1393
1361
 
1394
1362
  describe "when parsing packet with extra bytes on the end" do
1395
1363
  it "should throw an exception" do
1396
1364
  lambda {
1397
- @packet = MQTT::Packet.parse( "\xB0\x03\x12\x34\x00" )
1365
+ packet = MQTT::Packet.parse( "\xB0\x03\x12\x34\x00" )
1398
1366
  }.should raise_error(
1399
1367
  MQTT::ProtocolException,
1400
1368
  "Extra bytes at end of Unsubscribe Acknowledgment packet"