Skip to content

Commit

Permalink
Handle messages without data (#99)
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 believe this was previously handled by the nil check.

The nil data case was originally fixed in #14 so I am leaving the explicit match for messages with nil data. The attribute match will cover the cases where no data attribute exists on the payload.
  • Loading branch information
mcrumm authored May 6, 2024
1 parent 6fe1356 commit d7392c8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/broadway_cloud_pub_sub/pull_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ defmodule BroadwayCloudPubSub.PullClient do
end
end

defp decode_message(%{"data" => nil} = message), do: message

defp decode_message(%{"data" => encoded_data} = message) do
defp decode_message(%{"data" => encoded_data} = message) when is_binary(encoded_data) do
%{message | "data" => Base.decode64!(encoded_data)}
end

defp decode_message(%{"data" => nil} = message), do: message
defp decode_message(%{"attributes" => %{"payloadFormat" => "NONE"}} = 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 d7392c8

Please sign in to comment.