Browse Source

do not dispatch events for disconnected namespaces (Fixes #333)

pull/348/head
Miguel Grinberg 6 years ago
parent
commit
a839a36fa0
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 4
      socketio/asyncio_server.py
  2. 4
      socketio/server.py
  3. 15
      tests/asyncio/test_asyncio_server.py
  4. 15
      tests/common/test_server.py

4
socketio/asyncio_server.py

@ -428,6 +428,10 @@ class AsyncServer(server.Server):
namespace = namespace or '/'
self.logger.info('received event "%s" from %s [%s]', data[0], sid,
namespace)
if not self.manager.is_connected(sid, namespace):
self.logger.warning('%s is not connected to namespace %s',
sid, namespace)
return
if self.async_handlers:
self.start_background_task(self._handle_event_internal, self, sid,
data, namespace, id)

4
socketio/server.py

@ -638,6 +638,10 @@ class Server(object):
namespace = namespace or '/'
self.logger.info('received event "%s" from %s [%s]', data[0], sid,
namespace)
if not self.manager.is_connected(sid, namespace):
self.logger.warning('%s is not connected to namespace %s',
sid, namespace)
return
if self.async_handlers:
self.start_background_task(self._handle_event_internal, self, sid,
data, namespace, id)

15
tests/asyncio/test_asyncio_server.py

@ -436,6 +436,7 @@ class TestAsyncServer(unittest.TestCase):
def test_handle_event(self, eio):
eio.return_value.send = AsyncMock()
s = asyncio_server.AsyncServer(async_handlers=False)
s.manager.connect('123', '/')
handler = AsyncMock()
s.on('my message', handler)
_run(s._handle_eio_message('123', '2["my message","a","b","c"]'))
@ -444,14 +445,25 @@ class TestAsyncServer(unittest.TestCase):
def test_handle_event_with_namespace(self, eio):
eio.return_value.send = AsyncMock()
s = asyncio_server.AsyncServer(async_handlers=False)
s.manager.connect('123', '/foo')
handler = mock.MagicMock()
s.on('my message', handler, namespace='/foo')
_run(s._handle_eio_message('123', '2/foo,["my message","a","b","c"]'))
handler.assert_called_once_with('123', 'a', 'b', 'c')
def test_handle_event_with_disconnected_namespace(self, eio):
eio.return_value.send = AsyncMock()
s = asyncio_server.AsyncServer(async_handlers=False)
s.manager.connect('123', '/foo')
handler = mock.MagicMock()
s.on('my message', handler, namespace='/bar')
_run(s._handle_eio_message('123', '2/bar,["my message","a","b","c"]'))
handler.assert_not_called()
def test_handle_event_binary(self, eio):
eio.return_value.send = AsyncMock()
s = asyncio_server.AsyncServer(async_handlers=False)
s.manager.connect('123', '/')
handler = mock.MagicMock()
s.on('my message', handler)
_run(s._handle_eio_message('123', '52-["my message","a",'
@ -476,6 +488,7 @@ class TestAsyncServer(unittest.TestCase):
def test_handle_event_with_ack(self, eio):
eio.return_value.send = AsyncMock()
s = asyncio_server.AsyncServer(async_handlers=False)
s.manager.connect('123', '/')
handler = mock.MagicMock(return_value='foo')
s.on('my message', handler)
_run(s._handle_eio_message('123', '21000["my message","foo"]'))
@ -486,6 +499,7 @@ class TestAsyncServer(unittest.TestCase):
def test_handle_event_with_ack_none(self, eio):
eio.return_value.send = AsyncMock()
s = asyncio_server.AsyncServer(async_handlers=False)
s.manager.connect('123', '/')
handler = mock.MagicMock(return_value=None)
s.on('my message', handler)
_run(s._handle_eio_message('123', '21000["my message","foo"]'))
@ -729,6 +743,7 @@ class TestAsyncServer(unittest.TestCase):
def test_async_handlers(self, eio):
s = asyncio_server.AsyncServer(async_handlers=True)
s.manager.connect('123', '/')
_run(s._handle_eio_message('123', '2["my message","a","b","c"]'))
s.eio.start_background_task.assert_called_once_with(
s._handle_event_internal, s, '123', ['my message', 'a', 'b', 'c'],

15
tests/common/test_server.py

@ -392,6 +392,7 @@ class TestServer(unittest.TestCase):
def test_handle_event(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', '2["my message","a","b","c"]')
@ -399,13 +400,23 @@ class TestServer(unittest.TestCase):
def test_handle_event_with_namespace(self, eio):
s = server.Server(async_handlers=False)
s.manager.connect('123', '/foo')
handler = mock.MagicMock()
s.on('my message', handler, namespace='/foo')
s._handle_eio_message('123', '2/foo,["my message","a","b","c"]')
handler.assert_called_once_with('123', 'a', 'b', 'c')
def test_handle_event_with_disconnected_namespace(self, eio):
s = server.Server(async_handlers=False)
s.manager.connect('123', '/foo')
handler = mock.MagicMock()
s.on('my message', handler, namespace='/bar')
s._handle_eio_message('123', '2/bar,["my message","a","b","c"]')
handler.assert_not_called()
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",'
@ -418,7 +429,6 @@ class TestServer(unittest.TestCase):
def test_handle_event_binary_ack(self, eio):
mgr = mock.MagicMock()
s = server.Server(client_manager=mgr)
s.manager.initialize(s)
s._handle_eio_message('123', '61-321["my message","a",'
'{"_placeholder":true,"num":0}]')
s._handle_eio_message('123', b'foo')
@ -427,6 +437,7 @@ class TestServer(unittest.TestCase):
def test_handle_event_with_ack(self, eio):
s = server.Server(async_handlers=False)
s.manager.connect('123', '/')
handler = mock.MagicMock(return_value='foo')
s.on('my message', handler)
s._handle_eio_message('123', '21000["my message","foo"]')
@ -436,6 +447,7 @@ class TestServer(unittest.TestCase):
def test_handle_event_with_ack_none(self, eio):
s = server.Server(async_handlers=False)
s.manager.connect('123', '/')
handler = mock.MagicMock(return_value=None)
s.on('my message', handler)
s._handle_eio_message('123', '21000["my message","foo"]')
@ -655,6 +667,7 @@ class TestServer(unittest.TestCase):
def test_async_handlers(self, eio):
s = server.Server(async_handlers=True)
s.manager.connect('123', '/')
s._handle_eio_message('123', '2["my message","a","b","c"]')
s.eio.start_background_task.assert_called_once_with(
s._handle_event_internal, s, '123', ['my message', 'a', 'b', 'c'],

Loading…
Cancel
Save