0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Fix cowboy:set_env when the env value is missing

This commit is contained in:
Loïc Hoguin 2017-10-02 16:19:13 +02:00
parent 6460e9d2d2
commit 9317751cb3
No known key found for this signature in database
GPG key ID: 71366FF21851DF03
2 changed files with 19 additions and 3 deletions

View file

@ -68,6 +68,6 @@ stop_listener(Ref) ->
-spec set_env(ranch:ref(), atom(), any()) -> ok. -spec set_env(ranch:ref(), atom(), any()) -> ok.
set_env(Ref, Name, Value) -> set_env(Ref, Name, Value) ->
Opts = ranch:get_protocol_options(Ref), Opts = ranch:get_protocol_options(Ref),
{_, Env} = maps:find(env, Opts), Env = maps:get(env, Opts, #{}),
Opts2 = maps:put(env, maps:put(Name, Value, Env), Opts), Opts2 = maps:put(env, maps:put(Name, Value, Env), Opts),
ok = ranch:set_protocol_options(Ref, Opts2). ok = ranch:set_protocol_options(Ref, Opts2).

View file

@ -20,11 +20,14 @@
-import(cowboy_test, [gun_open/1]). -import(cowboy_test, [gun_open/1]).
all() -> all() ->
cowboy_test:common_all(). [{group, no_env}|cowboy_test:common_all()].
groups() -> groups() ->
cowboy_test:common_groups(ct_helper:all(?MODULE)). Common = ct_helper:all(?MODULE) -- [set_env_missing],
[{no_env, [], [set_env_missing]}|cowboy_test:common_groups(Common)].
init_per_group(Name=no_env, Config) ->
cowboy_test:init_http(Name, #{}, Config);
init_per_group(Name, Config) -> init_per_group(Name, Config) ->
cowboy_test:init_common_groups(Name, Config, ?MODULE). cowboy_test:init_common_groups(Name, Config, ?MODULE).
@ -48,3 +51,16 @@ set_env(Config) ->
Ref2 = gun:get(ConnPid2, "/"), Ref2 = gun:get(ConnPid2, "/"),
{response, _, 400, _} = gun:await(ConnPid2, Ref2), {response, _, 400, _} = gun:await(ConnPid2, Ref2),
ok. ok.
set_env_missing(Config) ->
doc("Live replace a middleware environment value when env was not provided."),
ConnPid1 = gun_open(Config),
Ref1 = gun:get(ConnPid1, "/"),
{response, _, 500, _} = gun:await(ConnPid1, Ref1),
Listener = proplists:get_value(name, config(tc_group_properties, Config)),
cowboy:set_env(Listener, dispatch, []),
%% Only new connections get the updated environment.
ConnPid2 = gun_open(Config),
Ref2 = gun:get(ConnPid2, "/"),
{response, _, 400, _} = gun:await(ConnPid2, Ref2),
ok.