From 5f421f93bc36d73161f7aa8705da5ea3a3793807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sun, 28 May 2017 19:04:16 +0200 Subject: Introduce the req_filter Websocket option This option allows customizing the compacting of the Req object when using Websocket. By default it will keep most public fields excluding headers of course, since those can be large. --- test/handlers/ws_terminate_h.erl | 34 ++++++++++++++++++++++++++++++++++ test/ws_SUITE.erl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 test/handlers/ws_terminate_h.erl (limited to 'test') diff --git a/test/handlers/ws_terminate_h.erl b/test/handlers/ws_terminate_h.erl new file mode 100644 index 0000000..4621df6 --- /dev/null +++ b/test/handlers/ws_terminate_h.erl @@ -0,0 +1,34 @@ +%% This module sends a message with terminate arguments to the test case process. + +-module(ws_terminate_h). +-behavior(cowboy_websocket). + +-export([init/2]). +-export([websocket_init/1]). +-export([websocket_handle/2]). +-export([websocket_info/2]). +-export([terminate/3]). + +-record(state, { + pid +}). + +init(Req, _) -> + Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))), + Opts = case cowboy_req:qs(Req) of + <<"req_filter">> -> #{req_filter => fun(_) -> filtered end}; + _ -> #{} + end, + {cowboy_websocket, Req, #state{pid=Pid}, Opts}. + +websocket_init(State) -> + {ok, State}. + +websocket_handle(_, State) -> + {ok, State}. + +websocket_info(_, State) -> + {ok, State}. + +terminate(Reason, Req, #state{pid=Pid}) -> + Pid ! {terminate, Reason, Req}. diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl index 38c145e..b127c7a 100644 --- a/test/ws_SUITE.erl +++ b/test/ws_SUITE.erl @@ -79,6 +79,7 @@ init_dispatch() -> {text, <<"won't be received">>}]} ]}, {"/ws_subprotocol", ws_subprotocol, []}, + {"/terminate", ws_terminate_h, []}, {"/ws_timeout_hibernate", ws_timeout_hibernate, []}, {"/ws_timeout_cancel", ws_timeout_cancel, []} ]} @@ -355,6 +356,39 @@ ws_subprotocol(Config) -> {_, "foo"} = lists:keyfind("sec-websocket-protocol", 1, Headers), ok. +ws_terminate(Config) -> + doc("The Req object is kept in a more compact form by default."), + {ok, Socket, _} = do_handshake("/terminate", + "x-test-pid: " ++ pid_to_list(self()) ++ "\r\n", Config), + %% Send a close frame. + ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), + {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + %% Confirm terminate/3 was called with a compacted Req. + receive {terminate, _, Req} -> + true = maps:is_key(path, Req), + false = maps:is_key(headers, Req), + ok + after 1000 -> + error(timeout) + end. + +ws_terminate_fun(Config) -> + doc("A function can be given to filter the Req object."), + {ok, Socket, _} = do_handshake("/terminate?req_filter", + "x-test-pid: " ++ pid_to_list(self()) ++ "\r\n", Config), + %% Send a close frame. + ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), + {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + %% Confirm terminate/3 was called with a compacted Req. + receive {terminate, _, Req} -> + filtered = Req, + ok + after 1000 -> + error(timeout) + end. + ws_text_fragments(Config) -> doc("Client sends fragmented text frames."), {ok, Socket, _} = do_handshake("/ws_echo", Config), -- cgit v1.2.3