diff options
author | Omer Katz <omer.drow@gmail.com> | 2019-03-13 17:47:58 +0200 |
---|---|---|
committer | Omer Katz <omer.drow@gmail.com> | 2019-03-13 17:47:58 +0200 |
commit | 4aa5dca4161328d53847893fb5f14c1d1aae2585 (patch) | |
tree | b0eefa8b42750b652d3d87aec0f37b320ff52521 | |
parent | 6c627986c312cdb7575ab0616a63f56cf40c0be7 (diff) | |
download | py-amqp-hypothesis.tar.gz |
Only arrays accept short strings.hypothesis
-rw-r--r-- | amqp/serialization.py | 29 | ||||
-rw-r--r-- | t/integration/test_rmq.py | 14 | ||||
-rw-r--r-- | tox.ini | 4 |
3 files changed, 35 insertions, 12 deletions
diff --git a/amqp/serialization.py b/amqp/serialization.py index d04f383..5296e37 100644 --- a/amqp/serialization.py +++ b/amqp/serialization.py @@ -12,6 +12,8 @@ import sys from datetime import datetime from decimal import Decimal from io import BytesIO +import logging +from pprint import pformat from .exceptions import FrameSyntaxError from .five import int_types, items, long_t, string, string_t @@ -20,6 +22,8 @@ from .spec import Basic from .utils import bytes_to_str as pstr_t from .utils import str_to_bytes +AMQP_LOGGER = logging.getLogger('amqp') + ftype_t = chr if sys.version_info[0] == 3 else None ILLEGAL_TABLE_TYPE = """\ @@ -250,6 +254,9 @@ def loads(format, buf, offset=0, else: raise FrameSyntaxError(ILLEGAL_TABLE_TYPE.format(p)) append(val) + if __debug__: + AMQP_LOGGER.debug("Deserialized the following stream:\n%s\nInto these values:\n%s", + buf, pformat(values)) return values, offset @@ -330,7 +337,12 @@ def dumps(format, values): write(pack('>Q', long_t(calendar.timegm(val.utctimetuple())))) _flushbits(bits, write) - return out.getvalue() + result = out.getvalue() + if __debug__: + AMQP_LOGGER.debug("Serialized the following values:\n%s\nInto:\n%s", + pformat(values), result) + + return result def _write_table(d, write, bits, pack=pack): @@ -360,7 +372,7 @@ def _write_array(l, write, bits, pack=pack): awrite = out.write for v in l: try: - _write_item(v, awrite, bits) + _write_item(v, awrite, bits, array_types=True) except ValueError: raise FrameSyntaxError( ILLEGAL_TABLE_TYPE_WITH_VALUE.format(type(v), v)) @@ -377,14 +389,19 @@ def _write_item(v, write, bits, pack=pack, string_t=string_t, bytes=bytes, string=string, bool=bool, float=float, int_types=int_types, Decimal=Decimal, datetime=datetime, dict=dict, list=list, tuple=tuple, - None_t=None): + None_t=None, array_types=False): if isinstance(v, string_t): v = v.encode('utf-8', 'surrogatepass') string_length = len(v) - if string_length > 255: - write(pack('>cI', b'S', string_length)) + + if array_types: + # Only arrays support short strings + if string_length > 255: + write(pack('>cI', b'S', string_length)) + else: + write(pack('>cB', b's', string_length)) else: - write(pack('>cB', b's', string_length)) + write(pack('>cI', b'S', string_length)) write(v) elif isinstance(v, bytes): write(pack('>cI', b'x', len(v))) diff --git a/t/integration/test_rmq.py b/t/integration/test_rmq.py index e5f0496..d7c4c3a 100644 --- a/t/integration/test_rmq.py +++ b/t/integration/test_rmq.py @@ -1,5 +1,6 @@ from __future__ import absolute_import, unicode_literals +import logging import os import pytest @@ -16,7 +17,8 @@ def connection(request): @pytest.mark.env('rabbitmq') -def test_connect(connection): +def test_connect(connection, caplog): + caplog.set_level(logging.DEBUG) connection.connect() connection.close() @@ -46,6 +48,8 @@ class test_rabbitmq_operations(): ) ) def test_publish_consume(self, publish_method, mandatory, immediate): + message = amqp.Message('Unittest') + method = getattr(self.channel, publish_method) callback = Mock() self.channel.queue_declare( queue='py-amqp-unittest', durable=False, exclusive=True @@ -56,8 +60,8 @@ class test_rabbitmq_operations(): # http://www.rabbitmq.com/blog/2012/11/19/breaking-things-with-rabbitmq-3-0/ if immediate and publish_method == "basic_publish_confirm": with pytest.raises(amqp.exceptions.AMQPNotImplementedError) as exc: - getattr(self.channel, publish_method)( - amqp.Message('Unittest'), + method( + message, routing_key='py-amqp-unittest', mandatory=mandatory, immediate=immediate @@ -69,8 +73,8 @@ class test_rabbitmq_operations(): return else: - getattr(self.channel, publish_method)( - amqp.Message('Unittest'), + method( + message, routing_key='py-amqp-unittest', mandatory=mandatory, immediate=immediate @@ -30,10 +30,12 @@ basepython = install_command = python -m pip --disable-pip-version-check install {opts} {packages} commands_pre = integration-rabbitmq: ./wait_for_rabbitmq.sh +commands_post = + integration-rabbitmq: ./rabbitmq_logs.sh docker = integration-rabbitmq: rabbitmq:alpine dockerenv = PYAMQP_INTEGRATION_INSTANCE=1 -passenv=CI +passenv = CI [testenv:apicheck] commands = |