Browse Source

minor class-based namespace fixes

pull/56/head
Miguel Grinberg 9 years ago
parent
commit
214abc8d31
  1. 9
      socketio/namespace.py
  2. 2
      socketio/server.py
  3. 22
      tests/test_namespace.py

9
socketio/namespace.py

@ -14,10 +14,17 @@ class Namespace(object):
self.namespace = namespace or '/' self.namespace = namespace or '/'
self.server = None self.server = None
def set_server(self, server): def _set_server(self, server):
self.server = server self.server = server
def trigger_event(self, event, *args): def trigger_event(self, event, *args):
"""Dispatch an event to the proper handler method.
In the most common usage, this method is not overloaded by subclasses,
as it performs the routing of events to methods. However, this
method can be overriden if special dispatching rules are needed, or if
having a single method that catches all events is desired.
"""
handler_name = 'on_' + event handler_name = 'on_' + event
if hasattr(self, handler_name): if hasattr(self, handler_name):
return getattr(self, handler_name)(*args) return getattr(self, handler_name)(*args)

2
socketio/server.py

@ -161,7 +161,7 @@ class Server(object):
""" """
if not isinstance(namespace_handler, namespace.Namespace): if not isinstance(namespace_handler, namespace.Namespace):
raise ValueError('Not a namespace instance') raise ValueError('Not a namespace instance')
namespace_handler.set_server(self) namespace_handler._set_server(self)
self.namespace_handlers[namespace_handler.namespace] = \ self.namespace_handlers[namespace_handler.namespace] = \
namespace_handler namespace_handler

22
tests/test_namespace.py

@ -17,7 +17,7 @@ class TestNamespace(unittest.TestCase):
result['result'] = (sid, environ) result['result'] = (sid, environ)
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.trigger_event('connect', 'sid', {'foo': 'bar'}) ns.trigger_event('connect', 'sid', {'foo': 'bar'})
self.assertEqual(result['result'], ('sid', {'foo': 'bar'})) self.assertEqual(result['result'], ('sid', {'foo': 'bar'}))
@ -29,7 +29,7 @@ class TestNamespace(unittest.TestCase):
result['result'] = sid result['result'] = sid
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.trigger_event('disconnect', 'sid') ns.trigger_event('disconnect', 'sid')
self.assertEqual(result['result'], 'sid') self.assertEqual(result['result'], 'sid')
@ -41,7 +41,7 @@ class TestNamespace(unittest.TestCase):
result['result'] = (sid, data) result['result'] = (sid, data)
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.trigger_event('custom_message', 'sid', {'data': 'data'}) ns.trigger_event('custom_message', 'sid', {'data': 'data'})
self.assertEqual(result['result'], ('sid', {'data': 'data'})) self.assertEqual(result['result'], ('sid', {'data': 'data'}))
@ -53,13 +53,13 @@ class TestNamespace(unittest.TestCase):
result['result'] = (sid, data) result['result'] = (sid, data)
ns = MyNamespace('/foo') ns = MyNamespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.trigger_event('another_custom_message', 'sid', {'data': 'data'}) ns.trigger_event('another_custom_message', 'sid', {'data': 'data'})
self.assertEqual(result, {}) self.assertEqual(result, {})
def test_emit(self): def test_emit(self):
ns = namespace.Namespace('/foo') ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.emit('ev', data='data', room='room', skip_sid='skip', ns.emit('ev', data='data', room='room', skip_sid='skip',
callback='cb') callback='cb')
ns.server.emit.assert_called_with( ns.server.emit.assert_called_with(
@ -73,7 +73,7 @@ class TestNamespace(unittest.TestCase):
def test_send(self): def test_send(self):
ns = namespace.Namespace('/foo') ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.send(data='data', room='room', skip_sid='skip', callback='cb') ns.send(data='data', room='room', skip_sid='skip', callback='cb')
ns.server.send.assert_called_with( ns.server.send.assert_called_with(
'data', room='room', skip_sid='skip', namespace='/foo', 'data', room='room', skip_sid='skip', namespace='/foo',
@ -86,7 +86,7 @@ class TestNamespace(unittest.TestCase):
def test_enter_room(self): def test_enter_room(self):
ns = namespace.Namespace('/foo') ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.enter_room('sid', 'room') ns.enter_room('sid', 'room')
ns.server.enter_room.assert_called_with('sid', 'room', ns.server.enter_room.assert_called_with('sid', 'room',
namespace='/foo') namespace='/foo')
@ -96,7 +96,7 @@ class TestNamespace(unittest.TestCase):
def test_leave_room(self): def test_leave_room(self):
ns = namespace.Namespace('/foo') ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.leave_room('sid', 'room') ns.leave_room('sid', 'room')
ns.server.leave_room.assert_called_with('sid', 'room', ns.server.leave_room.assert_called_with('sid', 'room',
namespace='/foo') namespace='/foo')
@ -106,7 +106,7 @@ class TestNamespace(unittest.TestCase):
def test_close_room(self): def test_close_room(self):
ns = namespace.Namespace('/foo') ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.close_room('room') ns.close_room('room')
ns.server.close_room.assert_called_with('room', namespace='/foo') ns.server.close_room.assert_called_with('room', namespace='/foo')
ns.close_room('room', namespace='/bar') ns.close_room('room', namespace='/bar')
@ -114,7 +114,7 @@ class TestNamespace(unittest.TestCase):
def test_rooms(self): def test_rooms(self):
ns = namespace.Namespace('/foo') ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.rooms('sid') ns.rooms('sid')
ns.server.rooms.assert_called_with('sid', namespace='/foo') ns.server.rooms.assert_called_with('sid', namespace='/foo')
ns.rooms('sid', namespace='/bar') ns.rooms('sid', namespace='/bar')
@ -122,7 +122,7 @@ class TestNamespace(unittest.TestCase):
def test_disconnect(self): def test_disconnect(self):
ns = namespace.Namespace('/foo') ns = namespace.Namespace('/foo')
ns.set_server(mock.MagicMock()) ns._set_server(mock.MagicMock())
ns.disconnect('sid') ns.disconnect('sid')
ns.server.disconnect.assert_called_with('sid', namespace='/foo') ns.server.disconnect.assert_called_with('sid', namespace='/foo')
ns.disconnect('sid', namespace='/bar') ns.disconnect('sid', namespace='/bar')

Loading…
Cancel
Save