Example #1
0
    def send(self, message, callback=None, force_json=False):
        """Send message to the client.

        `message`
            Message to send.
        `callback`
            Optional callback. If passed, callback will be called
            when client received sent message and sent acknowledgment
            back.
        `force_json`
            Optional argument. If set to True (and message is a string)
            then the message type will be JSON (Type 4 in socket_io protocol).
            This is what you want, when you send already json encoded strings.
        """
        if self.is_closed:
            return

        if callback is not None:
            msg = proto.message(self.endpoint,
                                message,
                                self.queue_ack(callback, message), force_json)
        else:
            msg = proto.message(self.endpoint, message, force_json=force_json)

        self.session.send_message(msg)
Example #2
0
    def send(self, message, callback=None, force_json=False):
        """Send message to the client.

        `message`
            Message to send.
        `callback`
            Optional callback. If passed, callback will be called
            when client received sent message and sent acknowledgment
            back.
        `force_json`
            Optional argument. If set to True (and message is a string)
            then the message type will be JSON (Type 4 in socket_io protocol).
            This is what you want, when you send already json encoded strings.
        """
        if self.is_closed:
            return

        if callback is not None:
            msg = proto.message(self.endpoint,
                                message,
                                self.queue_ack(callback, message), force_json)
        else:
            msg = proto.message(self.endpoint, message, force_json=force_json)

        self.session.send_message(msg)
Example #3
0
def test_json():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Send json message
    transport.recv(proto.message(None, dict(a=10, b=20)))

    # Check incoming message
    eq_(conn.pop_incoming(), dict(a=10, b=20))

    # Check outgoing message
    eq_(transport.pop_outgoing(), proto.message(None, dict(a=10, b=20)))
Example #4
0
def test_json():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Send json message
    transport.recv(proto.message(None, dict(a=10, b=20)))

    # Check incoming message
    eq_(conn.pop_incoming(), dict(a=10, b=20))

    # Check outgoing message
    eq_(transport.pop_outgoing(), proto.message(None, dict(a=10, b=20)))
Example #5
0
def test_message():
    # Test string message
    eq_(proto.message(None, 'abc'), '3:::abc')

    eq_(proto.message('abc', 'def'), '3::abc:def')

    eq_(proto.message(None, u'\u0403\u0404\u0405'),
        u'3:::\u0403\u0404\u0405'.encode('utf-8'))

    # TODO: Fix me
    eq_(proto.message(None, dict(a=1, b=2)),
        '4:::%s' % proto.json_dumps(dict(a=1, b=2)))
Example #6
0
def test_message():
    # Test string message
    eq_(proto.message(None, 'abc'), '3:::abc')

    eq_(proto.message('abc', 'def'), '3::abc:def')

    eq_(proto.message(None, '\u0403\u0404\u0405'), '3:::\u0403\u0404\u0405')

    # TODO: Multibyte encoding fix

    # TODO: Fix me
    eq_(proto.message(None, dict(a=1, b=2)),
        '4:::%s' % proto.json_dumps(dict(a=1, b=2)))
Example #7
0
def test_message():
    # Test string message
    eq_(proto.message(None, 'abc'), '3:::abc')

    eq_(proto.message('abc', 'def'), '3::abc:def')

    eq_(proto.message(None, '\u0403\u0404\u0405'),
                      '3:::\u0403\u0404\u0405')

    # TODO: Multibyte encoding fix

    # TODO: Fix me
    eq_(proto.message(None, dict(a=1, b=2)),
                      '4:::%s' % proto.json_dumps(dict(a=1, b=2)))
Example #8
0
def test_message():
    # Test string message
    eq_(proto.message(None, 'abc'), '3:::abc')

    eq_(proto.message('abc', 'def'), '3::abc:def')

    eq_(proto.message(None, u'\u0403\u0404\u0405'),
                      u'3:::\u0403\u0404\u0405'.encode('utf-8'))

    # TODO: Fix me
    eq_(proto.message(None, dict(a=1, b=2)),
                      '4:::%s' % proto.json_dumps(dict(a=1, b=2)))


    # TODO: Cover event
