Browse Source

Use correct binary packet types in the msgpack packet encoder (Fixes #811)

pull/844/head
Miguel Grinberg 3 years ago
parent
commit
60735dd4c2
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 2
      src/socketio/msgpack_packet.py
  2. 5
      src/socketio/packet.py
  3. 12
      tests/common/test_msgpack_packet.py

2
src/socketio/msgpack_packet.py

@ -3,6 +3,8 @@ from . import packet
class MsgPackPacket(packet.Packet):
uses_binary_events = False
def encode(self):
"""Encode the packet for transmission."""
return msgpack.dumps(self._to_dict())

5
src/socketio/packet.py

@ -19,6 +19,7 @@ class Packet(object):
# id: ASCII encoded, only if id is not None
# data: JSON dump of data payload
uses_binary_events = True
json = _json
def __init__(self, packet_type=EVENT, data=None, namespace=None, id=None,
@ -27,7 +28,9 @@ class Packet(object):
self.data = data
self.namespace = namespace
self.id = id
if binary or (binary is None and self._data_is_binary(self.data)):
if self.uses_binary_events and \
(binary or (binary is None and self._data_is_binary(
self.data))):
if self.packet_type == EVENT:
self.packet_type = BINARY_EVENT
elif self.packet_type == ACK:

12
tests/common/test_msgpack_packet.py

@ -22,3 +22,15 @@ class TestMsgPackPacket(unittest.TestCase):
assert p.data == p2.data
assert p.id == p2.id
assert p.namespace == p2.namespace
def test_encode_binary_event_packet(self):
p = msgpack_packet.MsgPackPacket(packet.EVENT, data={'foo': b'bar'})
assert p.packet_type == packet.EVENT
p2 = msgpack_packet.MsgPackPacket(encoded_packet=p.encode())
assert p2.data == {'foo': b'bar'}
def test_encode_binary_ack_packet(self):
p = msgpack_packet.MsgPackPacket(packet.ACK, data={'foo': b'bar'})
assert p.packet_type == packet.ACK
p2 = msgpack_packet.MsgPackPacket(encoded_packet=p.encode())
assert p2.data == {'foo': b'bar'}

Loading…
Cancel
Save