From 9fade02bf6a072423fb2efc8cd47d8977af8416b Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 7 Apr 2015 00:14:01 -0400 Subject: [PATCH] Fix regression3 failures: - Message payload must be bytes, base64ed, but then must be serializable to JSON. - Likewise, 'attributes' values must be serializable to JSON. --- gcloud/pubsub/test_topic.py | 6 +++--- gcloud/pubsub/topic.py | 3 ++- regression/pubsub.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/gcloud/pubsub/test_topic.py b/gcloud/pubsub/test_topic.py index fa1ce55a878c..849fa60ec0aa 100644 --- a/gcloud/pubsub/test_topic.py +++ b/gcloud/pubsub/test_topic.py @@ -126,12 +126,12 @@ def test_delete(self): self.assertEqual(req['method'], 'DELETE') self.assertEqual(req['path'], '/%s' % PATH) - def test_publish_single_wo_attrs(self): + def test_publish_single_bytes_wo_attrs(self): import base64 TOPIC_NAME = 'topic_name' PROJECT = 'PROJECT' PAYLOAD = b'This is the message text' - B64 = base64.b64encode(PAYLOAD) + B64 = base64.b64encode(PAYLOAD).decode('ascii') MSGID = 'DEADBEEF' MESSAGE = {'data': B64, 'attributes': {}} @@ -151,7 +151,7 @@ def test_publish_single_w_attrs(self): TOPIC_NAME = 'topic_name' PROJECT = 'PROJECT' PAYLOAD = b'This is the message text' - B64 = base64.b64encode(PAYLOAD) + B64 = base64.b64encode(PAYLOAD).decode('ascii') MSGID = 'DEADBEEF' MESSAGE = {'data': B64, 'attributes': {'attr1': 'value1', 'attr2': 'value2'}} diff --git a/gcloud/pubsub/topic.py b/gcloud/pubsub/topic.py index 9c3482cb4fa9..9243b895a536 100644 --- a/gcloud/pubsub/topic.py +++ b/gcloud/pubsub/topic.py @@ -113,7 +113,8 @@ def publish(self, message, **attrs): :rtype: str :returns: message ID assigned by the server to the published message """ - message_data = {'data': base64.b64encode(message), 'attributes': attrs} + message_b = base64.b64encode(message).decode('ascii') + message_data = {'data': message_b, 'attributes': attrs} data = {'messages': [message_data]} response = self.connection.api_request(method='POST', path='%s:publish' % self.path, diff --git a/regression/pubsub.py b/regression/pubsub.py index fb859dfc2d12..208f10f6d0fa 100644 --- a/regression/pubsub.py +++ b/regression/pubsub.py @@ -114,7 +114,7 @@ def test_message_pull_mode_e2e(self): self.to_delete.append(subscription) MESSAGE = b'MESSAGE' - EXTRA = b'EXTRA' + EXTRA = 'EXTRA' topic.publish(MESSAGE, extra=EXTRA) received = subscription.pull()