From f9b886e52493740f297a7091387f2e492d8f50f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20S=C3=B6derqvist?= Date: Wed, 23 Nov 2022 11:42:00 +0100 Subject: Add tests for ws subprotocol negotiation --- test/handlers/ws_subprotocol_h.erl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 test/handlers/ws_subprotocol_h.erl (limited to 'test/handlers') diff --git a/test/handlers/ws_subprotocol_h.erl b/test/handlers/ws_subprotocol_h.erl new file mode 100644 index 0000000..509d74d --- /dev/null +++ b/test/handlers/ws_subprotocol_h.erl @@ -0,0 +1,31 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(ws_subprotocol_h). + +-export([init/2]). +-export([websocket_handle/2]). +-export([websocket_info/2]). + +init(Req, State) -> + Protos = cowboy_req:parse_header(<<"sec-websocket-protocol">>, Req), + init_protos(Req, State, Protos). + +init_protos(Req, State, undefined) -> + {ok, cowboy_req:reply(400, #{}, <<"undefined">>, Req), State}; +init_protos(Req, State, []) -> + {ok, cowboy_req:reply(400, #{}, <<"nomatch">>, Req), State}; +init_protos(Req0, State, [<<"echo">> | _]) -> + Req = cowboy_req:set_resp_header(<<"sec-websocket-protocol">>, <<"echo">>, Req0), + {cowboy_websocket, Req, State}; +init_protos(Req, State, [_ | Protos]) -> + init_protos(Req, State, Protos). + +websocket_handle({text, Data}, State) -> + {[{text, Data}], State}; +websocket_handle({binary, Data}, State) -> + {[{binary, Data}], State}; +websocket_handle(_Frame, State) -> + {[], State}. + +websocket_info(_Info, State) -> + {[], State}. -- cgit v1.2.3