misc_SUITE.erl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. %% Copyright (c) 2017, Loïc Hoguin <essen@ninenines.eu>
  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. -module(misc_SUITE).
  15. -compile(export_all).
  16. -import(ct_helper, [config/2]).
  17. -import(ct_helper, [doc/1]).
  18. -import(cowboy_test, [gun_open/1]).
  19. all() ->
  20. [{group, no_env}|cowboy_test:common_all()].
  21. groups() ->
  22. Common = ct_helper:all(?MODULE) -- [set_env_missing],
  23. [{no_env, [], [set_env_missing]}|cowboy_test:common_groups(Common)].
  24. init_per_group(Name=no_env, Config) ->
  25. cowboy_test:init_http(Name, #{}, Config);
  26. init_per_group(Name, Config) ->
  27. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  28. end_per_group(Name, _) ->
  29. cowboy:stop_listener(Name).
  30. init_dispatch(_) ->
  31. cowboy_router:compile([{"localhost", [
  32. {"/", hello_h, []}
  33. ]}]).
  34. set_env(Config) ->
  35. doc("Live replace a middleware environment value."),
  36. ConnPid1 = gun_open(Config),
  37. Ref1 = gun:get(ConnPid1, "/"),
  38. {response, _, 200, _} = gun:await(ConnPid1, Ref1),
  39. Listener = proplists:get_value(name, config(tc_group_properties, Config)),
  40. cowboy:set_env(Listener, dispatch, []),
  41. %% Only new connections get the updated environment.
  42. ConnPid2 = gun_open(Config),
  43. Ref2 = gun:get(ConnPid2, "/"),
  44. {response, _, 400, _} = gun:await(ConnPid2, Ref2),
  45. ok.
  46. set_env_missing(Config) ->
  47. doc("Live replace a middleware environment value when env was not provided."),
  48. ConnPid1 = gun_open(Config),
  49. Ref1 = gun:get(ConnPid1, "/"),
  50. {response, _, 500, _} = gun:await(ConnPid1, Ref1),
  51. Listener = proplists:get_value(name, config(tc_group_properties, Config)),
  52. cowboy:set_env(Listener, dispatch, []),
  53. %% Only new connections get the updated environment.
  54. ConnPid2 = gun_open(Config),
  55. Ref2 = gun:get(ConnPid2, "/"),
  56. {response, _, 400, _} = gun:await(ConnPid2, Ref2),
  57. ok.