Skip to content

Commit bcd1a42

Browse files
Support binary packets with zero length (Fixes #257)
1 parent 531d28a commit bcd1a42

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/engineio/packet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, packet_type=NOOP, data=None, encoded_packet=None):
2323
self.binary = False
2424
if self.binary and self.packet_type != MESSAGE:
2525
raise ValueError('Binary packets can only be of type MESSAGE')
26-
if encoded_packet:
26+
if encoded_packet is not None:
2727
self.decode(encoded_packet)
2828

2929
def encode(self, b64=False):

tests/common/test_packet.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ def test_decode_text_packet(self):
2929
pkt = packet.Packet(encoded_packet=b'4text')
3030
assert pkt.encode() == b'4text'
3131

32+
def test_encode_empty_text_packet(self):
33+
data = ''
34+
pkt = packet.Packet(packet.MESSAGE, data=data)
35+
assert pkt.packet_type == packet.MESSAGE
36+
assert pkt.data == data
37+
assert not pkt.binary
38+
assert pkt.encode() == '4'
39+
40+
def test_decode_empty_text_packet(self):
41+
pkt = packet.Packet(encoded_packet=b'4')
42+
assert pkt.encode() == b'4'
43+
3244
def test_encode_binary_packet(self):
3345
pkt = packet.Packet(packet.MESSAGE, data=b'\x01\x02\x03')
3446
assert pkt.packet_type == packet.MESSAGE
@@ -50,17 +62,28 @@ def test_encode_binary_b64_packet(self):
5062
assert pkt.binary
5163
assert pkt.encode(b64=True) == 'bAQIDBA=='
5264

65+
def test_encode_empty_binary_packet(self):
66+
pkt = packet.Packet(packet.MESSAGE, data=b'')
67+
assert pkt.packet_type == packet.MESSAGE
68+
assert pkt.data == b''
69+
assert pkt.binary
70+
assert pkt.encode() == b''
71+
5372
def test_decode_binary_packet(self):
5473
pkt = packet.Packet(encoded_packet=b'\x04\x01\x02\x03')
55-
assert pkt.encode(), b'\x04\x01\x02\x03'
74+
assert pkt.encode() == b'\x04\x01\x02\x03'
5675

5776
def test_decode_binary_bytearray_packet(self):
5877
pkt = packet.Packet(encoded_packet=bytearray(b'\x04\x01\x02\x03'))
59-
assert pkt.encode(), b'\x04\x01\x02\x03'
78+
assert pkt.encode() == b'\x04\x01\x02\x03'
6079

6180
def test_decode_binary_b64_packet(self):
62-
pkt = packet.Packet(encoded_packet=b'b4AAEC')
63-
assert pkt.encode(), b'\x04\x01\x02\x03'
81+
pkt = packet.Packet(encoded_packet='bBAECAw==')
82+
assert pkt.encode() == b'\x04\x01\x02\x03'
83+
84+
def test_decode_empty_binary_packet(self):
85+
pkt = packet.Packet(encoded_packet=b'')
86+
assert pkt.encode() == b''
6487

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

0 commit comments

Comments
 (0)