plain_handler_SUITE.erl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. %% Copyright (c) 2018, 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(plain_handler_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. %% ct.
  21. all() ->
  22. cowboy_test:common_all().
  23. groups() ->
  24. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  25. init_per_suite(Config) ->
  26. ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"),
  27. Config.
  28. end_per_suite(Config) ->
  29. ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static").
  30. init_per_group(Name, Config) ->
  31. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  32. end_per_group(Name, _) ->
  33. cowboy_test:stop_group(Name).
  34. %% Routes.
  35. init_dispatch(_) ->
  36. cowboy_router:compile([{"localhost", [
  37. {"/crash/no_reply", crash_h, no_reply},
  38. {"/crash/reply", crash_h, reply}
  39. ]}]).
  40. %% Tests.
  41. crash_after_reply(Config) ->
  42. doc("A plain handler crash after a response was sent "
  43. "results in no 500 response."),
  44. ConnPid = gun_open(Config),
  45. Ref = gun:get(ConnPid, "/crash/reply", [
  46. {<<"accept-encoding">>, <<"gzip">>}
  47. ]),
  48. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  49. {error, timeout} = gun:await(ConnPid, Ref, 1000),
  50. gun:close(ConnPid).
  51. crash_before_reply(Config) ->
  52. doc("A plain handler crash before a response was sent "
  53. "results in a 500 response."),
  54. ConnPid = gun_open(Config),
  55. Ref = gun:get(ConnPid, "/crash/no_reply", [
  56. {<<"accept-encoding">>, <<"gzip">>}
  57. ]),
  58. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  59. gun:close(ConnPid).