diff --git a/pubsub/google/cloud/pubsub/_http.py b/pubsub/google/cloud/pubsub/_http.py index 6aef4edd6437..635c43bdaab5 100644 --- a/pubsub/google/cloud/pubsub/_http.py +++ b/pubsub/google/cloud/pubsub/_http.py @@ -15,6 +15,7 @@ """Create / interact with Google Cloud Pub/Sub connections.""" import base64 +import copy import functools import os @@ -203,9 +204,10 @@ def topic_publish(self, topic_path, messages): :rtype: list of string :returns: list of opaque IDs for published messages. """ - _transform_messages_base64(messages, _base64_unicode) + messages_to_send = copy.deepcopy(messages) + _transform_messages_base64(messages_to_send, _base64_unicode) conn = self._connection - data = {'messages': messages} + data = {'messages': messages_to_send} response = conn.api_request( method='POST', path='/%s:publish' % (topic_path,), data=data) return response['messageIds'] diff --git a/pubsub/unit_tests/test__http.py b/pubsub/unit_tests/test__http.py index 3401d0c5869e..3a0281a03eb2 100644 --- a/pubsub/unit_tests/test__http.py +++ b/pubsub/unit_tests/test__http.py @@ -284,10 +284,32 @@ def test_topic_publish_hit(self): msg_data = connection._called_with['data']['messages'][0]['data'] self.assertEqual(msg_data, B64_PAYLOAD) + def test_topic_publish_twice(self): + import base64 + + PAYLOAD = b'This is the message text' + B64_PAYLOAD = base64.b64encode(PAYLOAD).decode('ascii') + MESSAGE = {'data': PAYLOAD, 'attributes': {}} + RETURNED = {'messageIds': []} + connection = _Connection(RETURNED, RETURNED) + client = _Client(connection, self.PROJECT) + api = self._make_one(client) + + api.topic_publish(self.TOPIC_PATH, [MESSAGE]) + api.topic_publish(self.TOPIC_PATH, [MESSAGE]) + + messages = connection._called_with['data']['messages'] + self.assertEqual(len(messages), 1) + self.assertEqual(messages[0]['data'], B64_PAYLOAD) + def test_topic_publish_miss(self): + import base64 from google.cloud.exceptions import NotFound + PAYLOAD = b'This is the message text' + B64_PAYLOAD = base64.b64encode(PAYLOAD).decode('ascii') MESSAGE = {'data': PAYLOAD, 'attributes': {}} + B64MSG = {'data': B64_PAYLOAD, 'attributes': {}} connection = _Connection() client = _Client(connection, self.PROJECT) api = self._make_one(client) @@ -299,7 +321,7 @@ def test_topic_publish_miss(self): path = '/%s:publish' % (self.TOPIC_PATH,) self.assertEqual(connection._called_with['path'], path) self.assertEqual(connection._called_with['data'], - {'messages': [MESSAGE]}) + {'messages': [B64MSG]}) def test_topic_list_subscriptions_no_paging(self): from google.cloud.pubsub.topic import Topic