Skip to content

Commit

Permalink
Add missing tests (#12)
Browse files Browse the repository at this point in the history
* Refactor init tests

* Add missing tests for handle_in, handle_control, handle_info
  • Loading branch information
mtrudel committed Aug 14, 2023
1 parent 19b431a commit a718884
Showing 1 changed file with 105 additions and 16 deletions.
121 changes: 105 additions & 16 deletions test/websock_adapter/cowboy_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -183,34 +183,20 @@ defmodule WebSockAdapterCowboyAdapterTest do
assert connection_closed_for_reading?(client)
end

defmodule InitRestartCloseWebSock do
defmodule InitCloseWithRestartWebSock do
use NoopWebSock
def init(_opts), do: {:stop, {:shutdown, :restart}, :init}
end

@tag capture_log: true
test "can close a connection by returning an {:shutdown, :restart} tuple", context do
client = tcp_client(context)
http1_handshake(client, InitRestartCloseWebSock)
http1_handshake(client, InitCloseWithRestartWebSock)

assert recv_connection_close_frame(client) == {:ok, <<1012::16>>}
assert connection_closed_for_reading?(client)
end

defmodule InitAbnormalCloseWithCodeWebSock do
use NoopWebSock
def init(_opts), do: {:stop, :abnormal, 5555, :init}
end

@tag capture_log: true
test "can close a connection by returning an abnormal stop tuple with a code", context do
client = tcp_client(context)
http1_handshake(client, InitAbnormalCloseWithCodeWebSock)

assert recv_connection_close_frame(client) == {:ok, <<5555::16>>}
assert connection_closed_for_reading?(client)
end

defmodule InitCloseWithCodeAndNilDetailWebSock do
use NoopWebSock
def init(_opts), do: {:stop, :normal, {5555, nil}, :init}
Expand Down Expand Up @@ -418,6 +404,37 @@ defmodule WebSockAdapterCowboyAdapterTest do
assert connection_closed_for_reading?(client)
end

defmodule HandleInCloseWithRestartWebSock do
use NoopWebSock
def handle_in(_data, state), do: {:stop, {:shutdown, :restart}, state}
end

@tag capture_log: true
test "can close a connection by returning an {:shutdown, :restart} tuple", context do
client = tcp_client(context)
http1_handshake(client, HandleInCloseWithRestartWebSock)

send_text_frame(client, "OK")

assert recv_connection_close_frame(client) == {:ok, <<1012::16>>}
assert connection_closed_for_reading?(client)
end

defmodule HandleInCloseWithCodeAndNilDetailWebSock do
use NoopWebSock
def handle_in(_data, state), do: {:stop, :normal, {5555, nil}, state}
end

test "can close a connection by returning a stop tuple with a code and nil detail", context do
client = tcp_client(context)
http1_handshake(client, HandleInCloseWithCodeAndNilDetailWebSock)

send_text_frame(client, "OK")

assert recv_connection_close_frame(client) == {:ok, <<5555::16>>}
assert connection_closed_for_reading?(client)
end

defmodule HandleInCloseWithCodeAndDetailWebSock do
use NoopWebSock
def handle_in(_data, state), do: {:stop, :normal, {5555, "BOOM"}, state}
Expand Down Expand Up @@ -645,6 +662,39 @@ defmodule WebSockAdapterCowboyAdapterTest do
assert connection_closed_for_reading?(client)
end

defmodule HandleControlCloseWithRestartWebSock do
use NoopWebSock
def handle_control(_data, state), do: {:stop, {:shutdown, :restart}, state}
end

@tag capture_log: true
test "can close a connection by returning an {:shutdown, :restart} tuple", context do
client = tcp_client(context)
http1_handshake(client, HandleControlCloseWithRestartWebSock)

send_ping_frame(client, "OK")
_ = recv_pong_frame(client)

assert recv_connection_close_frame(client) == {:ok, <<1012::16>>}
assert connection_closed_for_reading?(client)
end

defmodule HandleControlCloseWithCodeAndNilDetailWebSock do
use NoopWebSock
def handle_control(_data, state), do: {:stop, :normal, {5555, nil}, state}
end

test "can close a connection by returning a stop tuple with a code and nil detail", context do
client = tcp_client(context)
http1_handshake(client, HandleControlCloseWithCodeAndNilDetailWebSock)

send_ping_frame(client, "OK")
_ = recv_pong_frame(client)

assert recv_connection_close_frame(client) == {:ok, <<5555::16>>}
assert connection_closed_for_reading?(client)
end

defmodule HandleControlCloseWithCodeAndDetailWebSock do
use NoopWebSock
def handle_control(_data, state), do: {:stop, :normal, {5555, "BOOM"}, state}
Expand Down Expand Up @@ -831,6 +881,45 @@ defmodule WebSockAdapterCowboyAdapterTest do
assert connection_closed_for_reading?(client)
end

defmodule HandleInfoCloseWithRestartWebSock do
use NoopWebSock
def handle_in(_data, state), do: {:push, {:text, :erlang.pid_to_list(self())}, state}
def handle_info(_data, state), do: {:stop, {:shutdown, :restart}, state}
end

@tag capture_log: true
test "can close a connection by returning an {:shutdown, :restart} tuple", context do
client = tcp_client(context)
http1_handshake(client, HandleInfoCloseWithRestartWebSock)

send_text_frame(client, "whoami")
{:ok, pid} = recv_text_frame(client)
pid = pid |> String.to_charlist() |> :erlang.list_to_pid()
Process.send(pid, "OK", [])

assert recv_connection_close_frame(client) == {:ok, <<1012::16>>}
assert connection_closed_for_reading?(client)
end

defmodule HandleInfoCloseWithCodeAndNilDetailWebSock do
use NoopWebSock
def handle_in(_data, state), do: {:push, {:text, :erlang.pid_to_list(self())}, state}
def handle_info(_data, state), do: {:stop, :normal, {5555, nil}, state}
end

test "can close a connection by returning a stop tuple with a code and nil detail", context do
client = tcp_client(context)
http1_handshake(client, HandleInfoCloseWithCodeAndNilDetailWebSock)

send_text_frame(client, "whoami")
{:ok, pid} = recv_text_frame(client)
pid = pid |> String.to_charlist() |> :erlang.list_to_pid()
Process.send(pid, "OK", [])

assert recv_connection_close_frame(client) == {:ok, <<5555::16>>}
assert connection_closed_for_reading?(client)
end

defmodule HandleInfoCloseWithCodeAndDetailWebSock do
use NoopWebSock
def handle_in(_data, state), do: {:push, {:text, :erlang.pid_to_list(self())}, state}
Expand Down

0 comments on commit a718884

Please sign in to comment.