Skip to content

Commit

Permalink
Merge branch 'Kurlov-fix-namespace-hyphens'
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jan 23, 2017
2 parents 3c86825 + 4f35e67 commit 923ded0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
13 changes: 11 additions & 2 deletions socketio/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
class Packet(object):
"""Socket.IO packet."""

# the format of the Socket.IO packet is as follows:
#
# type: 1 byte, values 0-6
# num_attachments: ASCII encoded, only if num_attachments != 0
# '-': only if num_attachments != 0
# namespace: only if namespace != '/'
# ',': only if namespace and one of id and data are defined in this packet
# id: ASCII encoded, only if id is not None
# data: JSON dump of data payload

json = _json

def __init__(self, packet_type=EVENT, data=None, namespace=None, id=None,
Expand Down Expand Up @@ -79,9 +89,8 @@ def decode(self, encoded_packet):
self.data = None
ep = ep[1:]
dash = (ep + '-').find('-')
comma = (ep + ',').find(',')
attachment_count = 0
if dash < comma:
if ep[0:dash].isdigit():
attachment_count = int(ep[0:dash])
ep = ep[dash + 1:]
if ep and ep[0:1] == '/':
Expand Down
11 changes: 11 additions & 0 deletions tests/test_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ def test_decode_namespace_no_data(self):
self.assertEqual(pkt.namespace, '/bar')
self.assertEqual(pkt.encode(), '2/bar')

def test_encode_namespace_with_hyphens(self):
pkt = packet.Packet(packet_type=packet.EVENT,
data=[six.text_type('foo')], namespace='/b-a-r')
self.assertEqual(pkt.namespace, '/b-a-r')
self.assertEqual(pkt.encode(), '2/b-a-r,["foo"]')

def test_decode_namespace_with_hyphens(self):
pkt = packet.Packet(encoded_packet='2/b-a-r,["foo"]')
self.assertEqual(pkt.namespace, '/b-a-r')
self.assertEqual(pkt.encode(), '2/b-a-r,["foo"]')

def test_encode_id(self):
pkt = packet.Packet(packet_type=packet.EVENT,
data=[six.text_type('foo')], id=123)
Expand Down

0 comments on commit 923ded0

Please sign in to comment.