| 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(misc_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 |
-include_lib("stdlib/include/assert.hrl"). |
| 24 |
|
| 25 |
all() -> |
| 26 |
[{group, app}, {group, env}|cowboy_test:common_all()]. |
| 27 |
|
| 28 |
groups() -> |
| 29 |
Common = ct_helper:all(?MODULE) |
| 30 |
-- [restart_gracefully, get_env, set_env, set_env_missing], |
| 31 |
[ |
| 32 |
{app, [], [restart_gracefully]}, |
| 33 |
{env, [parallel], [get_env, set_env, set_env_missing]} |
| 34 |
|cowboy_test:common_groups(Common)]. |
| 35 |
|
| 36 |
init_per_group(Name=app, Config) -> |
| 37 |
cowboy_test:init_http(Name, #{ |
| 38 |
env => #{dispatch => init_dispatch(Config)} |
| 39 |
}, Config); |
| 40 |
init_per_group(env, Config) -> |
| 41 |
Config; |
| 42 |
init_per_group(Name, Config) -> |
| 43 |
cowboy_test:init_common_groups(Name, Config, ?MODULE). |
| 44 |
|
| 45 |
end_per_group(env, _) -> |
| 46 |
ok; |
| 47 |
end_per_group(Name, _) -> |
| 48 |
cowboy_test:stop_group(Name). |
| 49 |
|
| 50 |
init_dispatch(_) -> |
| 51 |
cowboy_router:compile([{"localhost", [ |
| 52 |
{"/", hello_h, []} |
| 53 |
]}]). |
| 54 |
|
| 55 |
%% Logger function silencing the expected crash. |
| 56 |
|
| 57 |
error("Ranch listener " ++ _, [set_env_missing|_]) -> |
| 58 |
ok; |
| 59 |
error(Format, Args) -> |
| 60 |
error_logger:error_msg(Format, Args). |
| 61 |
|
| 62 |
%% Tests. |
| 63 |
|
| 64 |
restart_gracefully(Config) -> |
| 65 |
doc("Ensure we can process request when the cowboy application is being restarted."), |
| 66 |
ConnPid = gun_open(Config), |
| 67 |
%% We can do a request before stopping cowboy. |
| 68 |
Ref1 = gun:get(ConnPid, "/"), |
| 69 |
{response, _, 200, _} = gun:await(ConnPid, Ref1), |
| 70 |
%% Stop the cowboy application. |
| 71 |
ok = application:stop(cowboy), |
| 72 |
%% We can still do a request even though cowboy is stopped. |
| 73 |
Ref2 = gun:get(ConnPid, "/"), |
| 74 |
{response, _, 200, _} = gun:await(ConnPid, Ref2), |
| 75 |
%% Start the cowboy application again. |
| 76 |
ok = application:start(cowboy), |
| 77 |
%% Even after restarting there are no issues. |
| 78 |
Ref3 = gun:get(ConnPid, "/"), |
| 79 |
{response, _, 200, _} = gun:await(ConnPid, Ref3), |
| 80 |
ok. |
| 81 |
|
| 82 |
router_invalid_path(Config) -> |
| 83 |
doc("Ensure a path with invalid percent-encoded characters results in a 400."), |
| 84 |
ConnPid = gun_open(Config), |
| 85 |
Ref = gun:get(ConnPid, "/version/path/%\\u0016\\u0016/path"), |
| 86 |
{response, _, 400, _} = gun:await(ConnPid, Ref), |
| 87 |
ok. |
| 88 |
|
| 89 |
get_env(Config0) -> |
| 90 |
doc("Ensure we can retrieve middleware environment values."), |
| 91 |
Dispatch = init_dispatch(Config0), |
| 92 |
_Config = cowboy_test:init_http(?FUNCTION_NAME, #{ |
| 93 |
env => #{ |
| 94 |
dispatch => Dispatch, |
| 95 |
the_key => the_value |
| 96 |
} |
| 97 |
}, Config0), |
| 98 |
try |
| 99 |
Dispatch = cowboy:get_env(?FUNCTION_NAME, dispatch), |
| 100 |
Dispatch = cowboy:get_env(?FUNCTION_NAME, dispatch, the_default), |
| 101 |
the_value = cowboy:get_env(?FUNCTION_NAME, the_key), |
| 102 |
the_value = cowboy:get_env(?FUNCTION_NAME, the_key, the_default), |
| 103 |
?assertError({badkey, missing_key}, |
| 104 |
cowboy:get_env(?FUNCTION_NAME, missing_key)), |
| 105 |
the_default = cowboy:get_env(?FUNCTION_NAME, missing_key, the_default) |
| 106 |
after |
| 107 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 108 |
end. |
| 109 |
|
| 110 |
set_env(Config0) -> |
| 111 |
doc("Live replace a middleware environment value."), |
| 112 |
Config = cowboy_test:init_http(?FUNCTION_NAME, #{ |
| 113 |
env => #{dispatch => []} |
| 114 |
}, Config0), |
| 115 |
try |
| 116 |
ConnPid1 = gun_open(Config), |
| 117 |
Ref1 = gun:get(ConnPid1, "/"), |
| 118 |
{response, _, 400, _} = gun:await(ConnPid1, Ref1), |
| 119 |
cowboy:set_env(?FUNCTION_NAME, dispatch, init_dispatch(Config)), |
| 120 |
%% Only new connections get the updated environment. |
| 121 |
ConnPid2 = gun_open(Config), |
| 122 |
Ref2 = gun:get(ConnPid2, "/"), |
| 123 |
{response, _, 200, _} = gun:await(ConnPid2, Ref2) |
| 124 |
after |
| 125 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 126 |
end. |
| 127 |
|
| 128 |
set_env_missing(Config0) -> |
| 129 |
doc("Live replace a middleware environment value when env was not provided."), |
| 130 |
Config = cowboy_test:init_http(?FUNCTION_NAME, #{ |
| 131 |
logger => ?MODULE |
| 132 |
}, Config0), |
| 133 |
try |
| 134 |
ConnPid1 = gun_open(Config), |
| 135 |
Ref1 = gun:get(ConnPid1, "/"), |
| 136 |
{response, _, 500, _} = gun:await(ConnPid1, Ref1), |
| 137 |
cowboy:set_env(?FUNCTION_NAME, dispatch, []), |
| 138 |
%% Only new connections get the updated environment. |
| 139 |
ConnPid2 = gun_open(Config), |
| 140 |
Ref2 = gun:get(ConnPid2, "/"), |
| 141 |
{response, _, 400, _} = gun:await(ConnPid2, Ref2) |
| 142 |
after |
| 143 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 144 |
end. |