Browse Source

Add missing to argument to namespace emit() and send() calls (Fixes #810)

pull/812/head
Miguel Grinberg 3 years ago
parent
commit
ed08a01e65
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 11
      src/socketio/asyncio_namespace.py
  2. 11
      src/socketio/namespace.py
  3. 12
      tests/asyncio/test_asyncio_namespace.py
  4. 12
      tests/common/test_namespace.py

11
src/socketio/asyncio_namespace.py

@ -41,7 +41,7 @@ class AsyncNamespace(namespace.Namespace):
ret = handler(*args)
return ret
async def emit(self, event, data=None, room=None, skip_sid=None,
async def emit(self, event, data=None, to=None, room=None, skip_sid=None,
namespace=None, callback=None):
"""Emit a custom event to one or more connected clients.
@ -51,13 +51,13 @@ class AsyncNamespace(namespace.Namespace):
Note: this method is a coroutine.
"""
return await self.server.emit(event, data=data, room=room,
return await self.server.emit(event, data=data, to=to, room=room,
skip_sid=skip_sid,
namespace=namespace or self.namespace,
callback=callback)
async def send(self, data, room=None, skip_sid=None, namespace=None,
callback=None):
async def send(self, data, to=None, room=None, skip_sid=None,
namespace=None, callback=None):
"""Send a message to one or more connected clients.
The only difference with the :func:`socketio.Server.send` method is
@ -66,7 +66,8 @@ class AsyncNamespace(namespace.Namespace):
Note: this method is a coroutine.
"""
return await self.server.send(data, room=room, skip_sid=skip_sid,
return await self.server.send(data, to=to, room=room,
skip_sid=skip_sid,
namespace=namespace or self.namespace,
callback=callback)

11
src/socketio/namespace.py

@ -37,19 +37,20 @@ class Namespace(BaseNamespace):
def _set_server(self, server):
self.server = server
def emit(self, event, data=None, room=None, skip_sid=None, namespace=None,
callback=None):
def emit(self, event, data=None, to=None, room=None, skip_sid=None,
namespace=None, callback=None):
"""Emit a custom event to one or more connected clients.
The only difference with the :func:`socketio.Server.emit` method is
that when the ``namespace`` argument is not given the namespace
associated with the class is used.
"""
return self.server.emit(event, data=data, room=room, skip_sid=skip_sid,
return self.server.emit(event, data=data, to=to, room=room,
skip_sid=skip_sid,
namespace=namespace or self.namespace,
callback=callback)
def send(self, data, room=None, skip_sid=None, namespace=None,
def send(self, data, to=None, room=None, skip_sid=None, namespace=None,
callback=None):
"""Send a message to one or more connected clients.
@ -57,7 +58,7 @@ class Namespace(BaseNamespace):
that when the ``namespace`` argument is not given the namespace
associated with the class is used.
"""
return self.server.send(data, room=room, skip_sid=skip_sid,
return self.server.send(data, to=to, room=room, skip_sid=skip_sid,
namespace=namespace or self.namespace,
callback=callback)

12
tests/asyncio/test_asyncio_namespace.py

@ -93,13 +93,14 @@ class TestAsyncNamespace(unittest.TestCase):
ns._set_server(mock_server)
_run(
ns.emit(
'ev', data='data', room='room', skip_sid='skip', callback='cb'
'ev', data='data', to='room', skip_sid='skip', callback='cb'
)
)
ns.server.emit.mock.assert_called_with(
'ev',
data='data',
room='room',
to='room',
room=None,
skip_sid='skip',
namespace='/foo',
callback='cb',
@ -117,6 +118,7 @@ class TestAsyncNamespace(unittest.TestCase):
ns.server.emit.mock.assert_called_with(
'ev',
data='data',
to=None,
room='room',
skip_sid='skip',
namespace='/bar',
@ -128,10 +130,11 @@ class TestAsyncNamespace(unittest.TestCase):
mock_server = mock.MagicMock()
mock_server.send = AsyncMock()
ns._set_server(mock_server)
_run(ns.send(data='data', room='room', skip_sid='skip', callback='cb'))
_run(ns.send(data='data', to='room', skip_sid='skip', callback='cb'))
ns.server.send.mock.assert_called_with(
'data',
room='room',
to='room',
room=None,
skip_sid='skip',
namespace='/foo',
callback='cb',
@ -147,6 +150,7 @@ class TestAsyncNamespace(unittest.TestCase):
)
ns.server.send.mock.assert_called_with(
'data',
to=None,
room='room',
skip_sid='skip',
namespace='/bar',

12
tests/common/test_namespace.py

@ -56,11 +56,12 @@ class TestNamespace(unittest.TestCase):
def test_emit(self):
ns = namespace.Namespace('/foo')
ns._set_server(mock.MagicMock())
ns.emit('ev', data='data', room='room', skip_sid='skip', callback='cb')
ns.emit('ev', data='data', to='room', skip_sid='skip', callback='cb')
ns.server.emit.assert_called_with(
'ev',
data='data',
room='room',
to='room',
room=None,
skip_sid='skip',
namespace='/foo',
callback='cb',
@ -76,6 +77,7 @@ class TestNamespace(unittest.TestCase):
ns.server.emit.assert_called_with(
'ev',
data='data',
to=None,
room='room',
skip_sid='skip',
namespace='/bar',
@ -85,10 +87,11 @@ class TestNamespace(unittest.TestCase):
def test_send(self):
ns = namespace.Namespace('/foo')
ns._set_server(mock.MagicMock())
ns.send(data='data', room='room', skip_sid='skip', callback='cb')
ns.send(data='data', to='room', skip_sid='skip', callback='cb')
ns.server.send.assert_called_with(
'data',
room='room',
to='room',
room=None,
skip_sid='skip',
namespace='/foo',
callback='cb',
@ -102,6 +105,7 @@ class TestNamespace(unittest.TestCase):
)
ns.server.send.assert_called_with(
'data',
to=None,
room='room',
skip_sid='skip',
namespace='/bar',

Loading…
Cancel
Save