misc_SUITE.erl 3.6 KB

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