| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
-module(ws_handler_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
-import(cowboy_test, [gun_open/1]). |
| 22 |
-import(cowboy_test, [gun_open/2]). |
| 23 |
-import(cowboy_test, [gun_down/1]). |
| 24 |
|
| 25 |
%% ct. |
| 26 |
|
| 27 |
all() -> |
| 28 |
[ |
| 29 |
{group, h1}, |
| 30 |
{group, h1_hibernate}, |
| 31 |
{group, h2}, |
| 32 |
{group, h2_relay} |
| 33 |
]. |
| 34 |
|
| 35 |
%% @todo Test against HTTP/2 too. |
| 36 |
groups() -> |
| 37 |
AllTests = ct_helper:all(?MODULE), |
| 38 |
[ |
| 39 |
{h1, [parallel], AllTests}, |
| 40 |
{h1_hibernate, [parallel], AllTests}, |
| 41 |
%% The websocket_deflate_false test isn't compatible with HTTP/2. |
| 42 |
{h2, [parallel], AllTests -- [websocket_deflate_false]}, |
| 43 |
{h2_relay, [parallel], AllTests -- [websocket_deflate_false]} |
| 44 |
]. |
| 45 |
|
| 46 |
init_per_group(Name, Config) |
| 47 |
when Name =:= h1; Name =:= h1_hibernate -> |
| 48 |
cowboy_test:init_http(Name, #{ |
| 49 |
env => #{dispatch => init_dispatch(Name)} |
| 50 |
}, Config); |
| 51 |
init_per_group(Name, Config) |
| 52 |
when Name =:= h2; Name =:= h2_relay -> |
| 53 |
cowboy_test:init_http2(Name, #{ |
| 54 |
enable_connect_protocol => true, |
| 55 |
env => #{dispatch => init_dispatch(Name)} |
| 56 |
}, Config). |
| 57 |
|
| 58 |
end_per_group(Name, _) -> |
| 59 |
cowboy:stop_listener(Name). |
| 60 |
|
| 61 |
%% Dispatch configuration. |
| 62 |
|
| 63 |
init_dispatch(Name) -> |
| 64 |
InitialState = case Name of |
| 65 |
h1_hibernate -> #{run_or_hibernate => hibernate}; |
| 66 |
h2_relay -> #{run_or_hibernate => run, data_delivery => relay}; |
| 67 |
_ -> #{run_or_hibernate => run} |
| 68 |
end, |
| 69 |
cowboy_router:compile([{'_', [ |
| 70 |
{"/init", ws_init_commands_h, InitialState}, |
| 71 |
{"/handle", ws_handle_commands_h, InitialState}, |
| 72 |
{"/info", ws_info_commands_h, InitialState}, |
| 73 |
{"/trap_exit", ws_init_h, InitialState}, |
| 74 |
{"/active", ws_active_commands_h, InitialState}, |
| 75 |
{"/deflate", ws_deflate_commands_h, InitialState}, |
| 76 |
{"/set_options", ws_set_options_commands_h, InitialState}, |
| 77 |
{"/shutdown_reason", ws_shutdown_reason_commands_h, InitialState}, |
| 78 |
{"/terminate", ws_terminate_h, InitialState} |
| 79 |
]}]). |
| 80 |
|
| 81 |
%% Support functions for testing using Gun. |
| 82 |
|
| 83 |
gun_open_ws(Config, Path, Commands) -> |
| 84 |
ConnPid = gun_open(Config, #{http2_opts => #{notify_settings_changed => true}}), |
| 85 |
do_await_enable_connect_protocol(config(protocol, Config), ConnPid), |
| 86 |
StreamRef = gun:ws_upgrade(ConnPid, Path, [ |
| 87 |
{<<"x-commands">>, base64:encode(term_to_binary(Commands))} |
| 88 |
]), |
| 89 |
receive |
| 90 |
{gun_upgrade, ConnPid, StreamRef, [<<"websocket">>], _} -> |
| 91 |
{ok, ConnPid, StreamRef}; |
| 92 |
{gun_response, ConnPid, _, _, Status, Headers} -> |
| 93 |
exit({ws_upgrade_failed, Status, Headers}); |
| 94 |
{gun_error, ConnPid, StreamRef, Reason} -> |
| 95 |
exit({ws_upgrade_failed, Reason}) |
| 96 |
after 1000 -> |
| 97 |
error(timeout) |
| 98 |
end. |
| 99 |
|
| 100 |
do_await_enable_connect_protocol(http, _) -> |
| 101 |
ok; |
| 102 |
do_await_enable_connect_protocol(http2, ConnPid) -> |
| 103 |
{notify, settings_changed, #{enable_connect_protocol := true}} |
| 104 |
= gun:await(ConnPid, undefined), %% @todo Maybe have a gun:await/1? |
| 105 |
ok. |
| 106 |
|
| 107 |
receive_ws(ConnPid, StreamRef) -> |
| 108 |
receive |
| 109 |
{gun_ws, ConnPid, StreamRef, Frame} -> |
| 110 |
{ok, Frame} |
| 111 |
after 1000 -> |
| 112 |
{error, timeout} |
| 113 |
end. |
| 114 |
|
| 115 |
ensure_handle_is_called(ConnPid, StreamRef, "/handle") -> |
| 116 |
gun:ws_send(ConnPid, StreamRef, {text, <<"Necessary to trigger websocket_handle/2.">>}); |
| 117 |
ensure_handle_is_called(_, _, _) -> |
| 118 |
ok. |
| 119 |
|
| 120 |
do_receive(Tag) -> |
| 121 |
receive |
| 122 |
Msg when element(1, Msg) =:= Tag -> |
| 123 |
Msg |
| 124 |
after 1000 -> |
| 125 |
ct:pal("do_receive(~p): ~p", [Tag, process_info(self(), messages)]), |
| 126 |
error(timeout) |
| 127 |
end. |
| 128 |
|
| 129 |
%% Tests. |
| 130 |
|
| 131 |
websocket_init_nothing(Config) -> |
| 132 |
doc("Nothing happens when websocket_init/1 returns no commands."), |
| 133 |
do_nothing(Config, "/init"). |
| 134 |
|
| 135 |
websocket_handle_nothing(Config) -> |
| 136 |
doc("Nothing happens when websocket_handle/2 returns no commands."), |
| 137 |
do_nothing(Config, "/handle"). |
| 138 |
|
| 139 |
websocket_info_nothing(Config) -> |
| 140 |
doc("Nothing happens when websocket_info/2 returns no commands."), |
| 141 |
do_nothing(Config, "/info"). |
| 142 |
|
| 143 |
do_nothing(Config, Path) -> |
| 144 |
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, []), |
| 145 |
ensure_handle_is_called(ConnPid, StreamRef, Path), |
| 146 |
{error, timeout} = receive_ws(ConnPid, StreamRef), |
| 147 |
gun:close(ConnPid). |
| 148 |
|
| 149 |
websocket_init_invalid(Config) -> |
| 150 |
doc("The connection must be closed when websocket_init/1 returns an invalid command."), |
| 151 |
do_invalid(Config, "/init"). |
| 152 |
|
| 153 |
websocket_handle_invalid(Config) -> |
| 154 |
doc("The connection must be closed when websocket_handle/2 returns an invalid command."), |
| 155 |
do_invalid(Config, "/init"). |
| 156 |
|
| 157 |
websocket_info_invalid(Config) -> |
| 158 |
doc("The connection must be closed when websocket_info/2 returns an invalid command."), |
| 159 |
do_invalid(Config, "/info"). |
| 160 |
|
| 161 |
do_invalid(Config, Path) -> |
| 162 |
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, bad), |
| 163 |
ensure_handle_is_called(ConnPid, StreamRef, Path), |
| 164 |
case config(protocol, Config) of |
| 165 |
%% HTTP/1.1 closes the connection. |
| 166 |
http -> gun_down(ConnPid); |
| 167 |
%% HTTP/2 terminates the stream. |
| 168 |
http2 -> |
| 169 |
receive {gun_error, ConnPid, StreamRef, {stream_error, internal_error, _}} -> ok |
| 170 |
after 500 -> error(timeout) end |
| 171 |
end. |
| 172 |
|
| 173 |
websocket_init_one_frame(Config) -> |
| 174 |
doc("A single frame is received when websocket_init/1 returns it as a command."), |
| 175 |
do_one_frame(Config, "/init"). |
| 176 |
|
| 177 |
websocket_handle_one_frame(Config) -> |
| 178 |
doc("A single frame is received when websocket_handle/2 returns it as a command."), |
| 179 |
do_one_frame(Config, "/handle"). |
| 180 |
|
| 181 |
websocket_info_one_frame(Config) -> |
| 182 |
doc("A single frame is received when websocket_info/2 returns it as a command."), |
| 183 |
do_one_frame(Config, "/info"). |
| 184 |
|
| 185 |
do_one_frame(Config, Path) -> |
| 186 |
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [ |
| 187 |
{text, <<"One frame!">>} |
| 188 |
]), |
| 189 |
ensure_handle_is_called(ConnPid, StreamRef, Path), |
| 190 |
{ok, {text, <<"One frame!">>}} = receive_ws(ConnPid, StreamRef), |
| 191 |
gun:close(ConnPid). |
| 192 |
|
| 193 |
websocket_init_many_frames(Config) -> |
| 194 |
doc("Multiple frames are received when websocket_init/1 returns them as commands."), |
| 195 |
do_many_frames(Config, "/init"). |
| 196 |
|
| 197 |
websocket_handle_many_frames(Config) -> |
| 198 |
doc("Multiple frames are received when websocket_handle/2 returns them as commands."), |
| 199 |
do_many_frames(Config, "/handle"). |
| 200 |
|
| 201 |
websocket_info_many_frames(Config) -> |
| 202 |
doc("Multiple frames are received when websocket_info/2 returns them as commands."), |
| 203 |
do_many_frames(Config, "/info"). |
| 204 |
|
| 205 |
do_many_frames(Config, Path) -> |
| 206 |
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [ |
| 207 |
{text, <<"One frame!">>}, |
| 208 |
{binary, <<"Two frames!">>} |
| 209 |
]), |
| 210 |
ensure_handle_is_called(ConnPid, StreamRef, Path), |
| 211 |
{ok, {text, <<"One frame!">>}} = receive_ws(ConnPid, StreamRef), |
| 212 |
{ok, {binary, <<"Two frames!">>}} = receive_ws(ConnPid, StreamRef), |
| 213 |
gun:close(ConnPid). |
| 214 |
|
| 215 |
websocket_init_close_frame(Config) -> |
| 216 |
doc("A single close frame is received when websocket_init/1 returns it as a command."), |
| 217 |
do_close_frame(Config, "/init"). |
| 218 |
|
| 219 |
websocket_handle_close_frame(Config) -> |
| 220 |
doc("A single close frame is received when websocket_handle/2 returns it as a command."), |
| 221 |
do_close_frame(Config, "/handle"). |
| 222 |
|
| 223 |
websocket_info_close_frame(Config) -> |
| 224 |
doc("A single close frame is received when websocket_info/2 returns it as a command."), |
| 225 |
do_close_frame(Config, "/info"). |
| 226 |
|
| 227 |
do_close_frame(Config, Path) -> |
| 228 |
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [close]), |
| 229 |
ensure_handle_is_called(ConnPid, StreamRef, Path), |
| 230 |
{ok, close} = receive_ws(ConnPid, StreamRef), |
| 231 |
gun_down(ConnPid). |
| 232 |
|
| 233 |
websocket_init_many_frames_then_close_frame(Config) -> |
| 234 |
doc("Multiple frames are received followed by a close frame " |
| 235 |
"when websocket_init/1 returns them as commands."), |
| 236 |
do_many_frames_then_close_frame(Config, "/init"). |
| 237 |
|
| 238 |
websocket_handle_many_frames_then_close_frame(Config) -> |
| 239 |
doc("Multiple frames are received followed by a close frame " |
| 240 |
"when websocket_handle/2 returns them as commands."), |
| 241 |
do_many_frames_then_close_frame(Config, "/handle"). |
| 242 |
|
| 243 |
websocket_info_many_frames_then_close_frame(Config) -> |
| 244 |
doc("Multiple frames are received followed by a close frame " |
| 245 |
"when websocket_info/2 returns them as commands."), |
| 246 |
do_many_frames_then_close_frame(Config, "/info"). |
| 247 |
|
| 248 |
do_many_frames_then_close_frame(Config, Path) -> |
| 249 |
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [ |
| 250 |
{text, <<"One frame!">>}, |
| 251 |
{binary, <<"Two frames!">>}, |
| 252 |
close |
| 253 |
]), |
| 254 |
ensure_handle_is_called(ConnPid, StreamRef, Path), |
| 255 |
{ok, {text, <<"One frame!">>}} = receive_ws(ConnPid, StreamRef), |
| 256 |
{ok, {binary, <<"Two frames!">>}} = receive_ws(ConnPid, StreamRef), |
| 257 |
{ok, close} = receive_ws(ConnPid, StreamRef), |
| 258 |
gun_down(ConnPid). |
| 259 |
|
| 260 |
websocket_init_trap_exit_false(Config) -> |
| 261 |
doc("The trap_exit process flag must be set back to false before " |
| 262 |
"the connection is taken over by Websocket."), |
| 263 |
{ok, ConnPid, StreamRef} = gun_open_ws(Config, "/trap_exit?reply_trap_exit", []), |
| 264 |
{ok, {text, <<"trap_exit: false">>}} = receive_ws(ConnPid, StreamRef), |
| 265 |
ok. |
| 266 |
|
| 267 |
websocket_active_false(Config) -> |
| 268 |
doc("The {active, false} command stops receiving data from the socket. " |
| 269 |
"The {active, true} command reenables it."), |
| 270 |
{ok, ConnPid, StreamRef} = gun_open_ws(Config, "/active", []), |
| 271 |
%% We must exhaust the HTTP/2 flow control window |
| 272 |
%% otherwise the frame will be received even if active mode is disabled. |
| 273 |
gun:ws_send(ConnPid, StreamRef, {binary, <<0:100000/unit:8>>}), |
| 274 |
gun:ws_send(ConnPid, StreamRef, {text, <<"Not received until the handler enables active again.">>}), |
| 275 |
{error, timeout} = receive_ws(ConnPid, StreamRef), |
| 276 |
{ok, {binary, _}} = receive_ws(ConnPid, StreamRef), |
| 277 |
{ok, {text, <<"Not received until the handler enables active again.">>}} |
| 278 |
= receive_ws(ConnPid, StreamRef), |
| 279 |
gun:close(ConnPid). |
| 280 |
|
| 281 |
websocket_deflate_false(Config) -> |
| 282 |
doc("The {deflate, false} command temporarily disables compression. " |
| 283 |
"The {deflate, true} command reenables it."), |
| 284 |
%% We disable context takeover so that the compressed data |
| 285 |
%% does not change across all frames. |
| 286 |
{ok, Socket, Headers} = ws_SUITE:do_handshake("/deflate", |
| 287 |
"Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover\r\n", Config), |
| 288 |
{_, "permessage-deflate; server_no_context_takeover"} |
| 289 |
= lists:keyfind("sec-websocket-extensions", 1, Headers), |
| 290 |
%% The handler receives a compressed "Hello" frame and |
| 291 |
%% sends back a compressed or uncompressed echo intermittently. |
| 292 |
Mask = 16#11223344, |
| 293 |
CompressedHello = <<242, 72, 205, 201, 201, 7, 0>>, |
| 294 |
MaskedHello = ws_SUITE:do_mask(CompressedHello, Mask, <<>>), |
| 295 |
%% First echo is compressed. |
| 296 |
ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>), |
| 297 |
{ok, <<1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary>>} = gen_tcp:recv(Socket, 0, 6000), |
| 298 |
%% Second echo is not compressed when it is received back. |
| 299 |
ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>), |
| 300 |
{ok, <<1:1, 0:3, 1:4, 0:1, 5:7, "Hello">>} = gen_tcp:recv(Socket, 0, 6000), |
| 301 |
%% Third echo is compressed again. |
| 302 |
ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>), |
| 303 |
{ok, <<1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary>>} = gen_tcp:recv(Socket, 0, 6000), |
| 304 |
%% Client-initiated close. |
| 305 |
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>), |
| 306 |
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), |
| 307 |
{error, closed} = gen_tcp:recv(Socket, 0, 6000), |
| 308 |
ok. |
| 309 |
|
| 310 |
websocket_deflate_ignore_if_not_negotiated(Config) -> |
| 311 |
doc("The {deflate, boolean()} commands are ignored " |
| 312 |
"when compression was not negotiated."), |
| 313 |
{ok, ConnPid, StreamRef} = gun_open_ws(Config, "/deflate", []), |
| 314 |
_ = [begin |
| 315 |
gun:ws_send(ConnPid, StreamRef, {text, <<"Hello.">>}), |
| 316 |
{ok, {text, <<"Hello.">>}} = receive_ws(ConnPid, StreamRef) |
| 317 |
end || _ <- lists:seq(1, 10)], |
| 318 |
gun:close(ConnPid). |
| 319 |
|
| 320 |
websocket_set_options_idle_timeout(Config) -> |
| 321 |
doc("The idle_timeout option can be modified using the " |
| 322 |
"command {set_options, Opts} at runtime."), |
| 323 |
ConnPid = gun_open(Config, #{http2_opts => #{notify_settings_changed => true}}), |
| 324 |
do_await_enable_connect_protocol(config(protocol, Config), ConnPid), |
| 325 |
StreamRef = gun:ws_upgrade(ConnPid, "/set_options"), |
| 326 |
receive |
| 327 |
{gun_upgrade, ConnPid, StreamRef, [<<"websocket">>], _} -> |
| 328 |
ok; |
| 329 |
{gun_response, ConnPid, _, _, Status, Headers} -> |
| 330 |
exit({ws_upgrade_failed, Status, Headers}); |
| 331 |
{gun_error, ConnPid, StreamRef, Reason} -> |
| 332 |
exit({ws_upgrade_failed, Reason}) |
| 333 |
after 1000 -> |
| 334 |
error(timeout) |
| 335 |
end, |
| 336 |
%% We don't send anything for a short while and confirm |
| 337 |
%% that idle_timeout does not trigger. |
| 338 |
{error, timeout} = gun:await(ConnPid, StreamRef, 2000), |
| 339 |
%% Trigger the change in idle_timeout and confirm that |
| 340 |
%% the connection gets closed soon after. |
| 341 |
gun:ws_send(ConnPid, StreamRef, {text, <<"idle_timeout_short">>}), |
| 342 |
receive |
| 343 |
{gun_down, ConnPid, _, _, _} -> |
| 344 |
ok |
| 345 |
after 2000 -> |
| 346 |
error(timeout) |
| 347 |
end. |
| 348 |
|
| 349 |
websocket_set_options_max_frame_size(Config) -> |
| 350 |
doc("The max_frame_size option can be modified using the " |
| 351 |
"command {set_options, Opts} at runtime."), |
| 352 |
ConnPid = gun_open(Config, #{http2_opts => #{notify_settings_changed => true}}), |
| 353 |
do_await_enable_connect_protocol(config(protocol, Config), ConnPid), |
| 354 |
StreamRef = gun:ws_upgrade(ConnPid, "/set_options"), |
| 355 |
receive |
| 356 |
{gun_upgrade, ConnPid, StreamRef, [<<"websocket">>], _} -> |
| 357 |
ok; |
| 358 |
{gun_response, ConnPid, _, _, Status, Headers} -> |
| 359 |
exit({ws_upgrade_failed, Status, Headers}); |
| 360 |
{gun_error, ConnPid, StreamRef, Reason} -> |
| 361 |
exit({ws_upgrade_failed, Reason}) |
| 362 |
after 1000 -> |
| 363 |
error(timeout) |
| 364 |
end, |
| 365 |
%% We first send a 1MB frame to confirm that yes, we can |
| 366 |
%% send a frame that large. The default max_frame_size is infinity. |
| 367 |
gun:ws_send(ConnPid, StreamRef, {binary, <<0:8000000>>}), |
| 368 |
{ws, {binary, <<0:8000000>>}} = gun:await(ConnPid, StreamRef), |
| 369 |
%% Trigger the change in max_frame_size. From now on we will |
| 370 |
%% only allow frames of up to 1000 bytes. |
| 371 |
gun:ws_send(ConnPid, StreamRef, {text, <<"max_frame_size_small">>}), |
| 372 |
%% Confirm that we can send frames of up to 1000 bytes. |
| 373 |
gun:ws_send(ConnPid, StreamRef, {binary, <<0:8000>>}), |
| 374 |
{ws, {binary, <<0:8000>>}} = gun:await(ConnPid, StreamRef), |
| 375 |
%% Confirm that sending frames larger than 1000 bytes |
| 376 |
%% results in the closing of the connection. |
| 377 |
gun:ws_send(ConnPid, StreamRef, {binary, <<0:8008>>}), |
| 378 |
receive |
| 379 |
{gun_down, ConnPid, _, _, _} -> |
| 380 |
ok |
| 381 |
after 2000 -> |
| 382 |
error(timeout) |
| 383 |
end. |
| 384 |
|
| 385 |
websocket_shutdown_reason(Config) -> |
| 386 |
doc("The command {shutdown_reason, any()} can be used to " |
| 387 |
"change the shutdown reason of a Websocket connection."), |
| 388 |
ConnPid = gun_open(Config, #{http2_opts => #{notify_settings_changed => true}}), |
| 389 |
do_await_enable_connect_protocol(config(protocol, Config), ConnPid), |
| 390 |
StreamRef = gun:ws_upgrade(ConnPid, "/shutdown_reason", [ |
| 391 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 392 |
]), |
| 393 |
{upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef), |
| 394 |
WsPid = receive {ws_pid, P} -> P after 1000 -> error(timeout) end, |
| 395 |
MRef = monitor(process, WsPid), |
| 396 |
WsPid ! {self(), {?MODULE, ?FUNCTION_NAME}}, |
| 397 |
receive |
| 398 |
{'DOWN', MRef, process, WsPid, {shutdown, {?MODULE, ?FUNCTION_NAME}}} -> |
| 399 |
ok |
| 400 |
after 1000 -> |
| 401 |
error(timeout) |
| 402 |
end. |
| 403 |
|
| 404 |
websocket_terminate_close_normal(Config) -> |
| 405 |
doc("Receiving a close frame results in a terminate/3 call. " |
| 406 |
"The Req object is kept in a more compact form by default."), |
| 407 |
ConnPid = gun_open(Config, #{http2_opts => #{notify_settings_changed => true}}), |
| 408 |
do_await_enable_connect_protocol(config(protocol, Config), ConnPid), |
| 409 |
StreamRef = gun:ws_upgrade(ConnPid, "/terminate", [ |
| 410 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 411 |
]), |
| 412 |
{upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef), |
| 413 |
{ws_pid, WsPid} = do_receive(ws_pid), |
| 414 |
MRef = monitor(process, WsPid), |
| 415 |
gun:ws_send(ConnPid, StreamRef, close), |
| 416 |
{terminate, remote, Req} = do_receive(terminate), |
| 417 |
{'DOWN', MRef, process, WsPid, normal} = do_receive('DOWN'), |
| 418 |
%% Confirm terminate/3 was called with a compacted Req. |
| 419 |
true = maps:is_key(path, Req), |
| 420 |
false = maps:is_key(headers, Req), |
| 421 |
ok. |
| 422 |
|
| 423 |
websocket_terminate_close_reason(Config) -> |
| 424 |
doc("Receiving a close frame results in a terminate/3 call. " |
| 425 |
"The Req object is kept in a more compact form by default."), |
| 426 |
ConnPid = gun_open(Config, #{http2_opts => #{notify_settings_changed => true}}), |
| 427 |
do_await_enable_connect_protocol(config(protocol, Config), ConnPid), |
| 428 |
StreamRef = gun:ws_upgrade(ConnPid, "/terminate", [ |
| 429 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 430 |
]), |
| 431 |
{upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef), |
| 432 |
{ws_pid, WsPid} = do_receive(ws_pid), |
| 433 |
MRef = monitor(process, WsPid), |
| 434 |
gun:ws_send(ConnPid, StreamRef, {close, 4000, <<"test-close">>}), |
| 435 |
{terminate, {remote, 4000, <<"test-close">>}, Req} = do_receive(terminate), |
| 436 |
{'DOWN', MRef, process, WsPid, normal} = do_receive('DOWN'), |
| 437 |
%% Confirm terminate/3 was called with a compacted Req. |
| 438 |
true = maps:is_key(path, Req), |
| 439 |
false = maps:is_key(headers, Req), |
| 440 |
ok. |
| 441 |
|
| 442 |
websocket_terminate_socket_close(Config) -> |
| 443 |
doc("The socket getting closed results in a terminate/3 call. " |
| 444 |
"The Req object is kept in a more compact form by default."), |
| 445 |
Protocol = config(protocol, Config), |
| 446 |
ConnPid = gun_open(Config, #{http2_opts => #{notify_settings_changed => true}}), |
| 447 |
do_await_enable_connect_protocol(Protocol, ConnPid), |
| 448 |
StreamRef = gun:ws_upgrade(ConnPid, "/terminate", [ |
| 449 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 450 |
]), |
| 451 |
{upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef), |
| 452 |
{ws_pid, WsPid} = do_receive(ws_pid), |
| 453 |
MRef = monitor(process, WsPid), |
| 454 |
gun:close(ConnPid), |
| 455 |
%% Terminate reasons differ depending on the protocol. |
| 456 |
{terminate, Reason, Req} = do_receive(terminate), |
| 457 |
case Reason of |
| 458 |
{error, closed} when Protocol =:= http -> ok; |
| 459 |
shutdown when Protocol =:= http2 -> ok |
| 460 |
end, |
| 461 |
{'DOWN', MRef, process, WsPid, normal} = do_receive('DOWN'), |
| 462 |
%% Confirm terminate/3 was called with a compacted Req. |
| 463 |
true = maps:is_key(path, Req), |
| 464 |
false = maps:is_key(headers, Req), |
| 465 |
ok. |
| 466 |
|
| 467 |
websocket_terminate_req_filter(Config) -> |
| 468 |
doc("Receiving a close frame results in a terminate/3 call. " |
| 469 |
"A function can be given to filter the Req object."), |
| 470 |
ConnPid = gun_open(Config, #{http2_opts => #{notify_settings_changed => true}}), |
| 471 |
do_await_enable_connect_protocol(config(protocol, Config), ConnPid), |
| 472 |
StreamRef = gun:ws_upgrade(ConnPid, "/terminate?req_filter", [ |
| 473 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 474 |
]), |
| 475 |
{upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef), |
| 476 |
{ws_pid, WsPid} = do_receive(ws_pid), |
| 477 |
MRef = monitor(process, WsPid), |
| 478 |
gun:ws_send(ConnPid, StreamRef, close), |
| 479 |
{terminate, remote, Req} = do_receive(terminate), |
| 480 |
{'DOWN', MRef, process, WsPid, normal} = do_receive('DOWN'), |
| 481 |
%% Confirm terminate/3 was called with a filtered Req. |
| 482 |
filtered = Req, |
| 483 |
ok. |