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

Add missing tests #12

Merged
merged 2 commits into from
Aug 14, 2023
Merged
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
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
Loading