Skip to content

Commit f3b18bd

Browse files
Support Python's ConnectionRefusedError to reject a connection (#1515)
* Support Python's ConnectionRefusedError to reject a connection * unit tests
1 parent 194e1b7 commit f3b18bd

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

docs/server.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ a :class:`socketio.exceptions.ConnectionRefusedError` exception can be raised,
246246
and all of its arguments will be sent to the client with the rejection
247247
message::
248248

249+
from socketio.exceptions import ConnectionRefusedError
250+
249251
@sio.event
250252
def connect(sid, environ, auth):
251253
raise ConnectionRefusedError('authentication failed')

src/socketio/async_server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@ async def _handle_connect(self, eio_sid, namespace, data):
561561
except exceptions.ConnectionRefusedError as exc:
562562
fail_reason = exc.error_args
563563
success = False
564+
except ConnectionRefusedError:
565+
fail_reason = {"message": "Connection refused by server"}
566+
success = False
564567

565568
if success is False:
566569
if self.always_connect:

src/socketio/server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ def _handle_connect(self, eio_sid, namespace, data):
543543
except exceptions.ConnectionRefusedError as exc:
544544
fail_reason = exc.error_args
545545
success = False
546+
except ConnectionRefusedError:
547+
fail_reason = {"message": "Connection refused by server"}
548+
success = False
546549

547550
if success is False:
548551
if self.always_connect:

tests/async/test_server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,21 @@ async def test_handle_connect_rejected_with_exception(self, eio):
482482
'123', '4{"message":"fail_reason"}')
483483
assert s.environ == {'123': 'environ'}
484484

485+
async def test_handle_connect_rejected_with_python_exception(self, eio):
486+
eio.return_value.send = mock.AsyncMock()
487+
s = async_server.AsyncServer()
488+
handler = mock.MagicMock(
489+
side_effect=ConnectionRefusedError()
490+
)
491+
s.on('connect', handler)
492+
await s._handle_eio_connect('123', 'environ')
493+
await s._handle_eio_message('123', '0')
494+
assert not s.manager.is_connected('1', '/')
495+
handler.assert_called_once_with('1', 'environ')
496+
s.eio.send.assert_awaited_once_with(
497+
'123', '4{"message":"Connection refused by server"}')
498+
assert s.environ == {'123': 'environ'}
499+
485500
async def test_handle_connect_rejected_with_empty_exception(self, eio):
486501
eio.return_value.send = mock.AsyncMock()
487502
s = async_server.AsyncServer()

tests/common/test_server.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,20 @@ def test_handle_connect_rejected_with_exception(self, eio):
462462
s.eio.send.assert_called_once_with('123', '4{"message":"fail_reason"}')
463463
assert s.environ == {'123': 'environ'}
464464

465+
def test_handle_connect_rejected_with_python_exception(self, eio):
466+
s = server.Server()
467+
handler = mock.MagicMock(
468+
side_effect=ConnectionRefusedError()
469+
)
470+
s.on('connect', handler)
471+
s._handle_eio_connect('123', 'environ')
472+
s._handle_eio_message('123', '0')
473+
assert not s.manager.is_connected('1', '/')
474+
handler.assert_called_once_with('1', 'environ')
475+
s.eio.send.assert_called_once_with(
476+
'123', '4{"message":"Connection refused by server"}')
477+
assert s.environ == {'123': 'environ'}
478+
465479
def test_handle_connect_rejected_with_empty_exception(self, eio):
466480
s = server.Server()
467481
handler = mock.MagicMock(

0 commit comments

Comments
 (0)