Skip to content

Commit

Permalink
fix: update encoder to utf-8 (#81)
Browse files Browse the repository at this point in the history
* fix: update encoder to utf-8

* refactor: update links in index.rst

* test: add test case for consume_message
  • Loading branch information
kyleaquino authored Sep 10, 2024
1 parent 0b8dffe commit 5857829
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ User Guide
Testing <testing>

.. _pika: https://pypi.org/project/pika/
.. _default initialization settings: https://hub.docker.com/_/rabbitmq)
.. _default initialization settings: https://hub.docker.com/_/rabbitmq
.. _PyPI: https://pypi.org/project/PyRMQ/
.. _here: https://www.rabbitmq.com/priority.html
.. _RabbitMQ does not remove expired messages that aren't at the head of the queue: https://www.rabbitmq.com/ttl.html#per-message-ttl-caveats
.. _RabbitMQ does not remove expired messages that aren't at the head of the queue: https://www.rabbitmq.com/ttl.html
.. _e2e bindings: https://www.rabbitmq.com/e2e.html
2 changes: 1 addition & 1 deletion pyrmq/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def _consume_message(self, channel, method, properties, data: dict) -> None:
"""

if isinstance(data, bytes):
data = data.decode("ascii")
data = data.decode("utf-8", errors="replace")

data = json.loads(data)

Expand Down
29 changes: 24 additions & 5 deletions pyrmq/tests/test_consumer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""
Python with RabbitMQ—simplified so you won't have to.
Python with RabbitMQ—simplified so you won't have to.
:copyright: 2020-Present by Alexandre Gerona.
:license: MIT, see LICENSE for more details.
:copyright: 2020-Present by Alexandre Gerona.
:license: MIT, see LICENSE for more details.
Full documentation is available at https://pyrmq.readthedocs.io
Full documentation is available at https://pyrmq.readthedocs.io
"""

import logging
from random import randint
from time import sleep
from unittest.mock import patch
from unittest.mock import Mock, patch

import pytest
from pika.exceptions import AMQPConnectionError
Expand Down Expand Up @@ -401,3 +402,21 @@ def second_callback(data: dict, **kwargs):
second_consumer.start()
assert_consumed_message(second_response, {"count": 1})
second_consumer.close()


def should_consume_message_utf8_decoding(publisher_session: Publisher):
consumer = Consumer(
exchange_name=publisher_session.exchange_name,
queue_name=publisher_session.queue_name,
routing_key=publisher_session.routing_key,
callback=Mock(),
)

data = b'{"key": "value\xe2\x80\xac"}'

mock_channel = Mock()
mock_method = Mock()
mock_method.delivery_tag = "test_tag"
mock_properties = Mock()

consumer._consume_message(mock_channel, mock_method, mock_properties, data)

0 comments on commit 5857829

Please sign in to comment.