Example #9
0
def test_session_attach():
    # Create environment
    server, session, transport, conn = _get_test_environment(a=[10])

    # Check if connection opened
    eq_(conn.is_open, True)
    eq_(conn.request.arguments, {'a': [10]})
    eq_(conn.request.get_argument('a'), 10)

    # Send message and check if it was handled by connection
    transport.recv(proto.message(None, 'abc'))

    # Check if incoming queue has abc
    eq_(conn.pop_incoming(), 'abc')

    # Check if outgoing transport has abc
    eq_(transport.pop_outgoing(), '3:::abc')

    # Close session
    conn.close()

    # Check if it sent disconnect packet to the client
    eq_(transport.pop_outgoing(), '0::')

    # Detach
    session.remove_handler(transport)
    eq_(session.handler, None)

    # Check if session is still open
    eq_(transport.is_open, False)
    eq_(conn.is_open, False)
    eq_(session.is_closed, True)
Example #10
0
def test_ack():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Send message with ACK
    transport.recv(proto.message(None, 'abc', 1))

    # Check that message was received by the connection
    eq_(conn.pop_incoming(), 'abc')

    # Check for ACK
    eq_(transport.pop_outgoing(), '3:::abc')
    eq_(transport.pop_outgoing(), '6:::1')

    # Send with ACK
    def handler(message):
        eq_(message, 'abc')

        conn.send('yes')

    conn.send('abc', handler)

    eq_(transport.pop_outgoing(), '3:1::abc')

    # Send ACK from client
    transport.recv('6:::1')

    # Check if handler was called
    eq_(transport.pop_outgoing(), '3:::yes')
Example #11
0
 def test_on_message(self, m_send):
     session = EdtrRouter.create_session(self.request)
     self.sleep()
     message = 'test message'
     session.raw_message(proto.message(None, message))
     self.assertEqual(m_send.called, True)
     m_send.assert_called_with(message + "from server")
Example #12
0
def test_session_attach():
    # Create environment
    server, session, transport, conn = _get_test_environment(a=[10])

    # Check if connection opened
    eq_(conn.is_open, True)
    eq_(conn.request.arguments, {'a': [10]})
    eq_(conn.request.get_argument('a'), 10)

    # Send message and check if it was handled by connection
    transport.recv(proto.message(None, 'abc'))

    # Check if incoming queue has abc
    eq_(conn.pop_incoming(), 'abc')

    # Check if outgoing transport has abc
    eq_(transport.pop_outgoing(), '3:::abc')

    # Close session
    conn.close()

    # Check if it sent disconnect packet to the client
    eq_(transport.pop_outgoing(), '0::')

    # Detach
    session.remove_handler(transport)
    eq_(session.handler, None)

    # Check if session is still open
    eq_(transport.is_open, False)
    eq_(conn.is_open, False)
    eq_(session.is_closed, True)
Example #13
0
def test_ack():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Send message with ACK
    transport.recv(proto.message(None, 'abc', 1))

    # Check that message was received by the connection
    eq_(conn.pop_incoming(), 'abc')

    # Check for ACK
    eq_(transport.pop_outgoing(), '3:::abc')
    eq_(transport.pop_outgoing(), '6:::1')

    # Send with ACK
    def handler(message):
        eq_(message, 'abc')

        conn.send('yes')

    conn.send('abc', handler)

    eq_(transport.pop_outgoing(), '3:1::abc')

    # Send ACK from client
    transport.recv('6:::1')

    # Check if handler was called
    eq_(transport.pop_outgoing(), '3:::yes')
Example #14
0
    def send(self, message, callback=None):
        """Send message to the client.

        `message`
            Message to send.
        `callback`
            Optional callback. If passed, callback will be called
            when client received sent message and sent acknowledgment
            back.
        """
        if callback is not None:
            msg = proto.message(self.endpoint, message,
                                self.queue_ack(callback, message))
        else:
            msg = proto.message(self.endpoint, message)

        self.session.send_message(msg)
