kopia lustrzana https://github.com/Yakifo/amqtt
Add Unsuback packet
rodzic
82479dfc18
commit
1cc9d53364
|
@ -0,0 +1,22 @@
|
|||
# Copyright (c) 2015 Nicolas JOUANIN
|
||||
#
|
||||
# See the file license.txt for copying permission.
|
||||
from hbmqtt.mqtt.packet import MQTTPacket, MQTTFixedHeader, PacketType, PacketIdVariableHeader
|
||||
from hbmqtt.errors import HBMQTTException
|
||||
|
||||
|
||||
class UnsubackPacket(MQTTPacket):
|
||||
VARIABLE_HEADER = PacketIdVariableHeader
|
||||
PAYLOAD = None
|
||||
|
||||
def __init__(self, fixed: MQTTFixedHeader=None, variable_header: PacketIdVariableHeader=None, payload=None):
|
||||
if fixed is None:
|
||||
header = MQTTFixedHeader(PacketType.UNSUBACK, 0x00)
|
||||
else:
|
||||
if fixed.packet_type is not PacketType.UNSUBACK:
|
||||
raise HBMQTTException("Invalid fixed packet type %s for UnsubackPacket init" % fixed.packet_type)
|
||||
header = fixed
|
||||
|
||||
super().__init__(header)
|
||||
self.variable_header = variable_header
|
||||
self.payload = payload
|
|
@ -0,0 +1,25 @@
|
|||
# Copyright (c) 2015 Nicolas JOUANIN
|
||||
#
|
||||
# See the file license.txt for copying permission.
|
||||
import unittest
|
||||
|
||||
from hbmqtt.mqtt.unsuback import UnsubackPacket
|
||||
from hbmqtt.mqtt.packet import PacketIdVariableHeader
|
||||
from hbmqtt.codecs import *
|
||||
|
||||
class SubscribePacketTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.loop = asyncio.new_event_loop()
|
||||
|
||||
def test_from_stream(self):
|
||||
data = b'\xb0\x02\x00\x0a'
|
||||
stream = asyncio.StreamReader(loop=self.loop)
|
||||
stream.feed_data(data)
|
||||
stream.feed_eof()
|
||||
message = self.loop.run_until_complete(UnsubackPacket.from_stream(stream))
|
||||
self.assertEqual(message.variable_header.packet_id, 10)
|
||||
|
||||
def test_to_stream(self):
|
||||
variable_header = PacketIdVariableHeader(10)
|
||||
publish = UnsubackPacket(variable_header=variable_header)
|
||||
self.assertEqual(out, b'\xb0\x02\x00\x0a')
|
|
@ -25,5 +25,4 @@ class SubscribePacketTest(unittest.TestCase):
|
|||
payload = UnubscribePayload(['a/b', 'c/d'])
|
||||
publish = UnsubscribePacket(variable_header=variable_header, payload=payload)
|
||||
out = publish.to_bytes()
|
||||
print(out)
|
||||
self.assertEqual(out, b'\xa0\x0c\x00\n\x00\x03a/b\x00\x03c/d')
|
Ładowanie…
Reference in New Issue