| 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(plain_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 |
|
| 23 |
%% ct. |
| 24 |
|
| 25 |
all() -> |
| 26 |
cowboy_test:common_all(). |
| 27 |
|
| 28 |
groups() -> |
| 29 |
cowboy_test:common_groups(ct_helper:all(?MODULE)). |
| 30 |
|
| 31 |
init_per_suite(Config) -> |
| 32 |
ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"), |
| 33 |
Config. |
| 34 |
|
| 35 |
end_per_suite(Config) -> |
| 36 |
ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static"). |
| 37 |
|
| 38 |
init_per_group(Name, Config) -> |
| 39 |
cowboy_test:init_common_groups(Name, Config, ?MODULE). |
| 40 |
|
| 41 |
end_per_group(Name, _) -> |
| 42 |
cowboy_test:stop_group(Name). |
| 43 |
|
| 44 |
%% Routes. |
| 45 |
|
| 46 |
init_dispatch(_) -> |
| 47 |
cowboy_router:compile([{"localhost", [ |
| 48 |
{"/crash/external_exit", crash_h, external_exit}, |
| 49 |
{"/crash/no_reply", crash_h, no_reply}, |
| 50 |
{"/crash/reply", crash_h, reply} |
| 51 |
]}]). |
| 52 |
|
| 53 |
%% Tests. |
| 54 |
|
| 55 |
crash_after_reply(Config) -> |
| 56 |
doc("A plain handler crash after a response was sent " |
| 57 |
"results in no 500 response."), |
| 58 |
ConnPid = gun_open(Config), |
| 59 |
Ref = gun:get(ConnPid, "/crash/reply", [ |
| 60 |
{<<"accept-encoding">>, <<"gzip">>} |
| 61 |
]), |
| 62 |
Protocol = config(protocol, Config), |
| 63 |
_ = case gun:await(ConnPid, Ref) of |
| 64 |
{response, fin, 200, _} -> |
| 65 |
{error, timeout} = gun:await(ConnPid, Ref, 1000); |
| 66 |
%% See maybe_h3_error comment for details. |
| 67 |
{error, {stream_error, {stream_error, h3_internal_error, _}}} |
| 68 |
when Protocol =:= http3 -> |
| 69 |
ok |
| 70 |
end, |
| 71 |
gun:close(ConnPid). |
| 72 |
|
| 73 |
crash_before_reply(Config) -> |
| 74 |
doc("A plain handler crash before a response was sent " |
| 75 |
"results in a 500 response."), |
| 76 |
ConnPid = gun_open(Config), |
| 77 |
Ref = gun:get(ConnPid, "/crash/no_reply", [ |
| 78 |
{<<"accept-encoding">>, <<"gzip">>} |
| 79 |
]), |
| 80 |
{response, fin, 500, _} = gun:await(ConnPid, Ref), |
| 81 |
gun:close(ConnPid). |
| 82 |
|
| 83 |
external_exit_before_reply(Config) -> |
| 84 |
doc("A plain handler exits externally before a response was sent " |
| 85 |
"results in a 500 response."), |
| 86 |
ConnPid = gun_open(Config), |
| 87 |
Ref = gun:get(ConnPid, "/crash/external_exit", [ |
| 88 |
{<<"accept-encoding">>, <<"gzip">>} |
| 89 |
]), |
| 90 |
{response, fin, 500, _} = gun:await(ConnPid, Ref), |
| 91 |
gun:close(ConnPid). |