Example #15
0
def test_invalid_endpoint():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Send message to unconnected endpoint
    transport.recv(proto.message('test', 'abc'))

    # Check if message was received by default endpoint
    eq_(len(conn.incoming), 0)
Example #16
0
def test_invalid_endpoint():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Send message to unconnected endpoint
    transport.recv(proto.message('test', 'abc'))

    # Check if message was received by default endpoint
    eq_(len(conn.incoming), 0)
Example #17
0
    def send(self, message, callback=None):
        """Send message to the client.

        `message`
            Message to send.
        `callback`
            Optional callback. If passed, callback will be called
            when client received sent message and sent acknowledgment
            back.
        """
        if callback is not None:
            msg = proto.message(self.endpoint,
                                message,
                                self.queue_ack(callback, message))
        else:
            msg = proto.message(self.endpoint, message)

        self.session.send_message(msg)
Example #18
0
def test_endpoint():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Connect endpoint
    transport.recv(proto.connect('/test?a=123&b=456'))

    # Verify that client received connect message
    eq_(transport.pop_outgoing(), '1::/test')

    # Verify that connection object was created
    conn_test = session.endpoints['/test']
    eq_(conn_test.endpoint, '/test')
    eq_(conn_test.is_open, True)
    eq_(conn_test.request.arguments, dict(a=['123'], b=['456']))
    eq_(conn_test.request.get_argument('a'), '123')

    # Send message to endpoint and verify that it was received
    transport.recv(proto.message('/test', 'abc'))
    eq_(conn_test.pop_incoming(), 'abc')
    eq_(transport.pop_outgoing(), '3::/test:abc')

    # Close endpoint connection from client
    transport.recv(proto.disconnect('/test'))

    # Verify that everything was cleaned up
    eq_(transport.pop_outgoing(), '0::/test')
    eq_(conn_test.is_open, False)
    eq_(conn.is_open, True)
    eq_(session.is_closed, False)

    eq_(session.endpoints, dict())

    # Open another endpoint connection
    transport.recv(proto.connect('/test2'))

    # Verify that client received connect message
    eq_(transport.pop_outgoing(), '1::/test2')

    # Get connection
    conn_test = session.endpoints['/test2']
    eq_(conn_test.request.arguments, dict())

    # Close main connection
    transport.recv(proto.disconnect())

    # Check if connections were closed and sent out
    eq_(transport.pop_outgoing(), '0::/test2')
    eq_(transport.pop_outgoing(), '0::')

    eq_(conn_test.is_open, False)
    eq_(conn.is_open, False)
    eq_(session.is_closed, True)
Example #19
0
def test_endpoint():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Connect endpoint
    transport.recv(proto.connect("/test?a=123&b=456"))

    # Verify that client received connect message
    eq_(transport.pop_outgoing(), "1::/test")

    # Verify that connection object was created
    conn_test = session.endpoints["/test"]
    eq_(conn_test.endpoint, "/test")
    eq_(conn_test.is_open, True)
    eq_(conn_test.request.arguments, dict(a=["123"], b=["456"]))
    eq_(conn_test.request.get_argument("a"), "123")

    # Send message to endpoint and verify that it was received
    transport.recv(proto.message("/test", "abc"))
    eq_(conn_test.pop_incoming(), "abc")
    eq_(transport.pop_outgoing(), "3::/test:abc")

    # Close endpoint connection from client
    transport.recv(proto.disconnect("/test"))

    # Verify that everything was cleaned up
    eq_(transport.pop_outgoing(), "0::/test")
    eq_(conn_test.is_open, False)
    eq_(conn.is_open, True)
    eq_(session.is_closed, False)

    eq_(session.endpoints, dict())

    # Open another endpoint connection
    transport.recv(proto.connect("/test2"))

    # Verify that client received connect message
    eq_(transport.pop_outgoing(), "1::/test2")

    # Get connection
    conn_test = session.endpoints["/test2"]
    eq_(conn_test.request.arguments, dict())

    # Close main connection
    transport.recv(proto.disconnect())

    # Check if connections were closed and sent out
    eq_(transport.pop_outgoing(), "0::/test2")
    eq_(transport.pop_outgoing(), "0::")

    eq_(conn_test.is_open, False)
    eq_(conn.is_open, False)
    eq_(session.is_closed, True)
