Example #1
0
def test_contentFormat():
    '''Read content format in PUT request from bytes, and reserialize'''
    msg = msgModule.buildFrom(textContentPutMsg)
    
    assert len(msg.options)      == 2
    assert msg.options[0].type   == coap.OptionType.UriPath
    assert msg.options[1].type   == coap.OptionType.ContentFormat
    assert msg.options[1].value  == coap.MediaType.TextPlain

    assert msgModule.serialize(msg) == textContentPutMsg
Example #2
0
 def handle_write(self):
     if self._outgoing:
         message  = self._outgoing.pop(0)
         msgArray = msgModule.serialize(message)
         if log.isEnabledFor(logging.DEBUG):
             hexstr = ' '.join(['{:02x}'.format(b) for b in msgArray])
             log.debug('Send message to {0}; data (hex) {1}'.format(message.address, hexstr))
         else:
             log.info('Send message to {0}'.format(message.address))
             
         self.socket.sendto(msgArray, message.address)
Example #3
0
def test_binaryContentFormat():
    '''Read binary content format in PUT request from bytes, and reserialize'''
    msg = msgModule.buildFrom(binaryContentPutMsg)
    
    assert len(msg.options)      == 2
    assert msg.options[0].type   == coap.OptionType.UriPath
    assert msg.options[1].type   == coap.OptionType.ContentFormat
    assert msg.options[1].value  == coap.MediaType.OctetStream
    
    assert msg.payload == b'\x33'

    assert msgModule.serialize(msg) == binaryContentPutMsg
Example #4
0
def test_jsonContentFormat():
    '''Read JSON content format in POST request from bytes, and reserialize'''
    msg = msgModule.buildFrom(jsonContentPostMsg)
    
    assert len(msg.options)      == 2
    assert msg.options[0].type   == coap.OptionType.UriPath
    assert msg.options[1].type   == coap.OptionType.ContentFormat
    assert msg.options[1].value  == coap.MediaType.Json
    
    payload = msg.jsonPayload()
    assert 'v' in payload
    assert payload['v'] == -69

    assert msgModule.serialize(msg) == jsonContentPostMsg
Example #5
0
def test_simplePut():
    '''Read PUT request from bytes, and reserialize'''
    msg = msgModule.buildFrom(pingPutMsg)
    
    assert msg.version     == 1
    assert msg.messageType == coap.MessageType.NON
    assert msg.tokenLength == 0
    assert msg.codeClass   == coap.CodeClass.Request
    assert msg.codeDetail  == coap.RequestCode.PUT
    assert msg.messageId   == 791  # 0x0317
    assert msg.token       == None
    
    assert len(msg.options)     == 1
    assert msg.options[0].value == 'ping'
    
    assert msg.payload == b'2014,125'
    
    assert msgModule.serialize(msg) == pingPutMsg
Example #6
0
def test_simpleGet():
    '''Read GET request from bytes, and reserialize'''
    msg = msgModule.buildFrom(verGetMsg)
    
    assert msg.version     == 1
    assert msg.messageType == coap.MessageType.CON
    assert msg.tokenLength == 0
    assert msg.codeClass   == coap.CodeClass.Request
    assert msg.codeDetail  == coap.RequestCode.GET
    assert msg.messageId   == 27689  # 0x6C29
    assert msg.token       == None
    
    assert len(msg.options)     == 1
    assert msg.options[0].value == 'ver'
    
    assert msg.absolutePath()  == '/ver'
    
    assert msgModule.serialize(msg) == verGetMsg
Example #7
0
def test_token():
    '''Read token in GET request from bytes, and reserialize'''
    msg = msgModule.buildFrom(tokenMsg)
    
    assert msg.token == b'\x66'
    assert msgModule.serialize(msg) == tokenMsg