misc_SUITE.erl 3.5 KB

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