Example #20
0
def test_endpoint():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Connect endpoint
    transport.recv(proto.connect('/test?a=123&b=456'))

    # Verify that client received connect message
    eq_(transport.pop_outgoing(), '1::/test')

    # Verify that connection object was created
    conn_test = session.endpoints['/test']
    eq_(conn_test.endpoint, '/test')
    eq_(conn_test.is_open, True)
    eq_(conn_test.request.arguments, dict(a=['123'], b=['456']))
    eq_(conn_test.request.get_argument('a'), '123')

    # Send message to endpoint and verify that it was received
    transport.recv(proto.message('/test', 'abc'))
    eq_(conn_test.pop_incoming(), 'abc')
    eq_(transport.pop_outgoing(), '3::/test:abc')

    # Close endpoint connection from client
    transport.recv(proto.disconnect('/test'))

    # Verify that everything was cleaned up
    eq_(transport.pop_outgoing(), '0::/test')
    eq_(conn_test.is_open, False)
    eq_(conn.is_open, True)
    eq_(session.is_closed, False)

    eq_(session.endpoints, dict())

    # Open another endpoint connection
    transport.recv(proto.connect('/test2'))

    # Verify that client received connect message
    eq_(transport.pop_outgoing(), '1::/test2')

    # Get connection
    conn_test = session.endpoints['/test2']
    eq_(conn_test.request.arguments, dict())

    # Close main connection
    transport.recv(proto.disconnect())

    # Check if connections were closed and sent out
    eq_(transport.pop_outgoing(), '0::/test2')
    eq_(transport.pop_outgoing(), '0::')

    eq_(conn_test.is_open, False)
    eq_(conn.is_open, False)
    eq_(session.is_closed, True)
Example #21
0
def test_ack():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Send message with ACK
    transport.recv(proto.message(None, 'abc', 1))

    # Check that message was received by the connection
    eq_(conn.pop_incoming(), 'abc')

    # Check for ACK
    eq_(transport.pop_outgoing(), '3:::abc')
    eq_(transport.pop_outgoing(), '6:::1')

    # Send with ACK
    def handler(msg, data):
        eq_(msg, 'abc')
        eq_(data, None)

        conn.send('yes')

    conn.send('abc', handler)

    eq_(transport.pop_outgoing(), '3:1::abc')

    # Send ACK from client
    transport.recv('6:::1')

    # Check if handler was called
    eq_(transport.pop_outgoing(), '3:::yes')

    # Test ack with event
    # Send event with multiple parameters
    transport.recv(proto.event(None, 'test', 1, a=10, b=20))

    # Check outgoing
    eq_(transport.pop_outgoing(), proto.event(None, 'test', None, a=10, b=20))
    eq_(transport.pop_outgoing(), proto.ack(None, 1, 'test'))
Example #22
0
def test_ack():
    # Create environment
    server, session, transport, conn = _get_test_environment()

    # Send message with ACK
    transport.recv(proto.message(None, 'abc', 1))

    # Check that message was received by the connection
    eq_(conn.pop_incoming(), 'abc')

    # Check for ACK
    eq_(transport.pop_outgoing(), '3:::abc')
    eq_(transport.pop_outgoing(), '6:::1')

    # Send with ACK
    def handler(msg, data):
        eq_(msg, 'abc')
        eq_(data, None)

        conn.send('yes')

    conn.send('abc', handler)

    eq_(transport.pop_outgoing(), '3:1::abc')

    # Send ACK from client
    transport.recv('6:::1')

    # Check if handler was called
    eq_(transport.pop_outgoing(), '3:::yes')

    # Test ack with event
    # Send event with multiple parameters
    transport.recv(proto.event(None, 'test', 1, a=10, b=20))

    # Check outgoing
    eq_(transport.pop_outgoing(), proto.event(None, 'test', None, a=10, b=20))
    eq_(transport.pop_outgoing(), proto.ack(None, 1, 'test'))