misc_SUITE.erl 3.5 KB

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