Skip to content
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
6 changes: 4 additions & 2 deletions pubsub/google/cloud/pubsub/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Create / interact with Google Cloud Pub/Sub connections."""

import base64
import copy
import functools
import os

Expand Down Expand Up @@ -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)

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

_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']
Expand Down
24 changes: 23 additions & 1 deletion pubsub/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down