예제 #1
0
    def test_publish_recv_qos2(self):
        self.called = False

        def onPublish(topic, payload, qos, dup, retain, msgId):
            self.called = True
            self.topic = topic
            self.payload = payload.decode('utf-8')
            self.qos = qos
            self.retain = retain
            self.msgId = msgId
            self.dup = dup

        self.protocol.onPublish = onPublish
        pub = PUBLISH()
        pub.qos = 2
        pub.dup = False
        pub.retain = False
        pub.topic = "foo/bar/baz2"
        pub.msgId = 1
        pub.payload = "Hello world 2"
        self.protocol.dataReceived(pub.encode())
        self.transport.clear()
        self.assertEqual(self.called, False)
        rel = PUBREL()
        rel.msgId = pub.msgId
        self.protocol.dataReceived(rel.encode())
        self.assertEqual(self.topic, pub.topic)
        self.assertEqual(self.payload, pub.payload)
        self.assertEqual(self.qos, pub.qos)
        self.assertEqual(self.retain, pub.retain)
        self.assertEqual(self.msgId, pub.msgId)
        self.assertEqual(self.dup, pub.dup)
예제 #2
0
 def test_publish_recv_qos2(self):
     self.called = False
     def onPublish(topic, payload, qos, dup, retain, msgId):
         self.called = True
         self.topic   = topic
         self.payload = payload.decode('utf-8')
         self.qos     = qos
         self.retain  = retain
         self.msgId   = msgId 
         self.dup     = dup
     self.protocol.onPublish = onPublish
     pub =PUBLISH()
     pub.qos     = 2
     pub.dup     = False
     pub.retain  = False
     pub.topic   = "foo/bar/baz2"
     pub.msgId   = 1
     pub.payload = "Hello world 2"
     self.protocol.dataReceived(pub.encode())
     self.transport.clear()
     self.assertEqual(self.called, False)
     rel = PUBREL()
     rel.msgId = pub.msgId
     self.protocol.dataReceived(rel.encode())
     self.assertEqual(self.topic,   pub.topic)
     self.assertEqual(self.payload, pub.payload)
     self.assertEqual(self.qos,     pub.qos)
     self.assertEqual(self.retain,  pub.retain)
     self.assertEqual(self.msgId,   pub.msgId )
     self.assertEqual(self.dup,     pub.dup )
예제 #3
0
 def test_PUBLISH_encdec_payload_int(self):
     request  = PUBLISH()
     request.msgId   = 30001
     request.qos     = 1
     request.dup     = False
     request.retain  = True
     request.topic   = "foo"
     request.payload = 65537
     self.assertRaises(TypeError, request.encode)
예제 #4
0
 def test_PUBLISH_encdec_payload_int(self):
     request = PUBLISH()
     request.msgId = 30001
     request.qos = 1
     request.dup = False
     request.retain = True
     request.topic = "foo"
     request.payload = 65537
     self.assertRaises(TypeError, request.encode)
예제 #5
0
 def test_PUBLISH_encdec_payload_bytearray(self):
     request  = PUBLISH()
     response = PUBLISH()
     request.msgId   = 30001
     request.qos     = 1
     request.dup     = False
     request.retain  = True
     request.topic   = "foo"
     request.payload = bytearray(5)
     
     response.decode(request.encode())
     self.assertEqual(request.msgId,   response.msgId)
     self.assertEqual(request.qos,     response.qos)
     self.assertEqual(request.dup,     response.dup)
     self.assertEqual(request.retain,  response.retain)
     self.assertEqual(request.topic,   response.topic)
     self.assertEqual(request.payload, response.payload)
예제 #6
0
 def test_PUBLISH_encdec_retain(self):
     request  = PUBLISH()
     response = PUBLISH()
     request.msgId   = 30001
     request.qos     = 1
     request.dup     = False
     request.retain  = True
     request.topic   = "foo"
     request.payload = "foo"
     
     response.decode(request.encode())
     self.assertEqual(request.msgId,   response.msgId)
     self.assertEqual(request.qos,     response.qos)
     self.assertEqual(request.dup,     response.dup)
     self.assertEqual(request.retain,  response.retain)
     self.assertEqual(request.topic,   response.topic)
     self.assertEqual(request.payload, response.payload.decode(encoding='utf-8'))
