Skip to content
Closed
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
4 changes: 3 additions & 1 deletion lib/slipstream/serializer/phoenix_socket_v2_serializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ defmodule Slipstream.Serializer.PhoenixSocketV2Serializer do
defp decode_binary!(<<
@push::size(8),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current implementation for decode_binary!/1 is correct: this clause is decoding binary pushes and the clause below is for replies. I would need to check the Phoenix code again but I think only the reply binary has the ref_size and ref parts.

join_ref_size::size(8),
ref_size::size(8),
topic_size::size(8),
event_size::size(8),
join_ref::binary-size(join_ref_size),
ref::binary-size(ref_size),
topic::binary-size(topic_size),
event::binary-size(event_size),
data::binary
Expand All @@ -114,7 +116,7 @@ defmodule Slipstream.Serializer.PhoenixSocketV2Serializer do
topic: topic,
event: event,
payload: {:binary, data},
ref: nil,
ref: ref,
join_ref: join_ref
}
end
Expand Down
32 changes: 32 additions & 0 deletions test/slipstream/serializer/phoenix_socket_v2_serializer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,36 @@ defmodule Slipstream.Serializer.PhoenixSocketV2SerializerTest do
end)
end
end

describe "encode!/2 and decode!/2 round-trip" do
test "binary payload messages encode and decode correctly" do
original_message = %Message{
topic: "test:topic",
event: "test_event",
payload: {:binary, <<1, 2, 3, 4>>},
ref: "123",
join_ref: "456"
}

encoded = encode!(original_message, json_parser: Jason)
decoded = decode!(encoded, opcode: :binary, json_parser: Jason)

assert decoded == original_message
end

test "text/JSON payload messages encode and decode correctly" do
original_message = %Message{
topic: "test:topic",
event: "test_event",
payload: %{"foo" => "bar", "baz" => 123},
ref: "123",
join_ref: "456"
}

encoded = encode!(original_message, json_parser: Jason)
decoded = decode!(encoded, opcode: :text, json_parser: Jason)

assert decoded == original_message
end
end
end