Browse Source

Include error message and arguments in CONNECT_ERROR packet (Fixes #590)

pull/599/head
Miguel Grinberg 4 years ago
parent
commit
314971c8a0
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 2
      socketio/asyncio_server.py
  2. 12
      socketio/exceptions.py
  3. 2
      socketio/server.py
  4. 28
      tests/asyncio/test_asyncio_server.py
  5. 24
      tests/common/test_server.py

2
socketio/asyncio_server.py

@ -411,7 +411,7 @@ class AsyncServer(server.Server):
if self.always_connect:
await self._send_packet(eio_sid, packet.Packet(
packet.CONNECT, {'sid': sid}, namespace=namespace))
fail_reason = None
fail_reason = exceptions.ConnectionRefusedError().error_args
try:
success = await self._trigger_event('connect', namespace, sid,
self.environ[eio_sid])

12
socketio/exceptions.py

@ -15,11 +15,15 @@ class ConnectionRefusedError(ConnectionError):
"""
def __init__(self, *args):
if len(args) == 0:
self.error_args = None
elif len(args) == 1 and not isinstance(args[0], list):
self.error_args = args[0]
self.error_args = {'message': 'Connection rejected by server'}
elif len(args) == 1:
self.error_args = {'message': str(args[0])}
else:
self.error_args = args
self.error_args = {'message': str(args[0])}
if len(args) == 2:
self.error_args['data'] = args[1]
else:
self.error_args['data'] = args[1:]
class TimeoutError(SocketIOError):

2
socketio/server.py

@ -618,7 +618,7 @@ class Server(object):
if self.always_connect:
self._send_packet(eio_sid, packet.Packet(
packet.CONNECT, {'sid': sid}, namespace=namespace))
fail_reason = None
fail_reason = exceptions.ConnectionRefusedError().error_args
try:
success = self._trigger_event('connect', namespace, sid,
self.environ[eio_sid])

28
tests/asyncio/test_asyncio_server.py

@ -429,7 +429,8 @@ class TestAsyncServer(unittest.TestCase):
_run(s._handle_eio_message('123', '0'))
assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with('123', '4')
s.eio.send.mock.assert_called_once_with(
'123', '4{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected(self, eio):
@ -441,7 +442,8 @@ class TestAsyncServer(unittest.TestCase):
_run(s._handle_eio_message('123', '0/foo'))
assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_any_call('123', '4/foo')
s.eio.send.mock.assert_any_call(
'123', '4/foo,{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_rejected_always_connect(self, eio):
@ -454,7 +456,8 @@ class TestAsyncServer(unittest.TestCase):
assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_any_call('123', '0{"sid":"1"}')
s.eio.send.mock.assert_any_call('123', '1')
s.eio.send.mock.assert_any_call(
'123', '1{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected_always_connect(self, eio):
@ -467,7 +470,8 @@ class TestAsyncServer(unittest.TestCase):
assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_any_call('123', '0/foo,{"sid":"1"}')
s.eio.send.mock.assert_any_call('123', '1/foo')
s.eio.send.mock.assert_any_call(
'123', '1/foo,{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_rejected_with_exception(self, eio):
@ -481,7 +485,8 @@ class TestAsyncServer(unittest.TestCase):
_run(s._handle_eio_message('123', '0'))
assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with('123', '4"fail_reason"')
s.eio.send.mock.assert_called_once_with(
'123', '4{"message":"fail_reason"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_rejected_with_empty_exception(self, eio):
@ -495,22 +500,24 @@ class TestAsyncServer(unittest.TestCase):
_run(s._handle_eio_message('123', '0'))
assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with('123', '4')
s.eio.send.mock.assert_called_once_with(
'123', '4{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected_with_exception(self, eio):
eio.return_value.send = AsyncMock()
s = asyncio_server.AsyncServer()
handler = mock.MagicMock(
side_effect=exceptions.ConnectionRefusedError('fail_reason', 1)
side_effect=exceptions.ConnectionRefusedError(
'fail_reason', 1, '2')
)
s.on('connect', handler, namespace='/foo')
_run(s._handle_eio_connect('123', 'environ'))
_run(s._handle_eio_message('123', '0/foo'))
assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with('123',
'4/foo,["fail_reason",1]')
s.eio.send.mock.assert_called_once_with(
'123', '4/foo,{"message":"fail_reason","data":[1,"2"]}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected_with_empty_exception(self, eio):
@ -524,7 +531,8 @@ class TestAsyncServer(unittest.TestCase):
_run(s._handle_eio_message('123', '0/foo'))
assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ')
s.eio.send.mock.assert_called_once_with('123', '4/foo')
s.eio.send.mock.assert_called_once_with(
'123', '4/foo,{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_disconnect(self, eio):

24
tests/common/test_server.py

@ -359,7 +359,8 @@ class TestServer(unittest.TestCase):
assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ')
assert not s.manager.is_connected('1', '/')
s.eio.send.assert_called_once_with('123', '4')
s.eio.send.assert_called_once_with(
'123', '4{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected(self, eio):
@ -371,7 +372,8 @@ class TestServer(unittest.TestCase):
assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ')
assert not s.manager.is_connected('1', '/foo')
s.eio.send.assert_called_once_with('123', '4/foo')
s.eio.send.assert_called_once_with(
'123', '4/foo,{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_rejected_always_connect(self, eio):
@ -383,7 +385,8 @@ class TestServer(unittest.TestCase):
assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ')
s.eio.send.assert_any_call('123', '0{"sid":"1"}')
s.eio.send.assert_any_call('123', '1')
s.eio.send.assert_any_call(
'123', '1{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected_always_connect(self, eio):
@ -395,7 +398,8 @@ class TestServer(unittest.TestCase):
assert not s.manager.is_connected('1', '/foo')
handler.assert_called_once_with('1', 'environ')
s.eio.send.assert_any_call('123', '0/foo,{"sid":"1"}')
s.eio.send.assert_any_call('123', '1/foo')
s.eio.send.assert_any_call(
'123', '1/foo,{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_rejected_with_exception(self, eio):
@ -408,7 +412,7 @@ class TestServer(unittest.TestCase):
s._handle_eio_message('123', '0')
assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ')
s.eio.send.assert_called_once_with('123', '4"fail_reason"')
s.eio.send.assert_called_once_with('123', '4{"message":"fail_reason"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_rejected_with_empty_exception(self, eio):
@ -421,20 +425,21 @@ class TestServer(unittest.TestCase):
s._handle_eio_message('123', '0')
assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ')
s.eio.send.assert_called_once_with('123', '4')
s.eio.send.assert_called_once_with(
'123', '4{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_connect_namespace_rejected_with_exception(self, eio):
s = server.Server()
handler = mock.MagicMock(
side_effect=exceptions.ConnectionRefusedError(u'fail_reason', 1)
side_effect=exceptions.ConnectionRefusedError('fail_reason', 1)
)
s.on('connect', handler, namespace='/foo')
s._handle_eio_connect('123', 'environ')
s._handle_eio_message('123', '0/foo')
assert not s.manager.is_connected('1', '/foo')
s.eio.send.assert_called_once_with(
'123', '4/foo,["fail_reason",1]'
'123', '4/foo,{"message":"fail_reason","data":1}'
)
assert s.environ == {'123': 'environ'}
@ -447,7 +452,8 @@ class TestServer(unittest.TestCase):
s._handle_eio_connect('123', 'environ')
s._handle_eio_message('123', '0/foo')
assert not s.manager.is_connected('1', '/foo')
s.eio.send.assert_called_once_with('123', '4/foo')
s.eio.send.assert_called_once_with(
'123', '4/foo,{"message":"Connection rejected by server"}')
assert s.environ == {'123': 'environ'}
def test_handle_disconnect(self, eio):

Loading…
Cancel
Save