Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle messages without data #99

Merged
merged 3 commits into from
May 6, 2024
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
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
Loading