def test_reset_type(): msg = Message() msg.type = RequestType() assert msg.type == TYPE_REQUEST msg.type = ResponseType() assert msg.type == TYPE_RESPONSE msg.type = RequestType() assert msg.type == TYPE_REQUEST
def new_reply(status, method, reason=None): """Initializes empty response SIP message Args: status (int) method (Method) reason (str) Returns: :obj:SipMessage response with parametrized status, method and reason. """ msg = Message() msg.type = ResponseType(status=status, reason=reason) sip_msg = SipMessage() sip_msg.raw_message = msg sip_msg.method = method sip_msg.ruri = None return sip_msg
def __init__(self, message=None): """Provides parse and validation routines and simple reply interface for SIP messages. Fields: raw_message (Message) - Message instance containing raw message method (Method) - SIP method parsed from request line for requests or from CSeq header for responses headers (dict) - dict of headers: keys are HdrKey objects, values are parsed headers user (str) - user data """ if message is None: message = Message() self.raw_message = message self.method = None self._ruri = None self.headers = dict() self.user = None
def __init__(self, options=None, buf=None, state=STATE_FIRST_LINE, message=None, acc=None, content_length=None, start_pos=0): if options is None: self.options = dict() else: self.options = options self.buffer = buf self.state = state if message is None: self.message = Message() else: self.message = message if acc is None: self.acc = list() else: self.acc = acc self.content_length = content_length self.start_pos = start_pos
def test_message_serialize_response(): msg = Message() msg.type = ResponseType() msg.status = 200 msg.reason = 'OK' msg.set('Via', [ "SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1", "SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds" ]) msg.set('From', 'sip:[email protected]') msg.set('To', 'sip:[email protected]') msg.set('CSeq', '1 INVITE') msg.set('Call-Id', 'some-call-id') exp_msg = '\r\n'.join([ 'SIP/2.0 200 OK', 'Via: SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1', 'Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds', 'To: sip:[email protected]', 'From: sip:[email protected]', 'Call-Id: some-call-id', 'CSeq: 1 INVITE' ]) + '\r\n\r\n' assert exp_msg == msg.serialize()
def test_set_type(msg_type, type_name): msg = Message() msg.type = msg_type() assert msg.type == type_name
def test_message_serialize_request(): msg = Message() msg.type = RequestType() msg.method = INVITE msg.ruri = "sip:[email protected]" msg.set('Via', 'SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds') msg.set('From', 'sip:[email protected]') msg.set('To', 'sip:[email protected]') msg.set('Max-Forwards', '70') msg.set('CSeq', '1 INVITE') msg.set('Call-Id', 'some-call-id') exp_msg = 'INVITE sip:[email protected] SIP/2.0' + \ '\r\nVia: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds' + \ '\r\nTo: sip:[email protected]' + \ '\r\nFrom: sip:[email protected]' + \ '\r\nCall-Id: some-call-id' + \ '\r\nCSeq: 1 INVITE' + \ '\r\nMax-Forwards: 70\r\n\r\n' assert exp_msg == msg.serialize()
def test_get_header(): msg = Message() msg.add('Some-Header', "1") msg.add('Some-Header', 'a,b') hdr = msg.get('SOME-HEADER') assert hdr.values == ['1', 'a,b']
def test_set_ruri(ruri): msg = Message() msg.type = RequestType() assert msg.ruri is None msg.ruri = ruri assert msg.ruri == ruri
def test_set_method(method): msg = Message() msg.type = RequestType() assert msg.method is None msg.method = method assert msg.method == method
def test_set_reason(reason): msg = Message() msg.type = ResponseType() assert msg.reason is None msg.reason = reason assert msg.reason == reason
def test_set_status(status): msg = Message() msg.type = ResponseType() assert msg.status is None msg.status = status assert msg.status == status
def test_message_clear_headers(): msg = Message() msg.type = RequestType() msg.method = INVITE msg.ruri = 'sip:[email protected]' msg.set('Via', 'SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds') msg.set('From', 'sip:[email protected]') msg.set('To', 'sip:[email protected]') msg.set('Max-Forwards', '70') msg.set('CSeq', '1 INVITE') msg.set('Call-Id', 'some-call-id') msg.clear_header() assert [] == msg.headers
def test_message_invalid_method(): msg = Message() msg.type = RequestType() with pytest.raises(MessageError): msg.method = 123
def reset_message(self): self.message = Message()