Ejemplo n.º 1
0
 def test_from_json(self):
     assert Message.from_json(dumps([])) == []
     expected = [{'channel': '/test', 'id': '1'}]
     messages = Message.from_json(dumps(expected[0]))
     assert messages == expected
     for message in messages:
         assert isinstance(message, Message)
     expected = [
         {'channel': '/test1', 'id': '1'},
         {'channel': '/test2', 'id': '2'}
     ]
     messages = Message.from_json(dumps(expected))
     assert messages == expected
     for message in messages:
         assert isinstance(message, Message)
Ejemplo n.º 2
0
 def test_from_json_with_encoding(self):
     expected = [{'channel': u'/caf\xe9', 'id': '1'}]
     value = dumps(expected, ensure_ascii=False).encode('utf8')
     messages = Message.from_json(value, encoding='utf8')
     assert messages == expected
     for message in messages:
         assert isinstance(message, Message)
Ejemplo n.º 3
0
 def test_from_json_with_encoding(self):
     expected = [{'channel': '/caf\xe9', 'id': '1'}]
     value = dumps(expected, ensure_ascii=False).encode('utf8')
     messages = Message.from_json(value, encoding='utf8')
     assert messages == expected
     for message in messages:
         assert isinstance(message, Message)
Ejemplo n.º 4
0
    def _handle_response(self, response, messages):

        # Log the received response code and headers
        self.log.debug('Received response: %s' % response.code)
        for header, value in response.headers.get_all():
            self.log.debug('Response header %s: %s' % (header, value))

        # If there was an error, report the sent messages as failed
        if response.error:
            if isinstance(response.error, HTTPError):
                if response.error.code == 599:
                    error = errors.TimeoutError()
                else:
                    error = errors.ServerError(response.error.code)
            else:
                error = errors.CommunicationError(response.error)
            self.log.debug('Failed to send messages: %s' % error)
            self._client.fail_messages(messages, error)
            return

        # Update the cookies
        self.update_cookies(
            response.headers.get_list('Set-Cookie'),
            time_received=response.headers.get('Date')
        )

        # Get the received messages
        self.log.debug('Received body: %s' % response.body)
        messages = Message.from_json(response.body, encoding='utf8')
        self._client.receive_messages(messages)
Ejemplo n.º 5
0
    def _handle_response(self, response, messages):

        # Log the received response code and headers
        self.log.debug('Received response: %s' % response.code)
        for header, value in response.headers.get_all():
            self.log.debug('Response header %s: %s' % (header, value))

        # If there was an error, report the sent messages as failed
        if response.error:
            if isinstance(response.error, HTTPError):
                if response.error.code == 599:
                    error = errors.TimeoutError()
                else:
                    error = errors.ServerError(response.error.code)
            else:
                error = errors.CommunicationError(response.error)
            self.log.debug('Failed to send messages: %s' % error)
            self._client.fail_messages(messages, error)
            return

        # Update the cookies
        self.update_cookies(
            response.headers.get_list('Set-Cookie'),
            time_received=response.headers.get('Date')
        )

        # Get the received messages
        self.log.debug('Received body: %s' % response.body)
        messages = Message.from_json(response.body, encoding='utf8')
        self._client.receive_messages(messages)
Ejemplo n.º 6
0
 def test_from_json(self):
     assert Message.from_json(dumps([])) == []
     expected = [{'channel': '/test', 'id': '1'}]
     messages = Message.from_json(dumps(expected[0]))
     assert messages == expected
     for message in messages:
         assert isinstance(message, Message)
     expected = [{
         'channel': '/test1',
         'id': '1'
     }, {
         'channel': '/test2',
         'id': '2'
     }]
     messages = Message.from_json(dumps(expected))
     assert messages == expected
     for message in messages:
         assert isinstance(message, Message)