Browse Source
Support Python's ConnectionRefusedError to reject a connection (#1515)
* Support Python's ConnectionRefusedError to reject a connection
* unit tests
pull/1516/head
Miguel Grinberg
7 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with
37 additions and
0 deletions
-
docs/server.rst
-
src/socketio/async_server.py
-
src/socketio/server.py
-
tests/async/test_server.py
-
tests/common/test_server.py
|
|
|
@ -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') |
|
|
|
|
|
|
|
@ -561,6 +561,9 @@ class AsyncServer(base_server.BaseServer): |
|
|
|
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: |
|
|
|
|
|
|
|
@ -543,6 +543,9 @@ class Server(base_server.BaseServer): |
|
|
|
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: |
|
|
|
|
|
|
|
@ -482,6 +482,21 @@ class TestAsyncServer: |
|
|
|
'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() |
|
|
|
|
|
|
|
@ -462,6 +462,20 @@ class TestServer: |
|
|
|
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( |
|
|
|
|