Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for type "x" #1011

Merged
merged 6 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pika/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ def encode_value(pieces, value):
pieces.append(struct.pack('>cI', b'S', len(value)))
pieces.append(value)
return 5 + len(value)

if isinstance(value, bytes):
pieces.append(struct.pack('>cI', b'x', len(value)))
pieces.append(value)
return 5 + len(value)

if isinstance(value, bool):
pieces.append(struct.pack('>cB', b't', int(value)))
return 2
Expand Down Expand Up @@ -281,6 +287,12 @@ def decode_value(encoded, offset):
pass
offset += length

elif kind == b'x':
length = struct.unpack_from('>I', encoded, offset)[0]
offset += 4
value = encoded[offset:offset + length]
offset += length

# Field Array
elif kind == b'A':
length = struct.unpack_from('>I', encoded, offset)[0]
Expand Down
25 changes: 20 additions & 5 deletions tests/unit/data_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import decimal
import unittest
from collections import OrderedDict
from pika.compat import PY2, PY3

from pika import data, exceptions
from pika.compat import long
Expand All @@ -15,7 +16,7 @@
class DataTests(unittest.TestCase):

FIELD_TBL_ENCODED = (
b'\x00\x00\x00\xcb'
b'\x00\x00\x00\xdc'
b'\x05arrayA\x00\x00\x00\x0fI\x00\x00\x00\x01I'
b'\x00\x00\x00\x02I\x00\x00\x00\x03'
b'\x07boolvalt\x01'
Expand All @@ -28,7 +29,10 @@ class DataTests(unittest.TestCase):
b'\x04nullV'
b'\x06strvalS\x00\x00\x00\x04Test'
b'\x0ctimestampvalT\x00\x00\x00\x00Ec)\x92'
b'\x07unicodeS\x00\x00\x00\x08utf8=\xe2\x9c\x93')
b'\x07unicodeS\x00\x00\x00\x08utf8=\xe2\x9c\x93'
)

FIELD_TBL_ENCODED += b'\x05bytesx\x00\x00\x00\x06foobar' if PY3 else b'\x05bytesS\x00\x00\x00\x06foobar'

FIELD_TBL_VALUE = OrderedDict(
[('array', [1, 2, 3]), ('boolval', True), ('decimal',
Expand All @@ -40,7 +44,18 @@ class DataTests(unittest.TestCase):
None),
('strval', 'Test'), ('timestampval',
datetime.datetime(2006, 11, 21, 16, 30,
10)), ('unicode', u'utf8=✓')])
10)), ('unicode', u'utf8=✓'),
('bytes', b'foobar'),
])

def test_decode_bytes(self):
input = (
b'\x00\x00\x00\x01'
b'\x05bytesx\x00\x00\x00\x06foobar'
)
result = data.decode_table(input, 0)
self.assertEqual(result, ({'bytes': b'foobar'}, 21))


def test_encode_table(self):
result = []
Expand All @@ -50,15 +65,15 @@ def test_encode_table(self):
def test_encode_table_bytes(self):
result = []
byte_count = data.encode_table(result, self.FIELD_TBL_VALUE)
self.assertEqual(byte_count, 207)
self.assertEqual(byte_count, 224)

def test_decode_table(self):
value, byte_count = data.decode_table(self.FIELD_TBL_ENCODED, 0)
self.assertDictEqual(value, self.FIELD_TBL_VALUE)

def test_decode_table_bytes(self):
value, byte_count = data.decode_table(self.FIELD_TBL_ENCODED, 0)
self.assertEqual(byte_count, 207)
self.assertEqual(byte_count, 224)

def test_encode_raises(self):
self.assertRaises(exceptions.UnsupportedAMQPFieldException,
Expand Down