Exemplo n.º 1
0
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
Exemplo n.º 2
0
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')
Exemplo n.º 3
0
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', )
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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'