예제 #7
0
    def test_PUBLISH_encdec_payload_bytearray(self):
        request = PUBLISH()
        response = PUBLISH()
        request.msgId = 30001
        request.qos = 1
        request.dup = False
        request.retain = True
        request.topic = "foo"
        request.payload = bytearray(5)

        response.decode(request.encode())
        self.assertEqual(request.msgId, response.msgId)
        self.assertEqual(request.qos, response.qos)
        self.assertEqual(request.dup, response.dup)
        self.assertEqual(request.retain, response.retain)
        self.assertEqual(request.topic, response.topic)
        self.assertEqual(request.payload, response.payload)
예제 #8
0
    def test_PUBLISH_encdec_retain(self):
        request = PUBLISH()
        response = PUBLISH()
        request.msgId = 30001
        request.qos = 1
        request.dup = False
        request.retain = True
        request.topic = "foo"
        request.payload = "foo"

        response.decode(request.encode())
        self.assertEqual(request.msgId, response.msgId)
        self.assertEqual(request.qos, response.qos)
        self.assertEqual(request.dup, response.dup)
        self.assertEqual(request.retain, response.retain)
        self.assertEqual(request.topic, response.topic)
        self.assertEqual(request.payload,
                         response.payload.decode(encoding='utf-8'))
예제 #9
0
 def test_publish_recv_qos0(self):
     def onPublish(topic, payload, qos, dup, retain, msgId):
         self.topic   = topic
         self.payload = payload.decode('utf-8')
         self.qos     = qos
         self.retain  = retain
         self.msgId   = msgId 
     self.protocol.onPublish = onPublish
     pub =PUBLISH()
     pub.qos     = 0
     pub.dup     = False
     pub.retain  = False
     pub.topic   = "foo/bar/baz0"
     pub.msgId   = None
     pub.payload = "Hello world 0"
     self.protocol.dataReceived(pub.encode())
     self.assertEqual(self.topic,   pub.topic)
     self.assertEqual(self.payload, pub.payload)
     self.assertEqual(self.qos,     pub.qos)
     self.assertEqual(self.retain,  pub.retain)
     self.assertEqual(self.msgId,   pub.msgId )
예제 #10
0
    def test_publish_recv_qos0(self):
        def onPublish(topic, payload, qos, dup, retain, msgId):
            self.topic = topic
            self.payload = payload.decode('utf-8')
            self.qos = qos
            self.retain = retain
            self.msgId = msgId

        self.protocol.onPublish = onPublish
        pub = PUBLISH()
        pub.qos = 0
        pub.dup = False
        pub.retain = False
        pub.topic = "foo/bar/baz0"
        pub.msgId = None
        pub.payload = "Hello world 0"
        self.protocol.dataReceived(pub.encode())
        self.assertEqual(self.topic, pub.topic)
        self.assertEqual(self.payload, pub.payload)
        self.assertEqual(self.qos, pub.qos)
        self.assertEqual(self.retain, pub.retain)
        self.assertEqual(self.msgId, pub.msgId)
예제 #11
0
 def test_publish_recv_qos1(self):
     def onPublish(topic, payload, qos, dup, retain, msgId):
         self.topic   = topic
         self.payload = payload.decode('utf-8')
         self.qos     = qos
         self.retain  = retain
         self.msgId   = msgId 
         self.dup     = dup
     self.protocol.setPublishHandler(onPublish)
     pub =PUBLISH()
     pub.qos     = 1
     pub.dup     = False
     pub.retain  = False
     pub.topic   = "foo/bar/baz1"
     pub.msgId   = 1
     pub.payload = "Hello world 1"
     self.protocol.dataReceived(pub.encode())
     self.transport.clear()
     self.assertEqual(self.topic,   pub.topic)
     self.assertEqual(self.payload, pub.payload)
     self.assertEqual(self.qos,     pub.qos)
     self.assertEqual(self.retain,  pub.retain)
     self.assertEqual(self.msgId,   pub.msgId )
     self.assertEqual(self.dup,     pub.dup )