aboutsummaryrefslogtreecommitdiffstats
path: root/test/handlers
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2016-08-15 19:21:38 +0200
committerLoïc Hoguin <[email protected]>2016-08-15 19:21:38 +0200
commit1d01d0fc06bae095e488f17a172246907eceea3f (patch)
treeaa4d6e5bdf5d59735cb2f7d70a054985f4f83f6d /test/handlers
parentaf88442610f9f0cc3ec620bbae9ad90f940a8176 (diff)
downloadcowboy-1d01d0fc06bae095e488f17a172246907eceea3f.tar.gz
cowboy-1d01d0fc06bae095e488f17a172246907eceea3f.tar.bz2
cowboy-1d01d0fc06bae095e488f17a172246907eceea3f.zip
Allow websocket_init/1 to reply/close/hibernate
Diffstat (limited to 'test/handlers')
-rw-r--r--test/handlers/ws_init_h.erl47
1 files changed, 47 insertions, 0 deletions
diff --git a/test/handlers/ws_init_h.erl b/test/handlers/ws_init_h.erl
new file mode 100644
index 0000000..08971ae
--- /dev/null
+++ b/test/handlers/ws_init_h.erl
@@ -0,0 +1,47 @@
+%% This module returns a different value in websocket_init/1 depending on the query string.
+
+-module(ws_init_h).
+-behavior(cowboy_websocket).
+
+-export([init/2]).
+-export([websocket_init/1]).
+-export([websocket_handle/2]).
+-export([websocket_info/2]).
+
+init(Req, _) ->
+ State = binary_to_atom(cowboy_req:qs(Req), latin1),
+ {cowboy_websocket, Req, State}.
+
+%% Sleep to make sure the HTTP response was sent.
+websocket_init(State) ->
+ timer:sleep(100),
+ do_websocket_init(State).
+
+do_websocket_init(State=ok) ->
+ {ok, State};
+do_websocket_init(State=ok_hibernate) ->
+ {ok, State, hibernate};
+do_websocket_init(State=reply) ->
+ {reply, {text, "Hello"}, State};
+do_websocket_init(State=reply_hibernate) ->
+ {reply, {text, "Hello"}, State, hibernate};
+do_websocket_init(State=reply_close) ->
+ {reply, close, State};
+do_websocket_init(State=reply_close_hibernate) ->
+ {reply, close, State, hibernate};
+do_websocket_init(State=reply_many) ->
+ {reply, [{text, "Hello"}, {binary, "World"}], State};
+do_websocket_init(State=reply_many_hibernate) ->
+ {reply, [{text, "Hello"}, {binary, "World"}], State, hibernate};
+do_websocket_init(State=reply_many_close) ->
+ {reply, [{text, "Hello"}, close], State};
+do_websocket_init(State=reply_many_close_hibernate) ->
+ {reply, [{text, "Hello"}, close], State, hibernate};
+do_websocket_init(State=stop) ->
+ {stop, State}.
+
+websocket_handle(_, State) ->
+ {ok, State}.
+
+websocket_info(_, State) ->
+ {ok, State}.