| 1 |
%% This module sends a message with terminate arguments to the test case process. |
| 2 |
|
| 3 |
-module(ws_terminate_h). |
| 4 |
-behavior(cowboy_websocket). |
| 5 |
|
| 6 |
-export([init/2]). |
| 7 |
-export([websocket_init/1]). |
| 8 |
-export([websocket_handle/2]). |
| 9 |
-export([websocket_info/2]). |
| 10 |
-export([terminate/3]). |
| 11 |
|
| 12 |
-record(state, { |
| 13 |
pid |
| 14 |
}). |
| 15 |
|
| 16 |
init(Req, _) -> |
| 17 |
Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))), |
| 18 |
Opts = case cowboy_req:qs(Req) of |
| 19 |
<<"req_filter">> -> #{req_filter => fun(_) -> filtered end}; |
| 20 |
_ -> #{} |
| 21 |
end, |
| 22 |
{cowboy_websocket, Req, #state{pid=Pid}, Opts}. |
| 23 |
|
| 24 |
websocket_init(State=#state{pid=Pid}) -> |
| 25 |
Pid ! {ws_pid, self()}, |
| 26 |
%% We must trap 'EXIT' signals for HTTP/2 to call terminate/3. |
| 27 |
process_flag(trap_exit, true), |
| 28 |
{ok, State}. |
| 29 |
|
| 30 |
websocket_handle(_, State) -> |
| 31 |
{ok, State}. |
| 32 |
|
| 33 |
websocket_info(_, State) -> |
| 34 |
{ok, State}. |
| 35 |
|
| 36 |
terminate(Reason, Req, #state{pid=Pid}) -> |
| 37 |
Pid ! {terminate, Reason, Req}, |
| 38 |
ok. |