aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/handlers/ws_shutdown_reason_commands_h.erl38
-rw-r--r--test/ws_handler_SUITE.erl21
2 files changed, 58 insertions, 1 deletions
diff --git a/test/handlers/ws_shutdown_reason_commands_h.erl b/test/handlers/ws_shutdown_reason_commands_h.erl
new file mode 100644
index 0000000..90b435c
--- /dev/null
+++ b/test/handlers/ws_shutdown_reason_commands_h.erl
@@ -0,0 +1,38 @@
+%% This module sends the process pid to the test pid
+%% found in the x-test-pid header, then changes the
+%% shutdown reason and closes the connection normally.
+
+-module(ws_shutdown_reason_commands_h).
+-behavior(cowboy_websocket).
+
+-export([init/2]).
+-export([websocket_init/1]).
+-export([websocket_handle/2]).
+-export([websocket_info/2]).
+
+init(Req, RunOrHibernate) ->
+ TestPid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
+ {cowboy_websocket, Req, {TestPid, RunOrHibernate}}.
+
+websocket_init(State={TestPid, RunOrHibernate}) ->
+ TestPid ! {ws_pid, self()},
+ ShutdownReason = receive
+ {TestPid, SR} ->
+ SR
+ after 1000 ->
+ error(timeout)
+ end,
+ Commands = [
+ {shutdown_reason, ShutdownReason},
+ close
+ ],
+ case RunOrHibernate of
+ run -> {Commands, State};
+ hibernate -> {Commands, State, hibernate}
+ end.
+
+websocket_handle(_, State) ->
+ {[], State}.
+
+websocket_info(_, State) ->
+ {[], State}.
diff --git a/test/ws_handler_SUITE.erl b/test/ws_handler_SUITE.erl
index 67d50d2..872b152 100644
--- a/test/ws_handler_SUITE.erl
+++ b/test/ws_handler_SUITE.erl
@@ -52,7 +52,8 @@ init_dispatch(Name) ->
{"/info", ws_info_commands_h, RunOrHibernate},
{"/active", ws_active_commands_h, RunOrHibernate},
{"/deflate", ws_deflate_commands_h, RunOrHibernate},
- {"/set_options", ws_set_options_commands_h, RunOrHibernate}
+ {"/set_options", ws_set_options_commands_h, RunOrHibernate},
+ {"/shutdown_reason", ws_shutdown_reason_commands_h, RunOrHibernate}
]}]).
%% Support functions for testing using Gun.
@@ -286,3 +287,21 @@ websocket_set_options_idle_timeout(Config) ->
after 2000 ->
error(timeout)
end.
+
+websocket_shutdown_reason(Config) ->
+ doc("The command {shutdown_reason, any()} can be used to "
+ "change the shutdown reason of a Websocket connection."),
+ ConnPid = gun_open(Config),
+ StreamRef = gun:ws_upgrade(ConnPid, "/shutdown_reason", [
+ {<<"x-test-pid">>, pid_to_list(self())}
+ ]),
+ {upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef),
+ WsPid = receive {ws_pid, P} -> P after 1000 -> error(timeout) end,
+ MRef = monitor(process, WsPid),
+ WsPid ! {self(), {?MODULE, ?FUNCTION_NAME}},
+ receive
+ {'DOWN', MRef, process, WsPid, {shutdown, {?MODULE, ?FUNCTION_NAME}}} ->
+ ok
+ after 1000 ->
+ error(timeout)
+ end.