Browse Source
Support sending bytestrings when using pub/sub managers
pull/1536/head
Miguel Grinberg
7 months ago
Failed to extract signature
4 changed files with
66 additions and
2 deletions
-
src/socketio/packet.py
-
tests/async/test_pubsub_manager.py
-
tests/common/test_packet.py
-
tests/common/test_pubsub_manager.py
|
|
|
@ -154,7 +154,7 @@ class Packet: |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def _deconstruct_binary_internal(cls, data, attachments): |
|
|
|
if isinstance(data, bytes): |
|
|
|
if isinstance(data, (bytes, bytearray)): |
|
|
|
attachments.append(data) |
|
|
|
return {'_placeholder': True, 'num': len(attachments) - 1} |
|
|
|
elif isinstance(data, list): |
|
|
|
@ -169,7 +169,7 @@ class Packet: |
|
|
|
@classmethod |
|
|
|
def data_is_binary(cls, data): |
|
|
|
"""Check if the data contains binary components.""" |
|
|
|
if isinstance(data, bytes): |
|
|
|
if isinstance(data, (bytes, bytearray)): |
|
|
|
return True |
|
|
|
elif isinstance(data, list): |
|
|
|
return functools.reduce( |
|
|
|
|
|
|
|
@ -97,6 +97,36 @@ class TestAsyncPubSubManager: |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
async def test_emit_bytearray(self): |
|
|
|
await self.pm.emit('foo', bytearray(b'bar')) |
|
|
|
self.pm._publish.assert_awaited_once_with( |
|
|
|
{ |
|
|
|
'method': 'emit', |
|
|
|
'event': 'foo', |
|
|
|
'binary': True, |
|
|
|
'data': [{'_placeholder': True, 'num': 0}, 'YmFy'], |
|
|
|
'namespace': '/', |
|
|
|
'room': None, |
|
|
|
'skip_sid': None, |
|
|
|
'callback': None, |
|
|
|
'host_id': '123456', |
|
|
|
} |
|
|
|
) |
|
|
|
await self.pm.emit('foo', {'foo': bytearray(b'bar')}) |
|
|
|
self.pm._publish.assert_awaited_with( |
|
|
|
{ |
|
|
|
'method': 'emit', |
|
|
|
'event': 'foo', |
|
|
|
'binary': True, |
|
|
|
'data': [{'foo': {'_placeholder': True, 'num': 0}}, 'YmFy'], |
|
|
|
'namespace': '/', |
|
|
|
'room': None, |
|
|
|
'skip_sid': None, |
|
|
|
'callback': None, |
|
|
|
'host_id': '123456', |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
async def test_emit_with_to(self): |
|
|
|
sid = 'room-mate' |
|
|
|
await self.pm.emit('foo', 'bar', to=sid) |
|
|
|
|
|
|
|
@ -279,11 +279,15 @@ class TestPacket: |
|
|
|
assert not pkt.data_is_binary(['foo']) |
|
|
|
assert not pkt.data_is_binary([]) |
|
|
|
assert pkt.data_is_binary([b'foo']) |
|
|
|
assert pkt.data_is_binary([bytearray(b'foo')]) |
|
|
|
assert pkt.data_is_binary(['foo', b'bar']) |
|
|
|
assert pkt.data_is_binary(['foo', bytearray(b'bar')]) |
|
|
|
|
|
|
|
def test_data_is_binary_dict(self): |
|
|
|
pkt = packet.Packet() |
|
|
|
assert not pkt.data_is_binary({'a': 'foo'}) |
|
|
|
assert not pkt.data_is_binary({}) |
|
|
|
assert pkt.data_is_binary({'a': b'foo'}) |
|
|
|
assert pkt.data_is_binary({'a': bytearray(b'foo')}) |
|
|
|
assert pkt.data_is_binary({'a': 'foo', 'b': b'bar'}) |
|
|
|
assert pkt.data_is_binary({'a': 'foo', 'b': bytearray(b'bar')}) |
|
|
|
|
|
|
|
@ -109,6 +109,36 @@ class TestPubSubManager: |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
def test_emit_bytearray(self): |
|
|
|
self.pm.emit('foo', bytearray(b'bar')) |
|
|
|
self.pm._publish.assert_called_once_with( |
|
|
|
{ |
|
|
|
'method': 'emit', |
|
|
|
'event': 'foo', |
|
|
|
'binary': True, |
|
|
|
'data': [{'_placeholder': True, 'num': 0}, 'YmFy'], |
|
|
|
'namespace': '/', |
|
|
|
'room': None, |
|
|
|
'skip_sid': None, |
|
|
|
'callback': None, |
|
|
|
'host_id': '123456', |
|
|
|
} |
|
|
|
) |
|
|
|
self.pm.emit('foo', {'foo': bytearray(b'bar')}) |
|
|
|
self.pm._publish.assert_called_with( |
|
|
|
{ |
|
|
|
'method': 'emit', |
|
|
|
'event': 'foo', |
|
|
|
'binary': True, |
|
|
|
'data': [{'foo': {'_placeholder': True, 'num': 0}}, 'YmFy'], |
|
|
|
'namespace': '/', |
|
|
|
'room': None, |
|
|
|
'skip_sid': None, |
|
|
|
'callback': None, |
|
|
|
'host_id': '123456', |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
def test_emit_with_to(self): |
|
|
|
sid = "ferris" |
|
|
|
self.pm.emit('foo', 'bar', to=sid) |
|
|
|
|