コード例 #1
0
 def test_handle_event_binary(self, eio):
     s = server.Server(async_handlers=False)
     s.manager.connect('123', '/')
     handler = mock.MagicMock()
     s.on('my message', handler)
     s._handle_eio_message(
         '123',
         '52-["my message","a",'
         '{"_placeholder":true,"num":1},'
         '{"_placeholder":true,"num":0}]',
     )
     s._handle_eio_message('123', b'foo')
     s._handle_eio_message('123', b'bar')
     handler.assert_called_once_with('1', 'a', b'bar', b'foo')
コード例 #2
0
    def test_call_with_timeout(self, eio):
        mgr = mock.MagicMock()
        s = server.Server(client_manager=mgr)

        def fake_event_wait(timeout=None):
            self.assertEqual(timeout, 12)
            return False

        s.eio.create_event.return_value.wait = fake_event_wait
        self.assertRaises(exceptions.TimeoutError,
                          s.call,
                          'foo',
                          sid='123',
                          timeout=12)
コード例 #3
0
 def test_handle_disconnect_only_namespace(self, eio):
     mgr = mock.MagicMock()
     s = server.Server(client_manager=mgr)
     s.manager.get_namespaces = mock.MagicMock(return_value=['/', '/foo'])
     handler = mock.MagicMock()
     s.on('disconnect', handler)
     handler_namespace = mock.MagicMock()
     s.on('disconnect', handler_namespace, namespace='/foo')
     s._handle_eio_connect('123', 'environ')
     s._handle_eio_message('123', '0/foo')
     s._handle_eio_message('123', '1/foo')
     self.assertEqual(handler.call_count, 0)
     handler_namespace.assert_called_once_with('123')
     self.assertEqual(s.environ, {'123': 'environ'})
コード例 #4
0
    def test_bad_namespace_handler(self, eio):
        class Dummy(object):
            pass

        class AsyncNS(namespace.Namespace):
            def is_asyncio_based(self):
                return True

        s = server.Server()
        self.assertRaises(ValueError, s.register_namespace, 123)
        self.assertRaises(ValueError, s.register_namespace, Dummy)
        self.assertRaises(ValueError, s.register_namespace, Dummy())
        self.assertRaises(ValueError, s.register_namespace,
                          namespace.Namespace)
        self.assertRaises(ValueError, s.register_namespace, AsyncNS())
コード例 #5
0
    def test_on_event(self, eio):
        s = server.Server()

        @s.on('connect')
        def foo():
            pass

        def bar():
            pass
        s.on('disconnect', bar)
        s.on('disconnect', bar, namespace='/foo')

        self.assertEqual(s.handlers['/']['connect'], foo)
        self.assertEqual(s.handlers['/']['disconnect'], bar)
        self.assertEqual(s.handlers['/foo']['disconnect'], bar)
コード例 #6
0
 def test_handle_connect_namespace_rejected_with_exception(self, eio):
     mgr = mock.MagicMock()
     s = server.Server(client_manager=mgr)
     handler = mock.MagicMock(
         side_effect=exceptions.ConnectionRefusedError(u'fail_reason', 1))
     s.on('connect', handler, namespace='/foo')
     ret = s._handle_eio_connect('123', 'environ')
     s._handle_eio_message('123', '0/foo')
     assert ret is None
     assert s.manager.connect.call_count == 2
     assert s.manager.disconnect.call_count == 1
     assert s.environ == {'123': 'environ'}
     s.eio.send.assert_any_call('123',
                                '4/foo,["fail_reason",1]',
                                binary=False)
コード例 #7
0
    def test_on_event(self, eio):
        s = server.Server()

        @s.on('connect')
        def foo():
            pass

        def bar():
            pass

        s.on('disconnect', bar)
        s.on('disconnect', bar, namespace='/foo')

        assert s.handlers['/']['connect'] == foo
        assert s.handlers['/']['disconnect'] == bar
        assert s.handlers['/foo']['disconnect'] == bar
コード例 #8
0
    def test_bad_namespace_handler(self, eio):
        class Dummy(object):
            pass

        class AsyncNS(namespace.Namespace):
            def is_asyncio_based(self):
                return True

        s = server.Server()
        with pytest.raises(ValueError):
            s.register_namespace(123)
        with pytest.raises(ValueError):
            s.register_namespace(Dummy)
        with pytest.raises(ValueError):
            s.register_namespace(Dummy())
        with pytest.raises(ValueError):
            s.register_namespace(namespace.Namespace)
        with pytest.raises(ValueError):
            s.register_namespace(AsyncNS())
