Skip to content

Commit

Permalink
Stringify atoms in parameter keys for Channel tests (phoenixframework…
Browse files Browse the repository at this point in the history
…#1694)

This handles pushes as well as params passed to connect.
  • Loading branch information
tokafish authored and josevalim committed May 14, 2016
1 parent 2763163 commit 1d6fb28
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/phoenix/test/channel_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ defmodule Phoenix.ChannelTest do
if endpoint = Module.get_attribute(__CALLER__.module, :endpoint) do
quote do
Transport.connect(unquote(endpoint), unquote(handler), :channel_test,
unquote(__MODULE__), NoopSerializer, unquote(params))
unquote(__MODULE__), NoopSerializer, Phoenix.ChannelTest.__stringify__(unquote(params)))
end
else
raise "module attribute @endpoint not set for socket/2"
Expand Down Expand Up @@ -349,7 +349,7 @@ defmodule Phoenix.ChannelTest do
def push(socket, event, payload \\ %{}) do
ref = make_ref()
send(socket.channel_pid,
%Message{event: event, topic: socket.topic, ref: ref, payload: payload})
%Message{event: event, topic: socket.topic, ref: ref, payload: __stringify__(payload)})
ref
end

Expand Down Expand Up @@ -537,4 +537,13 @@ defmodule Phoenix.ChannelTest do
_ -> raise "no channel found for topic #{inspect topic} in #{inspect socket.handler}"
end
end

@doc false
def __stringify__(%{} = params),
do: Enum.into(params, %{}, &stringify_kv/1)
def __stringify__(other),
do: other

defp stringify_kv({k, v}),
do: {to_string(k), __stringify__(v)}
end
11 changes: 11 additions & 0 deletions test/phoenix/test/channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,17 @@ defmodule Phoenix.Test.ChannelTest do
assert_broadcast "broadcast", %{"foo" => "bar"}
end

test "pushes atom parameter keys as strings" do
{:ok, _, socket} = join(socket(), Channel, "foo:ok")

ref = push socket, "reply", %{req: %{parameter: 1}}
assert_reply ref, :ok, %{"resp" => %{"parameter" => 1}}
end

test "connects with atom parameter keys as strings" do
:error = connect(UserSocket, %{reject: true})
end

## handle_out

test "push broadcasts by default" do
Expand Down

0 comments on commit 1d6fb28

Please sign in to comment.