aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2024-01-16 12:20:38 +0100
committerLoïc Hoguin <[email protected]>2024-01-16 12:20:38 +0100
commit08a8d7b9e981002545bf77af367c734d9ea162ab (patch)
tree7f1d03b94f57a787ffaf9f7bfbb87f2bdd7a4216
parent920adb9b8258bf60f167f7ef86d33e66e467fcd7 (diff)
downloadcowboy-08a8d7b9e981002545bf77af367c734d9ea162ab.tar.gz
cowboy-08a8d7b9e981002545bf77af367c734d9ea162ab.tar.bz2
cowboy-08a8d7b9e981002545bf77af367c734d9ea162ab.zip
Confirm Websocket pong frames are received by handlers
-rw-r--r--test/handlers/ws_ping_h.erl23
-rw-r--r--test/ws_SUITE.erl14
2 files changed, 36 insertions, 1 deletions
diff --git a/test/handlers/ws_ping_h.erl b/test/handlers/ws_ping_h.erl
new file mode 100644
index 0000000..a5848fe
--- /dev/null
+++ b/test/handlers/ws_ping_h.erl
@@ -0,0 +1,23 @@
+%% This module sends an empty ping to the client and
+%% waits for a pong before sending a text frame. It
+%% is used to confirm server-initiated pings work.
+
+-module(ws_ping_h).
+-behavior(cowboy_websocket).
+
+-export([init/2]).
+-export([websocket_init/1]).
+-export([websocket_handle/2]).
+-export([websocket_info/2]).
+
+init(Req, _) ->
+ {cowboy_websocket, Req, undefined}.
+
+websocket_init(State) ->
+ {[{ping, <<>>}], State}.
+
+websocket_handle(pong, State) ->
+ {[{text, <<"OK!!">>}], State}.
+
+websocket_info(_, State) ->
+ {[], State}.
diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl
index 126d8c6..b0d590e 100644
--- a/test/ws_SUITE.erl
+++ b/test/ws_SUITE.erl
@@ -68,7 +68,8 @@ init_dispatch() ->
{"/ws_timeout_cancel", ws_timeout_cancel, []},
{"/ws_max_frame_size", ws_max_frame_size, []},
{"/ws_deflate_opts", ws_deflate_opts_h, []},
- {"/ws_dont_validate_utf8", ws_dont_validate_utf8_h, []}
+ {"/ws_dont_validate_utf8", ws_dont_validate_utf8_h, []},
+ {"/ws_ping", ws_ping_h, []}
]}
]).
@@ -472,6 +473,17 @@ ws_max_frame_size_intermediate_fragment_close(Config) ->
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
ok.
+ws_ping(Config) ->
+ doc("Server initiated pings can receive a pong in response."),
+ {ok, Socket, _} = do_handshake("/ws_ping", Config),
+ %% Receive a server-sent ping.
+ {ok, << 1:1, 0:3, 9:4, 0:1, 0:7 >>} = gen_tcp:recv(Socket, 0, 6000),
+ %% Send a pong back with a 0 mask.
+ ok = gen_tcp:send(Socket, << 1:1, 0:3, 10:4, 1:1, 0:7, 0:32 >>),
+ %% Receive a text frame as a result.
+ {ok, << 1:1, 0:3, 1:4, 0:1, 4:7, "OK!!" >>} = gen_tcp:recv(Socket, 0, 6000),
+ ok.
+
ws_send_close(Config) ->
doc("Server-initiated close frame ends the connection."),
{ok, Socket, _} = do_handshake("/ws_send_close", Config),