Skip to content

Commit

Permalink
Support binary packets with zero length (Fixes #257)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 20, 2021
1 parent 531d28a commit bcd1a42
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/engineio/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, packet_type=NOOP, data=None, encoded_packet=None):
self.binary = False
if self.binary and self.packet_type != MESSAGE:
raise ValueError('Binary packets can only be of type MESSAGE')
if encoded_packet:
if encoded_packet is not None:
self.decode(encoded_packet)

def encode(self, b64=False):
Expand Down
31 changes: 27 additions & 4 deletions tests/common/test_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ def test_decode_text_packet(self):
pkt = packet.Packet(encoded_packet=b'4text')
assert pkt.encode() == b'4text'

def test_encode_empty_text_packet(self):
data = ''
pkt = packet.Packet(packet.MESSAGE, data=data)
assert pkt.packet_type == packet.MESSAGE
assert pkt.data == data
assert not pkt.binary
assert pkt.encode() == '4'

def test_decode_empty_text_packet(self):
pkt = packet.Packet(encoded_packet=b'4')
assert pkt.encode() == b'4'

def test_encode_binary_packet(self):
pkt = packet.Packet(packet.MESSAGE, data=b'\x01\x02\x03')
assert pkt.packet_type == packet.MESSAGE
Expand All @@ -50,17 +62,28 @@ def test_encode_binary_b64_packet(self):
assert pkt.binary
assert pkt.encode(b64=True) == 'bAQIDBA=='

def test_encode_empty_binary_packet(self):
pkt = packet.Packet(packet.MESSAGE, data=b'')
assert pkt.packet_type == packet.MESSAGE
assert pkt.data == b''
assert pkt.binary
assert pkt.encode() == b''

def test_decode_binary_packet(self):
pkt = packet.Packet(encoded_packet=b'\x04\x01\x02\x03')
assert pkt.encode(), b'\x04\x01\x02\x03'
assert pkt.encode() == b'\x04\x01\x02\x03'

def test_decode_binary_bytearray_packet(self):
pkt = packet.Packet(encoded_packet=bytearray(b'\x04\x01\x02\x03'))
assert pkt.encode(), b'\x04\x01\x02\x03'
assert pkt.encode() == b'\x04\x01\x02\x03'

def test_decode_binary_b64_packet(self):
pkt = packet.Packet(encoded_packet=b'b4AAEC')
assert pkt.encode(), b'\x04\x01\x02\x03'
pkt = packet.Packet(encoded_packet='bBAECAw==')
assert pkt.encode() == b'\x04\x01\x02\x03'

def test_decode_empty_binary_packet(self):
pkt = packet.Packet(encoded_packet=b'')
assert pkt.encode() == b''

def test_encode_json_packet(self):
pkt = packet.Packet(packet.MESSAGE, data={'a': 123, 'b': '456'})
Expand Down

0 comments on commit bcd1a42

Please sign in to comment.