2015-06-18 08:22:46 +00:00
|
|
|
# Copyright (c) 2015 Nicolas JOUANIN
|
|
|
|
#
|
|
|
|
# See the file license.txt for copying permission.
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from hbmqtt.mqtt.unsubscribe import UnsubscribePacket, UnubscribePayload
|
|
|
|
from hbmqtt.mqtt.packet import PacketIdVariableHeader
|
|
|
|
from hbmqtt.codecs import *
|
|
|
|
|
2015-06-29 20:37:01 +00:00
|
|
|
class UnsubscribePacketTest(unittest.TestCase):
|
2015-06-18 08:22:46 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
|
|
|
|
def test_from_stream(self):
|
2015-06-29 20:37:01 +00:00
|
|
|
data = b'\xa2\x0c\x00\n\x00\x03a/b\x00\x03c/d'
|
2015-06-18 08:22:46 +00:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(data)
|
|
|
|
stream.feed_eof()
|
|
|
|
message = self.loop.run_until_complete(UnsubscribePacket.from_stream(stream))
|
|
|
|
self.assertEqual(message.payload.topics[0], 'a/b')
|
|
|
|
self.assertEqual(message.payload.topics[1], 'c/d')
|
|
|
|
|
|
|
|
def test_to_stream(self):
|
|
|
|
variable_header = PacketIdVariableHeader(10)
|
|
|
|
payload = UnubscribePayload(['a/b', 'c/d'])
|
|
|
|
publish = UnsubscribePacket(variable_header=variable_header, payload=payload)
|
|
|
|
out = publish.to_bytes()
|
2015-06-29 20:37:01 +00:00
|
|
|
self.assertEqual(out, b'\xa2\x0c\x00\n\x00\x03a/b\x00\x03c/d')
|