コード例 #9
0
 def test_emit_default_namespace(self, eio):
     mgr = mock.MagicMock()
     s = server.Server(client_manager=mgr)
     s.emit('my event', {'foo': 'bar'},
            to='room',
            skip_sid='123',
            callback='cb')
     s.manager.emit.assert_called_once_with('my event', {'foo': 'bar'},
                                            '/',
                                            room='room',
                                            skip_sid='123',
                                            callback='cb')
     s.emit('my event', {'foo': 'bar'},
            room='room',
            skip_sid='123',
            callback='cb')
     s.manager.emit.assert_called_with('my event', {'foo': 'bar'},
                                       '/',
                                       room='room',
                                       skip_sid='123',
                                       callback='cb')
コード例 #10
0
    def test_session(self, eio):
        fake_session = {}

        def fake_get_session(eio_sid):
            assert eio_sid == '123'
            return fake_session

        def fake_save_session(eio_sid, session):
            global fake_session
            assert eio_sid == '123'
            fake_session = session

        s = server.Server()
        s.eio.get_session = fake_get_session
        s.eio.save_session = fake_save_session
        s._handle_eio_connect('123', 'environ')
        s._handle_eio_message('123', '0')
        s._handle_eio_message('123', '0/ns')
        sid = s.manager.sid_from_eio_sid('123', '/')
        sid2 = s.manager.sid_from_eio_sid('123', '/ns')
        s.save_session(sid, {'foo': 'bar'})
        with s.session(sid) as session:
            assert session == {'foo': 'bar'}
            session['foo'] = 'baz'
            session['bar'] = 'foo'
        assert s.get_session(sid) == {'foo': 'baz', 'bar': 'foo'}
        assert fake_session == {'/': {'foo': 'baz', 'bar': 'foo'}}
        with s.session(sid2, namespace='/ns') as session:
            assert session == {}
            session['a'] = 'b'
        assert s.get_session(sid2, namespace='/ns') == {'a': 'b'}
        assert fake_session == {
            '/': {
                'foo': 'baz',
                'bar': 'foo'
            },
            '/ns': {
                'a': 'b'
            },
        }
コード例 #11
0
    def test_event(self, eio):
        s = server.Server()

        @s.event
        def connect():
            pass

        @s.event
        def foo():
            pass

        @s.event()
        def bar():
            pass

        @s.event(namespace='/foo')
        def disconnect():
            pass

        assert s.handlers['/']['connect'] == connect
        assert s.handlers['/']['foo'] == foo
        assert s.handlers['/']['bar'] == bar
        assert s.handlers['/foo']['disconnect'] == disconnect
コード例 #12
0
    def test_event(self, eio):
        s = server.Server()

        @s.event
        def connect():
            pass

        @s.event
        def foo():
            pass

        @s.event()
        def bar():
            pass

        @s.event(namespace='/foo')
        def disconnect():
            pass

        self.assertEqual(s.handlers['/']['connect'], connect)
        self.assertEqual(s.handlers['/']['foo'], foo)
        self.assertEqual(s.handlers['/']['bar'], bar)
        self.assertEqual(s.handlers['/foo']['disconnect'], disconnect)
コード例 #13
0
    def test_custom_json(self, eio):
        # Warning: this test cannot run in parallel with other tests, as it
        # changes the JSON encoding/decoding functions

        class CustomJSON(object):
            @staticmethod
            def dumps(*args, **kwargs):
                return '*** encoded ***'

            @staticmethod
            def loads(*args, **kwargs):
                return '+++ decoded +++'

        server.Server(json=CustomJSON)
        eio.assert_called_once_with(**{'json': CustomJSON})

        pkt = packet.Packet(packet_type=packet.EVENT,
                            data={six.text_type('foo'): six.text_type('bar')})
        self.assertEqual(pkt.encode(), '2*** encoded ***')
        pkt2 = packet.Packet(encoded_packet=pkt.encode())
        self.assertEqual(pkt2.data, '+++ decoded +++')

        # restore the default JSON module
        packet.Packet.json = json
