Skip to content

Commit

Permalink
Improved routing to catch-all namespace handlers (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
Champgy authored Mar 24, 2024
1 parent fff4640 commit bd39b8f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/socketio/base_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def _get_event_handler(self, event, namespace, args):
'*' in self.handlers[namespace]:
handler = self.handlers[namespace]['*']
args = (event, *args)
elif '*' in self.handlers:
if handler is None and '*' in self.handlers:
if event in self.handlers['*']:
handler = self.handlers['*'][event]
args = (namespace, *args)
Expand All @@ -245,7 +245,7 @@ def _get_namespace_handler(self, namespace, args):
handler = None
if namespace in self.namespace_handlers:
handler = self.namespace_handlers[namespace]
elif '*' in self.namespace_handlers:
if handler is None and '*' in self.namespace_handlers:
handler = self.namespace_handlers['*']
args = (namespace, *args)
return handler, args
Expand Down
9 changes: 9 additions & 0 deletions tests/common/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,16 +578,20 @@ def test_handle_event_with_catchall_namespace(self, eio):
s = server.Server(async_handlers=False)
sid_foo = s.manager.connect('123', '/foo')
sid_bar = s.manager.connect('123', '/bar')
sid_baz = s.manager.connect('123', '/baz')
connect_star_handler = mock.MagicMock()
msg_foo_handler = mock.MagicMock()
msg_star_handler = mock.MagicMock()
star_foo_handler = mock.MagicMock()
star_star_handler = mock.MagicMock()
my_message_baz_handler = mock.MagicMock()
s.on('connect', connect_star_handler, namespace='*')
s.on('msg', msg_foo_handler, namespace='/foo')
s.on('msg', msg_star_handler, namespace='*')
s.on('*', star_foo_handler, namespace='/foo')
s.on('*', star_star_handler, namespace='*')
s.on('my message', my_message_baz_handler, namespace='/baz')

s._trigger_event('connect', '/bar', sid_bar)
s._handle_eio_message('123', '2/foo,["msg","a","b"]')
s._handle_eio_message('123', '2/bar,["msg","a","b"]')
Expand All @@ -602,6 +606,11 @@ def test_handle_event_with_catchall_namespace(self, eio):
star_star_handler.assert_called_once_with(
'my message', '/bar', sid_bar, 'a', 'b', 'c')

s._handle_eio_message('123', '2/baz,["my message","a","b","c"]')
s._handle_eio_message('123', '2/baz,["msg","a","b"]')
my_message_baz_handler.assert_called_once_with(sid_baz, 'a', 'b', 'c')
msg_star_handler.assert_called_with('/baz', sid_baz, 'a', 'b')

def test_handle_event_with_disconnected_namespace(self, eio):
s = server.Server(async_handlers=False)
s.manager.connect('123', '/foo')
Expand Down

0 comments on commit bd39b8f

Please sign in to comment.