Example #1
0
    async def receive(self):
        multipart = await self.socket.recv_multipart()
        parsed_message = parse_multipart_message(multipart)

        if not parsed_message:
            log.error('Received junk off the wire')
            raise MercuryClientException('Message is malformed')

        try:
            message = msgpack.unpackb(parsed_message['message'],
                                      encoding='utf-8')
        except TypeError as type_error:
            log.error('Received unpacked, non-string type: %s : %s' %
                      (type(parsed_message), type_error))
            await self.send_error(parsed_message['address'],
                                  'Client error, message is not packed')
            raise MercuryClientException('Message is malformed')

        except (msgpack.UnpackException,
                msgpack.ExtraData) as msgpack_exception:
            log.error('Received invalid request: %s' % str(msgpack_exception))

            await self.send_error(parsed_message['address'],
                                  'Client error, message is malformed')
            raise MercuryClientException('Message is malformed')

        return parsed_message['address'], message
Example #2
0
def test_parse_multipart_message_error():
    """Test parse_multipart_message() with a 2-parts message."""
    message = ['addr', '']
    parsed_message = transport.parse_multipart_message(message)
    assert parsed_message == {}
Example #3
0
def test_parse_multipart_message():
    """Test parse_multipart_message()"""
    message = ['addr', '', 'msg']
    parsed_message = transport.parse_multipart_message(message)
    assert parsed_message == {'address': ['addr', ''], 'message': 'msg'}