Exemplo n.º 1
0
def _parse_ami(buffer_):
    result = []

    def aux(event_name, action_id, message):
        result.append(message)

    parser.parse_buffer(buffer_, aux, aux)

    return result
Exemplo n.º 2
0
    def test_incomplete_channel_variables_is_ignored(self):
        msg = textwrap.dedent('''\
            Event: some-event\r
            ChanVariable: FOO\r
            \r
            ''').encode('utf-8')

        parse_buffer(msg, self.mock_event_callback,
                     self.mock_response_callback)

        assert_that(self.mock_event_callback.call_count, equal_to(0))
        assert_that(self.mock_response_callback.call_count, equal_to(0))
Exemplo n.º 3
0
    def test_given_incomplete_character_when_parse_buffer_then_incomplete_character_returned(
        self, ):
        msg = b"foo\xc3"

        unparsed_buffer = parse_buffer(msg, self.mock_event_callback,
                                       self.mock_response_callback)

        assert_that(unparsed_buffer, equal_to(msg))
Exemplo n.º 4
0
    def test_given_incomplete_message_when_parse_buffer_then_whole_buffer_returned(
        self, ):
        raw_buffer = b'incomplete'

        unparsed_buffer = parse_buffer(raw_buffer, self.mock_event_callback,
                                       self.mock_response_callback)

        assert_that(raw_buffer, equal_to(unparsed_buffer))
Exemplo n.º 5
0
    def test_given_unknown_message_when_parse_buffer_then_no_callback(self):
        msg = b"unknown: message" + MESSAGE_DELIMITER

        unparsed_buffer = parse_buffer(msg, self.mock_event_callback,
                                       self.mock_response_callback)

        assert_that(unparsed_buffer, equal_to(b''))
        assert_that(self.mock_event_callback.call_count, equal_to(0))
        assert_that(self.mock_response_callback.call_count, equal_to(0))
Exemplo n.º 6
0
    def test_channel_variables_with_double_newline_is_ignored(self):
        msg = textwrap.dedent('''\
            Event: some-event\r
            ChanVariable: FOO=newline\r
            \r
            newline\r
            \r
            newline\r
            \r
            ChanVariable: BAZ=inga\r
            \r
            ''').encode('utf-8')

        parse_buffer(msg, self.mock_event_callback,
                     self.mock_response_callback)

        assert_that(self.mock_event_callback.call_count, equal_to(1))
        assert_that(self.mock_response_callback.call_count, equal_to(0))
Exemplo n.º 7
0
    def test_given_complete_message_when_parse_buffer_then_callback_and_buffer_emptied(
        self, ):
        complete_msg = 'complete message'
        raw_buffer = EVENT_DELIMITER + complete_msg.encode(
            'utf-8') + MESSAGE_DELIMITER

        unparsed_buffer = parse_buffer(raw_buffer, self.mock_event_callback,
                                       self.mock_response_callback)

        assert_that(unparsed_buffer, equal_to(b''))
        self.mock_event_callback.assert_called_once_with(
            complete_msg, None, {'Event': complete_msg})
Exemplo n.º 8
0
    def test_given_event_with_chan_variables_when_parse_buffer_then_chan_variables_are_parsed(
        self, ):
        msg = textwrap.dedent('''\
            Event: some-event\r
            ChanVariable: FOO=bar\r
            ChanVariable: BAZ=inga\r
            \r
            ''').encode('utf-8')
        expected_event = {
            'Event': 'some-event',
            'ChanVariable': {
                'FOO': 'bar',
                'BAZ': 'inga'
            },
        }

        parse_buffer(msg, self.mock_event_callback,
                     self.mock_response_callback)

        self.mock_event_callback.assert_any_call('some-event', None,
                                                 expected_event)
Exemplo n.º 9
0
    def test_given_complete_messages_when_parse_buffer_then_multiple_callback_and_buffer_emptied(
        self, ):
        first_msg = "first complete message"
        second_msg = "second complete message"
        raw_buffer = (EVENT_DELIMITER + first_msg.encode('utf-8') +
                      MESSAGE_DELIMITER + RESPONSE_DELIMITER +
                      second_msg.encode('utf-8') + MESSAGE_DELIMITER)

        unparsed_buffer = parse_buffer(raw_buffer, self.mock_event_callback,
                                       self.mock_response_callback)

        assert_that(unparsed_buffer, equal_to(b''))
        self.mock_event_callback.assert_any_call(first_msg, None,
                                                 {'Event': first_msg})
        self.mock_response_callback.assert_any_call(second_msg, None,
                                                    {'Response': second_msg})
Exemplo n.º 10
0
# Copyright 2017-2020 The Wazo Authors  (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later

import os

from wazo_amid.ami.parser import parse_buffer

__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__)))


def pass_(*args, **kwargs):
    pass


with open(os.path.join(__location__, 'ami-messages.txt'), 'rb') as f:
    sample = f.read().replace('\n', '\r\n')

ami_stream = sample * 50

parse_buffer(ami_stream, pass_, pass_)
Exemplo n.º 11
0
 def _parse_buffer(self):
     self._buffer = parser.parse_buffer(self._buffer,
                                        self.event_parser_callback, None)