コード例 #14
0
 def test_call_without_async_handlers(self, eio):
     mgr = mock.MagicMock()
     s = server.Server(client_manager=mgr, async_handlers=False)
     self.assertRaises(RuntimeError, s.call, 'foo', sid='123', timeout=12)
コード例 #15
0
 def test_handle_invalid_packet(self, eio):
     s = server.Server()
     self.assertRaises(ValueError, s._handle_eio_message, '123', '9')
コード例 #16
0
 def test_handle_disconnect_unknown_client(self, eio):
     mgr = mock.MagicMock()
     s = server.Server(client_manager=mgr)
     s._handle_eio_disconnect('123')
コード例 #17
0
 def test_transport(self, eio):
     s = server.Server()
     s.eio.transport = mock.MagicMock(return_value='polling')
     s._handle_eio_connect('foo', 'environ')
     self.assertEqual(s.transport('foo'), 'polling')
     s.eio.transport.assert_called_once_with('foo')
コード例 #18
0
 def test_emit_internal_binary(self, eio):
     s = server.Server(binary=True)
     s._emit_internal('123', u'my event', b'my binary data')
     self.assertEqual(s.eio.send.call_count, 2)
コード例 #19
0
 def test_emit_internal_default_namespace(self, eio):
     s = server.Server()
     s._emit_internal('123', 'my event', 'my data')
     s.eio.send.assert_called_once_with('123',
                                        '2["my event","my data"]',
                                        binary=False)
コード例 #20
0
 def test_emit_internal_with_list(self, eio):
     s = server.Server()
     s._emit_internal('123', 'my event', ['foo', 'bar'], namespace='/foo')
     s.eio.send.assert_called_once_with('123',
                                        '2/foo,["my event",["foo","bar"]]',
                                        binary=False)
コード例 #21
0
 def test_handle_request(self, eio):
     s = server.Server()
     s.handle_request('environ', 'start_response')
     s.eio.handle_request.assert_called_once_with('environ',
                                                  'start_response')
コード例 #22
0
 def test_start_background_task(self, eio):
     s = server.Server()
     s.start_background_task('foo', 'bar', baz='baz')
     s.eio.start_background_task.assert_called_once_with('foo',
                                                         'bar',
                                                         baz='baz')
コード例 #23
0
 def test_send(self, eio):
     mgr = mock.MagicMock()
     s = server.Server(client_manager=mgr)
     s.send('foo', 'room', '123', namespace='/foo', callback='cb')
     s.manager.emit.assert_called_once_with('message', 'foo', '/foo',
                                            'room', '123', 'cb')
コード例 #24
0
 def test_disconnect(self, eio):
     s = server.Server()
     s._handle_eio_connect('123', 'environ')
     s.disconnect('123')
     s.eio.send.assert_any_call('123', '1', binary=False)
コード例 #25
0
 def test_disconnect_namespace(self, eio):
     s = server.Server()
     s._handle_eio_connect('123', 'environ')
     s._handle_eio_message('123', '0/foo')
     s.disconnect('123', namespace='/foo')
     s.eio.send.assert_any_call('123', '1/foo', binary=False)
コード例 #26
0
 def test_leave_room(self, eio):
     mgr = mock.MagicMock()
     s = server.Server(client_manager=mgr)
     s.leave_room('123', 'room', namespace='/foo')
     s.manager.leave_room.assert_called_once_with('123', '/foo', 'room')
コード例 #27
0
 def test_engineio_logger(self, eio):
     server.Server(engineio_logger='foo')
     eio.assert_called_once_with(**{
         'logger': 'foo',
         'async_handlers': False
     })
コード例 #28
0
 def test_close_room_default_namespace(self, eio):
     mgr = mock.MagicMock()
     s = server.Server(client_manager=mgr)
     s.close_room('room')
     s.manager.close_room.assert_called_once_with('room', '/')
コード例 #29
0
 def test_sleep(self, eio):
     s = server.Server()
     s.sleep(1.23)
     s.eio.sleep.assert_called_once_with(1.23)
コード例 #30
0
 def test_rooms_default_namespace(self, eio):
     mgr = mock.MagicMock()
     s = server.Server(client_manager=mgr)
     s.rooms('123')
     s.manager.get_rooms.assert_called_once_with('123', '/')