def test_valid_will(): """ A valid will topic/message spec sets flags and payload. """ cs = mqttpacket.ConnectSpec( will_topic=u'my_will_topic', will_message=u'my_will_message', will_qos=1, ) wt = u'my_will_topic' wm = u'my_will_message' assert cs.will_topic == wt assert cs.will_message == wm assert cs.flags() == 0x0e assert len(cs.payload()) == 32 cs = mqttpacket.ConnectSpec( will_topic=u'wt2', will_message=u'wm2', will_qos=2, ) assert cs.will_topic == u'wt2' assert cs.will_message == u'wm2' assert cs.flags() == 0x16
def test_will_must_be_unicode(): """ Will topic and will message must be unicode. """ with pytest.raises(TypeError): mqttpacket.ConnectSpec(will_topic=b'foo', will_message=u'bar') with pytest.raises(TypeError): mqttpacket.ConnectSpec(will_topic=u'biz', will_message=b'baz')
def test_will_requirements(): """ Will topic and will message must be set together. """ with pytest.raises(ValueError): mqttpacket.ConnectSpec(will_topic=u'foo', ) with pytest.raises(ValueError): mqttpacket.ConnectSpec(will_message=u'my message', )
def test_will_qos_values(): """ Will QOS can only be 0 - 2 """ with pytest.raises(ValueError): mqttpacket.ConnectSpec(will_topic=u'biz', will_message=u'baz', will_qos=3) mqttpacket.ConnectSpec(will_topic=u'my_will_topic', will_message=u'my_will_message', will_qos=1) mqttpacket.ConnectSpec(will_topic=u'my_will_topic', will_message=u'my_will_message', will_qos=2)
def test_default_spec(): """ A default spec has a remaining length of zero and a clean session. """ cs = mqttpacket.ConnectSpec() assert not cs.payload() assert cs.flags() == 0x02
def test_connect_with_spec(): """ A valid connect spec is properly encoded. """ cs = mqttpacket.ConnectSpec( will_topic=u'my_will_topic', will_message=u'my_will_message', will_qos=1, ) packet = mqttpacket.connect(u'test', connect_spec=cs) assert isinstance(packet, bytes) assert len(packet) == 50 assert six.indexbytes(packet, 0) == 16 assert six.indexbytes(packet, 9) == 0x0e assert packet[14:18].decode('utf-8') == u'test'