misc_SUITE.erl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. all() ->
  21. [{group, app}, {group, set_env}|cowboy_test:common_all()].
  22. groups() ->
  23. Common = ct_helper:all(?MODULE)
  24. -- [restart_gracefully, set_env, set_env_missing],
  25. [
  26. {app, [], [restart_gracefully]},
  27. {set_env, [parallel], [set_env, set_env_missing]}
  28. |cowboy_test:common_groups(Common)].
  29. init_per_group(Name=app, Config) ->
  30. cowboy_test:init_http(Name, #{
  31. env => #{dispatch => init_dispatch(Config)}
  32. }, Config);
  33. init_per_group(set_env, Config) ->
  34. Config;
  35. init_per_group(Name, Config) ->
  36. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  37. end_per_group(set_env, _) ->
  38. ok;
  39. end_per_group(Name, _) ->
  40. cowboy:stop_listener(Name).
  41. init_dispatch(_) ->
  42. cowboy_router:compile([{"localhost", [
  43. {"/", hello_h, []}
  44. ]}]).
  45. %% Logger function silencing the expected crash.
  46. error("Ranch listener " ++ _, [set_env_missing|_]) ->
  47. ok;
  48. error(Format, Args) ->
  49. error_logger:error_msg(Format, Args).
  50. %% Tests.
  51. restart_gracefully(Config) ->
  52. doc("Ensure we can process request when the cowboy application is being restarted."),
  53. ConnPid = gun_open(Config),
  54. %% We can do a request before stopping cowboy.
  55. Ref1 = gun:get(ConnPid, "/"),
  56. {response, _, 200, _} = gun:await(ConnPid, Ref1),
  57. %% Stop the cowboy application.
  58. ok = application:stop(cowboy),
  59. %% We can still do a request even though cowboy is stopped.
  60. Ref2 = gun:get(ConnPid, "/"),
  61. {response, _, 200, _} = gun:await(ConnPid, Ref2),
  62. %% Start the cowboy application again.
  63. ok = application:start(cowboy),
  64. %% Even after restarting there are no issues.
  65. Ref3 = gun:get(ConnPid, "/"),
  66. {response, _, 200, _} = gun:await(ConnPid, Ref3),
  67. ok.
  68. router_invalid_path(Config) ->
  69. doc("Ensure a path with invalid percent-encoded characters results in a 400."),
  70. ConnPid = gun_open(Config),
  71. Ref = gun:get(ConnPid, "/version/path/%\\u0016\\u0016/path"),
  72. {response, _, 400, _} = gun:await(ConnPid, Ref),
  73. ok.
  74. set_env(Config0) ->
  75. doc("Live replace a middleware environment value."),
  76. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  77. env => #{dispatch => []}
  78. }, Config0),
  79. try
  80. ConnPid1 = gun_open(Config),
  81. Ref1 = gun:get(ConnPid1, "/"),
  82. {response, _, 400, _} = gun:await(ConnPid1, Ref1),
  83. cowboy:set_env(?FUNCTION_NAME, dispatch, init_dispatch(Config)),
  84. %% Only new connections get the updated environment.
  85. ConnPid2 = gun_open(Config),
  86. Ref2 = gun:get(ConnPid2, "/"),
  87. {response, _, 200, _} = gun:await(ConnPid2, Ref2)
  88. after
  89. cowboy:stop_listener(?FUNCTION_NAME)
  90. end.
  91. set_env_missing(Config0) ->
  92. doc("Live replace a middleware environment value when env was not provided."),
  93. Config = cowboy_test:init_http(?FUNCTION_NAME, #{
  94. logger => ?MODULE
  95. }, Config0),
  96. try
  97. ConnPid1 = gun_open(Config),
  98. Ref1 = gun:get(ConnPid1, "/"),
  99. {response, _, 500, _} = gun:await(ConnPid1, Ref1),
  100. cowboy:set_env(?FUNCTION_NAME, dispatch, []),
  101. %% Only new connections get the updated environment.
  102. ConnPid2 = gun_open(Config),
  103. Ref2 = gun:get(ConnPid2, "/"),
  104. {response, _, 400, _} = gun:await(ConnPid2, Ref2)
  105. after
  106. cowboy:stop_listener(?FUNCTION_NAME)
  107. end.