Skip to content

Commit 7605630

Browse files
Allow custom client subclasses to be used in SimpleClient and AsyncSimpleClient (Fixes miguelgrinberg#1432)
1 parent a598a55 commit 7605630

File tree

4 files changed

+68
-47
lines changed

4 files changed

+68
-47
lines changed

src/socketio/async_simple_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class AsyncSimpleClient:
1212
The positional and keyword arguments given in the constructor are passed
1313
to the underlying :func:`socketio.AsyncClient` object.
1414
"""
15+
client_class = AsyncClient
16+
1517
def __init__(self, *args, **kwargs):
1618
self.client_args = args
1719
self.client_kwargs = kwargs
@@ -60,7 +62,8 @@ async def connect(self, url, headers={}, auth=None, transports=None,
6062
self.namespace = namespace
6163
self.input_buffer = []
6264
self.input_event.clear()
63-
self.client = AsyncClient(*self.client_args, **self.client_kwargs)
65+
self.client = self.client_class(
66+
*self.client_args, **self.client_kwargs)
6467

6568
@self.client.event(namespace=self.namespace)
6669
def connect(): # pragma: no cover

src/socketio/simple_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class SimpleClient:
1212
The positional and keyword arguments given in the constructor are passed
1313
to the underlying :func:`socketio.Client` object.
1414
"""
15+
client_class = Client
16+
1517
def __init__(self, *args, **kwargs):
1618
self.client_args = args
1719
self.client_kwargs = kwargs
@@ -58,7 +60,8 @@ def connect(self, url, headers={}, auth=None, transports=None,
5860
self.namespace = namespace
5961
self.input_buffer = []
6062
self.input_event.clear()
61-
self.client = Client(*self.client_args, **self.client_kwargs)
63+
self.client = self.client_class(
64+
*self.client_args, **self.client_kwargs)
6265

6366
@self.client.event(namespace=self.namespace)
6467
def connect(): # pragma: no cover

tests/async/test_simple_client.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,51 @@ async def test_constructor(self):
1616
assert not client.connected
1717

1818
async def test_connect(self):
19+
mock_client = mock.MagicMock()
20+
original_client_class = AsyncSimpleClient.client_class
21+
AsyncSimpleClient.client_class = mock_client
22+
1923
client = AsyncSimpleClient(123, a='b')
20-
with mock.patch('socketio.async_simple_client.AsyncClient') \
21-
as mock_client:
24+
mock_client.return_value.connect = mock.AsyncMock()
25+
26+
await client.connect('url', headers='h', auth='a', transports='t',
27+
namespace='n', socketio_path='s',
28+
wait_timeout='w')
29+
mock_client.assert_called_once_with(123, a='b')
30+
assert client.client == mock_client()
31+
mock_client().connect.assert_awaited_once_with(
32+
'url', headers='h', auth='a', transports='t',
33+
namespaces=['n'], socketio_path='s', wait_timeout='w')
34+
mock_client().event.call_count == 3
35+
mock_client().on.assert_called_once_with('*', namespace='n')
36+
assert client.namespace == 'n'
37+
assert not client.input_event.is_set()
38+
39+
AsyncSimpleClient.client_class = original_client_class
40+
41+
async def test_connect_context_manager(self):
42+
mock_client = mock.MagicMock()
43+
original_client_class = AsyncSimpleClient.client_class
44+
AsyncSimpleClient.client_class = mock_client
45+
46+
async with AsyncSimpleClient(123, a='b') as client:
2247
mock_client.return_value.connect = mock.AsyncMock()
2348

24-
await client.connect('url', headers='h', auth='a', transports='t',
25-
namespace='n', socketio_path='s',
26-
wait_timeout='w')
49+
await client.connect('url', headers='h', auth='a',
50+
transports='t', namespace='n',
51+
socketio_path='s', wait_timeout='w')
2752
mock_client.assert_called_once_with(123, a='b')
2853
assert client.client == mock_client()
2954
mock_client().connect.assert_awaited_once_with(
3055
'url', headers='h', auth='a', transports='t',
3156
namespaces=['n'], socketio_path='s', wait_timeout='w')
3257
mock_client().event.call_count == 3
33-
mock_client().on.assert_called_once_with('*', namespace='n')
58+
mock_client().on.assert_called_once_with(
59+
'*', namespace='n')
3460
assert client.namespace == 'n'
3561
assert not client.input_event.is_set()
3662

37-
async def test_connect_context_manager(self):
38-
async def _t():
39-
async with AsyncSimpleClient(123, a='b') as client:
40-
with mock.patch('socketio.async_simple_client.AsyncClient') \
41-
as mock_client:
42-
mock_client.return_value.connect = mock.AsyncMock()
43-
44-
await client.connect('url', headers='h', auth='a',
45-
transports='t', namespace='n',
46-
socketio_path='s', wait_timeout='w')
47-
mock_client.assert_called_once_with(123, a='b')
48-
assert client.client == mock_client()
49-
mock_client().connect.assert_awaited_once_with(
50-
'url', headers='h', auth='a', transports='t',
51-
namespaces=['n'], socketio_path='s', wait_timeout='w')
52-
mock_client().event.call_count == 3
53-
mock_client().on.assert_called_once_with(
54-
'*', namespace='n')
55-
assert client.namespace == 'n'
56-
assert not client.input_event.is_set()
57-
58-
await _t()
63+
AsyncSimpleClient.client_class = original_client_class
5964

6065
async def test_connect_twice(self):
6166
client = AsyncSimpleClient(123, a='b')

tests/common/test_simple_client.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,34 @@ def test_constructor(self):
1414
assert not client.connected
1515

1616
def test_connect(self):
17+
mock_client = mock.MagicMock()
18+
original_client_class = SimpleClient.client_class
19+
SimpleClient.client_class = mock_client
20+
1721
client = SimpleClient(123, a='b')
18-
with mock.patch('socketio.simple_client.Client') as mock_client:
22+
client.connect('url', headers='h', auth='a', transports='t',
23+
namespace='n', socketio_path='s', wait_timeout='w')
24+
mock_client.assert_called_once_with(123, a='b')
25+
assert client.client == mock_client()
26+
mock_client().connect.assert_called_once_with(
27+
'url', headers='h', auth='a', transports='t',
28+
namespaces=['n'], socketio_path='s', wait_timeout='w')
29+
mock_client().event.call_count == 3
30+
mock_client().on.assert_called_once_with('*', namespace='n')
31+
assert client.namespace == 'n'
32+
assert not client.input_event.is_set()
33+
34+
SimpleClient.client_class = original_client_class
35+
36+
def test_connect_context_manager(self):
37+
mock_client = mock.MagicMock()
38+
original_client_class = SimpleClient.client_class
39+
SimpleClient.client_class = mock_client
40+
41+
with SimpleClient(123, a='b') as client:
1942
client.connect('url', headers='h', auth='a', transports='t',
20-
namespace='n', socketio_path='s', wait_timeout='w')
43+
namespace='n', socketio_path='s',
44+
wait_timeout='w')
2145
mock_client.assert_called_once_with(123, a='b')
2246
assert client.client == mock_client()
2347
mock_client().connect.assert_called_once_with(
@@ -28,21 +52,7 @@ def test_connect(self):
2852
assert client.namespace == 'n'
2953
assert not client.input_event.is_set()
3054

31-
def test_connect_context_manager(self):
32-
with SimpleClient(123, a='b') as client:
33-
with mock.patch('socketio.simple_client.Client') as mock_client:
34-
client.connect('url', headers='h', auth='a', transports='t',
35-
namespace='n', socketio_path='s',
36-
wait_timeout='w')
37-
mock_client.assert_called_once_with(123, a='b')
38-
assert client.client == mock_client()
39-
mock_client().connect.assert_called_once_with(
40-
'url', headers='h', auth='a', transports='t',
41-
namespaces=['n'], socketio_path='s', wait_timeout='w')
42-
mock_client().event.call_count == 3
43-
mock_client().on.assert_called_once_with('*', namespace='n')
44-
assert client.namespace == 'n'
45-
assert not client.input_event.is_set()
55+
SimpleClient.client_class = original_client_class
4656

4757
def test_connect_twice(self):
4858
client = SimpleClient(123, a='b')

0 commit comments

Comments
 (0)