Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ a :class:`socketio.exceptions.ConnectionRefusedError` exception can be raised,
and all of its arguments will be sent to the client with the rejection
message::

from socketio.exceptions import ConnectionRefusedError

@sio.event
def connect(sid, environ, auth):
raise ConnectionRefusedError('authentication failed')
Expand Down
3 changes: 3 additions & 0 deletions src/socketio/async_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,9 @@ async def _handle_connect(self, eio_sid, namespace, data):
except exceptions.ConnectionRefusedError as exc:
fail_reason = exc.error_args
success = False
except ConnectionRefusedError:
fail_reason = {"message": "Connection refused by server"}
success = False

if success is False:
if self.always_connect:
Expand Down
3 changes: 3 additions & 0 deletions src/socketio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ def _handle_connect(self, eio_sid, namespace, data):
except exceptions.ConnectionRefusedError as exc:
fail_reason = exc.error_args
success = False
except ConnectionRefusedError:
fail_reason = {"message": "Connection refused by server"}
success = False

if success is False:
if self.always_connect:
Expand Down
15 changes: 15 additions & 0 deletions tests/async/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,21 @@ async def test_handle_connect_rejected_with_exception(self, eio):
'123', '4{"message":"fail_reason"}')
assert s.environ == {'123': 'environ'}

async def test_handle_connect_rejected_with_python_exception(self, eio):
eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer()
handler = mock.MagicMock(
side_effect=ConnectionRefusedError()
)
s.on('connect', handler)
await s._handle_eio_connect('123', 'environ')
await s._handle_eio_message('123', '0')
assert not s.manager.is_connected('1', '/')
handler.assert_called_once_with('1', 'environ')
s.eio.send.assert_awaited_once_with(
'123', '4{"message":"Connection refused by server"}')
assert s.environ == {'123': 'environ'}

async def test_handle_connect_rejected_with_empty_exception(self, eio):
eio.return_value.send = mock.AsyncMock()
s = async_server.AsyncServer()
Expand Down
14 changes: 14 additions & 0 deletions tests/common/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,20 @@ def test_handle_connect_rejected_with_exception(self, eio):
s.eio.send.assert_called_once_with('123', '4{"message":"fail_reason"}')
assert s.environ == {'123': 'environ'}

def test_handle_connect_rejected_with_python_exception(self, eio):
s = server.Server()
handler = mock.MagicMock(
side_effect=ConnectionRefusedError()
)
s.on('connect', handler)
s._handle_eio_connect('123', 'environ')
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{"message":"Connection refused by server"}')
assert s.environ == {'123': 'environ'}

def test_handle_connect_rejected_with_empty_exception(self, eio):
s = server.Server()
handler = mock.MagicMock(
Expand Down
Loading