misc_SUITE.erl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. router_invalid_path(Config) ->
  35. doc("Ensure a path with invalid percent-encoded characters results in a 400."),
  36. ConnPid = gun_open(Config),
  37. Ref = gun:get(ConnPid, "/version/path/%\\u0016\\u0016/path"),
  38. {response, _, 400, _} = gun:await(ConnPid, Ref),
  39. ok.
  40. set_env(Config) ->
  41. doc("Live replace a middleware environment value."),
  42. ConnPid1 = gun_open(Config),
  43. Ref1 = gun:get(ConnPid1, "/"),
  44. {response, _, 200, _} = gun:await(ConnPid1, Ref1),
  45. Listener = proplists:get_value(name, config(tc_group_properties, Config)),
  46. cowboy:set_env(Listener, dispatch, []),
  47. %% Only new connections get the updated environment.
  48. ConnPid2 = gun_open(Config),
  49. Ref2 = gun:get(ConnPid2, "/"),
  50. {response, _, 400, _} = gun:await(ConnPid2, Ref2),
  51. ok.
  52. set_env_missing(Config) ->
  53. doc("Live replace a middleware environment value when env was not provided."),
  54. ConnPid1 = gun_open(Config),
  55. Ref1 = gun:get(ConnPid1, "/"),
  56. {response, _, 500, _} = gun:await(ConnPid1, Ref1),
  57. Listener = proplists:get_value(name, config(tc_group_properties, Config)),
  58. cowboy:set_env(Listener, dispatch, []),
  59. %% Only new connections get the updated environment.
  60. ConnPid2 = gun_open(Config),
  61. Ref2 = gun:get(ConnPid2, "/"),
  62. {response, _, 400, _} = gun:await(ConnPid2, Ref2),
  63. ok.