Skip to content

Commit

Permalink
Decode serializer response in tests for Erlang/OTP26
Browse files Browse the repository at this point in the history
OTP 26 changed the way maps are represented internally. The JSON
serializer tests were implicitly dependant on this order. This commit
decodes the response of the encoding and compares it to a map, allowing
the tests to pass regardless of the key order.
  • Loading branch information
Gazler committed Sep 13, 2023
1 parent 9d1d06c commit 3b1b767
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
29 changes: 24 additions & 5 deletions test/phoenix/socket/v1_json_serializer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ defmodule Phoenix.Socket.V1.JSONSerializerTest do
@serializer V1.JSONSerializer
@v1_msg_json "{\"event\":\"e\",\"payload\":\"m\",\"ref\":null,\"topic\":\"t\"}"
@v1_bad_json "[null,null,\"t\",\"e\",{\"m\":1}]"
@v1_reply_json "{\"event\":\"phx_reply\",\"payload\":{\"response\":null,\"status\":null},\"ref\":\"null\",\"topic\":\"t\"}"
@v1_fastlane_json "{\"event\":\"e\",\"payload\":\"m\",\"ref\":null,\"topic\":\"t\"}"

def encode!(serializer, msg) do
{:socket_push, :text, encoded} = serializer.encode!(msg)
Expand All @@ -24,12 +22,26 @@ defmodule Phoenix.Socket.V1.JSONSerializerTest do

test "encode!/1 encodes `Phoenix.Socket.Message` as JSON" do
msg = %Message{topic: "t", event: "e", payload: "m"}
assert encode!(@serializer, msg) == @v1_msg_json
encoded = encode!(@serializer, msg)

assert Jason.decode!(encoded) == %{
"event" => "e",
"payload" => "m",
"ref" => nil,
"topic" => "t"
}
end

test "encode!/1 encodes `Phoenix.Socket.Reply` as JSON" do
msg = %Reply{topic: "t", ref: "null"}
assert encode!(@serializer, msg) == @v1_reply_json
encoded = encode!(@serializer, msg)

assert Jason.decode!(encoded) == %{
"event" => "phx_reply",
"payload" => %{"response" => nil, "status" => nil},
"ref" => "null",
"topic" => "t"
}
end

test "decode!/2 decodes `Phoenix.Socket.Message` from JSON" do
Expand All @@ -47,6 +59,13 @@ defmodule Phoenix.Socket.V1.JSONSerializerTest do

test "fastlane!/1 encodes a broadcast into a message as JSON" do
msg = %Broadcast{topic: "t", event: "e", payload: "m"}
assert fastlane!(@serializer, msg) == @v1_fastlane_json
encoded = fastlane!(@serializer, msg)

assert Jason.decode!(encoded) == %{
"event" => "e",
"payload" => "m",
"ref" => nil,
"topic" => "t"
}
end
end
12 changes: 9 additions & 3 deletions test/phoenix/socket/v2_json_serializer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule Phoenix.Socket.V2.JSONSerializerTest do

@serializer V2.JSONSerializer
@v2_fastlane_json "[null,null,\"t\",\"e\",{\"m\":1}]"
@v2_reply_json "[null,null,\"t\",\"phx_reply\",{\"response\":{\"m\":1},\"status\":null}]"
@v2_msg_json "[null,null,\"t\",\"e\",{\"m\":1}]"

@client_push <<
Expand Down Expand Up @@ -101,7 +100,15 @@ defmodule Phoenix.Socket.V2.JSONSerializerTest do

test "encode!/1 encodes `Phoenix.Socket.Reply` as JSON" do
msg = %Reply{topic: "t", payload: %{m: 1}}
assert encode!(@serializer, msg) == @v2_reply_json
encoded = encode!(@serializer, msg)

assert Jason.decode!(encoded) == [
nil,
nil,
"t",
"phx_reply",
%{"response" => %{"m" => 1}, "status" => nil}
]
end

test "decode!/2 decodes `Phoenix.Socket.Message` from JSON" do
Expand Down Expand Up @@ -225,7 +232,6 @@ defmodule Phoenix.Socket.V2.JSONSerializerTest do
payload: {:binary, <<101, 102, 103>>}
})
end

end
end

Expand Down

0 comments on commit 3b1b767

Please sign in to comment.