Skip to content

Commit

Permalink
Handle messages without data
Browse files Browse the repository at this point in the history
Messages with payloadFormat: "NONE" may not contain the data attribute.
Note something may have changed on Google's side because I belive this
was previously handled by the nil check.

Closes #98
  • Loading branch information
mcrumm committed May 2, 2024
1 parent 6fe1356 commit 1677741
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/broadway_cloud_pub_sub/pull_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ defmodule BroadwayCloudPubSub.PullClient do
%{message | "data" => Base.decode64!(encoded_data)}
end

defp decode_message(message), do: message

defp headers(config) do
token = get_token(config)
[{"authorization", "Bearer #{token}"}, {"content-type", "application/json"}]
Expand Down
31 changes: 31 additions & 0 deletions test/broadway_cloud_pub_sub/pull_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ defmodule BroadwayCloudPubSub.PullClientTest do
}
"""

@no_payload_response """
{
"receivedMessages": [
{
"ackId": "1",
"message": {
"attributes": {
"payloadFormat": "NONE"
},
"messageId": "20240501001",
"publishTime": "2024-05-01T13:07:41.716Z"
}
}
]
}
"""

setup do
server = Bypass.open()
base_url = "http://localhost:#{server.port}"
Expand Down Expand Up @@ -155,6 +172,20 @@ defmodule BroadwayCloudPubSub.PullClientTest do
assert message.metadata.orderingKey == "key1"
end

test "returns a list of Broadway.Message when payloadFormat is NONE", %{
opts: base_opts,
server: server
} do
on_pubsub_request(server, fn _, _ ->
{:ok, @no_payload_response}
end)

{:ok, opts} = PullClient.init(base_opts)

assert [message] = PullClient.receive_messages(10, & &1, opts)
assert message.metadata.messageId == "20240501001"
end

test "returns a list of Broadway.Message with :data and :metadata set", %{
opts: base_opts
} do
Expand Down

0 comments on commit 1677741

Please sign in to comment.