From 4a6503186bf3a72880e7c99be76406550aeded96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 2 Jul 2019 17:28:44 +0200 Subject: Add response_inform/response_headers/response_end events This covers many scenarios but more need to be added. --- test/event_SUITE.erl | 84 ++++++++++++++++++++++++++++++++++++++++---- test/handlers/empty_h.erl | 11 ++++++ test/handlers/hello_h.erl | 10 ++++++ test/handlers/inform_h.erl | 16 +++++++++ test/handlers/stream_h.erl | 14 ++++++++ test/handlers/trailers_h.erl | 18 ++++++++++ 6 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 test/handlers/empty_h.erl create mode 100644 test/handlers/hello_h.erl create mode 100644 test/handlers/inform_h.erl create mode 100644 test/handlers/stream_h.erl create mode 100644 test/handlers/trailers_h.erl (limited to 'test') diff --git a/test/event_SUITE.erl b/test/event_SUITE.erl index d52296e..83edca9 100644 --- a/test/event_SUITE.erl +++ b/test/event_SUITE.erl @@ -37,7 +37,13 @@ groups() -> init_per_suite(Config) -> {ok, _} = cowboy:start_clear(?MODULE, [], #{env => #{ - dispatch => cowboy_router:compile([{'_', [{"/", ws_echo, []}]}]) + dispatch => cowboy_router:compile([{'_', [ + {"/", hello_h, []}, + {"/empty", empty_h, []}, + {"/inform", inform_h, []}, + {"/stream", stream_h, []}, + {"/trailers", trailers_h, []} + ]}]) }}), OriginPort = ranch:get_port(?MODULE), [{origin_port, OriginPort}|Config]. @@ -157,11 +163,11 @@ do_request_event_headers(Config, EventName) -> request_end(Config) -> doc("Confirm that the request_end event callback is called."), - do_request_end_event(Config, ?FUNCTION_NAME), - do_request_end_event_headers(Config, ?FUNCTION_NAME), - do_request_end_event_headers_content_length(Config, ?FUNCTION_NAME). + do_request_end(Config, ?FUNCTION_NAME), + do_request_end_headers(Config, ?FUNCTION_NAME), + do_request_end_headers_content_length(Config, ?FUNCTION_NAME). -do_request_end_event(Config, EventName) -> +do_request_end(Config, EventName) -> {ok, Pid, _} = do_gun_open(Config), {ok, _} = gun:await_up(Pid), StreamRef = gun:get(Pid, "/"), @@ -172,7 +178,7 @@ do_request_end_event(Config, EventName) -> } = do_receive_event(EventName), gun:close(Pid). -do_request_end_event_headers(Config, EventName) -> +do_request_end_headers(Config, EventName) -> {ok, Pid, _} = do_gun_open(Config), {ok, _} = gun:await_up(Pid), StreamRef = gun:put(Pid, "/", [ @@ -187,7 +193,7 @@ do_request_end_event_headers(Config, EventName) -> } = do_receive_event(EventName), gun:close(Pid). -do_request_end_event_headers_content_length(Config, EventName) -> +do_request_end_headers_content_length(Config, EventName) -> {ok, Pid, _} = do_gun_open(Config), {ok, _} = gun:await_up(Pid), StreamRef = gun:put(Pid, "/", [ @@ -203,6 +209,58 @@ do_request_end_event_headers_content_length(Config, EventName) -> } = do_receive_event(EventName), gun:close(Pid). +response_inform(Config) -> + doc("Confirm that the request_inform event callback is called."), + {ok, Pid, _} = do_gun_open(Config), + {ok, _} = gun:await_up(Pid), + StreamRef = gun:get(Pid, "/inform"), + ReplyTo = self(), + #{ + stream_ref := StreamRef, + reply_to := ReplyTo, + status := 103, + headers := [_|_] + } = do_receive_event(?FUNCTION_NAME), + #{ + stream_ref := StreamRef, + reply_to := ReplyTo, + status := 103, + headers := [_|_] + } = do_receive_event(?FUNCTION_NAME), + gun:close(Pid). + +response_headers(Config) -> + doc("Confirm that the request_headers event callback is called."), + {ok, Pid, _} = do_gun_open(Config), + {ok, _} = gun:await_up(Pid), + StreamRef = gun:get(Pid, "/"), + ReplyTo = self(), + #{ + stream_ref := StreamRef, + reply_to := ReplyTo, + status := 200, + headers := [_|_] + } = do_receive_event(?FUNCTION_NAME), + gun:close(Pid). + +response_end(Config) -> + doc("Confirm that the request_headers event callback is called."), + do_response_end(Config, ?FUNCTION_NAME, "/"), + do_response_end(Config, ?FUNCTION_NAME, "/empty"), + do_response_end(Config, ?FUNCTION_NAME, "/stream"), + do_response_end(Config, ?FUNCTION_NAME, "/trailers"). + +do_response_end(Config, EventName, Path) -> + {ok, Pid, _} = do_gun_open(Config), + {ok, _} = gun:await_up(Pid), + StreamRef = gun:get(Pid, Path), + ReplyTo = self(), + #{ + stream_ref := StreamRef, + reply_to := ReplyTo + } = do_receive_event(EventName), + gun:close(Pid). + disconnect(Config) -> doc("Confirm that the disconnect event callback is called on disconnect."), {ok, OriginPid, OriginPort} = init_origin(tcp), @@ -277,6 +335,18 @@ request_end(EventData, Pid) -> Pid ! {?FUNCTION_NAME, EventData}, Pid. +response_inform(EventData, Pid) -> + Pid ! {?FUNCTION_NAME, EventData}, + Pid. + +response_headers(EventData, Pid) -> + Pid ! {?FUNCTION_NAME, EventData}, + Pid. + +response_end(EventData, Pid) -> + Pid ! {?FUNCTION_NAME, EventData}, + Pid. + disconnect(EventData, Pid) -> Pid ! {?FUNCTION_NAME, EventData}, Pid. diff --git a/test/handlers/empty_h.erl b/test/handlers/empty_h.erl new file mode 100644 index 0000000..7b634cb --- /dev/null +++ b/test/handlers/empty_h.erl @@ -0,0 +1,11 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(empty_h). + +-export([init/2]). + +init(Req, State) -> + {ok, cowboy_req:reply(200, #{ + <<"content-type">> => <<"text/plain">> + }, Req), State}. + diff --git a/test/handlers/hello_h.erl b/test/handlers/hello_h.erl new file mode 100644 index 0000000..e3c71ab --- /dev/null +++ b/test/handlers/hello_h.erl @@ -0,0 +1,10 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(hello_h). + +-export([init/2]). + +init(Req, State) -> + {ok, cowboy_req:reply(200, #{ + <<"content-type">> => <<"text/plain">> + }, <<"Hello world!">>, Req), State}. diff --git a/test/handlers/inform_h.erl b/test/handlers/inform_h.erl new file mode 100644 index 0000000..f62b31f --- /dev/null +++ b/test/handlers/inform_h.erl @@ -0,0 +1,16 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(inform_h). + +-export([init/2]). + +init(Req, State) -> + cowboy_req:inform(103, #{ + <<"content-type">> => <<"text/plain">> + }, Req), + cowboy_req:inform(103, #{ + <<"content-type">> => <<"text/plain">> + }, Req), + {ok, cowboy_req:reply(200, #{ + <<"content-type">> => <<"text/plain">> + }, <<"Hello world!">>, Req), State}. diff --git a/test/handlers/stream_h.erl b/test/handlers/stream_h.erl new file mode 100644 index 0000000..fd6774e --- /dev/null +++ b/test/handlers/stream_h.erl @@ -0,0 +1,14 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(stream_h). + +-export([init/2]). + +init(Req0, State) -> + Req = cowboy_req:stream_reply(200, #{ + <<"content-type">> => <<"text/plain">> + }, Req0), + cowboy_req:stream_body(<<"Hello ">>, nofin, Req), + cowboy_req:stream_body(<<"world!">>, nofin, Req), + %% The stream will be closed by Cowboy. + {ok, Req, State}. diff --git a/test/handlers/trailers_h.erl b/test/handlers/trailers_h.erl new file mode 100644 index 0000000..a94d974 --- /dev/null +++ b/test/handlers/trailers_h.erl @@ -0,0 +1,18 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(trailers_h). + +-export([init/2]). + +init(Req0, State) -> + Req = cowboy_req:stream_reply(200, #{ + <<"content-type">> => <<"text/plain">>, + <<"trailer">> => <<"expires">> + }, Req0), + cowboy_req:stream_body(<<"Hello ">>, nofin, Req), + cowboy_req:stream_body(<<"world!">>, nofin, Req), + cowboy_req:stream_trailers(#{ + <<"expires">> => <<"Sun, 10 Dec 2017 19:13:47 GMT">> + }, Req), + {ok, Req, State}. + -- cgit v1.2.3