2015-06-17 20:06:11 +00:00
|
|
|
# Copyright (c) 2015 Nicolas JOUANIN
|
|
|
|
#
|
|
|
|
# See the file license.txt for copying permission.
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from hbmqtt.mqtt.subscribe import SubscribePacket, SubscribePayload
|
|
|
|
from hbmqtt.mqtt.packet import PacketIdVariableHeader
|
2015-09-03 19:31:06 +00:00
|
|
|
from hbmqtt.mqtt.constants import *
|
2015-06-17 20:06:11 +00:00
|
|
|
from hbmqtt.codecs import *
|
2015-07-31 21:01:32 +00:00
|
|
|
from hbmqtt.adapters import BufferReader
|
|
|
|
|
2015-06-17 20:06:11 +00:00
|
|
|
|
|
|
|
class SubscribePacketTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
|
|
|
|
def test_from_stream(self):
|
|
|
|
data = b'\x80\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02'
|
2015-07-31 21:01:32 +00:00
|
|
|
stream = BufferReader(data)
|
2015-06-17 20:06:11 +00:00
|
|
|
message = self.loop.run_until_complete(SubscribePacket.from_stream(stream))
|
2015-09-03 19:31:06 +00:00
|
|
|
(topic, qos) = message.topics[0]
|
|
|
|
self.assertEqual(topic, 'a/b')
|
|
|
|
self.assertEqual(qos, QOS_1)
|
|
|
|
(topic, qos) = message.topics[1]
|
|
|
|
self.assertEqual(topic, 'c/d')
|
|
|
|
self.assertEqual(qos, QOS_2)
|
2015-06-17 20:06:11 +00:00
|
|
|
|
|
|
|
def test_to_stream(self):
|
|
|
|
variable_header = PacketIdVariableHeader(10)
|
|
|
|
payload = SubscribePayload(
|
|
|
|
[
|
2015-09-03 19:31:06 +00:00
|
|
|
('a/b', QOS_1),
|
|
|
|
('c/d', QOS_2)
|
2015-06-17 20:06:11 +00:00
|
|
|
])
|
|
|
|
publish = SubscribePacket(variable_header=variable_header, payload=payload)
|
|
|
|
out = publish.to_bytes()
|
2015-07-31 21:01:32 +00:00
|
|
|
self.assertEqual(out, b'\x82\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02')
|