stream_handler_SUITE.erl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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(stream_handler_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. %% ct.
  20. all() ->
  21. cowboy_test:common_all().
  22. groups() ->
  23. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  24. init_per_group(Name = http, Config) ->
  25. cowboy_test:init_http(Name, #{stream_handlers => [stream_handler_h]}, Config);
  26. init_per_group(Name = https, Config) ->
  27. cowboy_test:init_https(Name, #{stream_handlers => [stream_handler_h]}, Config);
  28. init_per_group(Name = h2, Config) ->
  29. cowboy_test:init_http2(Name, #{stream_handlers => [stream_handler_h]}, Config);
  30. init_per_group(Name = h2c, Config) ->
  31. Config1 = cowboy_test:init_http(Name, #{stream_handlers => [stream_handler_h]}, Config),
  32. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  33. init_per_group(Name = http_compress, Config) ->
  34. cowboy_test:init_http(Name, #{
  35. stream_handlers => [cowboy_compress_h, stream_handler_h]
  36. }, Config);
  37. init_per_group(Name = https_compress, Config) ->
  38. cowboy_test:init_https(Name, #{
  39. stream_handlers => [cowboy_compress_h, stream_handler_h]
  40. }, Config);
  41. init_per_group(Name = h2_compress, Config) ->
  42. cowboy_test:init_http2(Name, #{
  43. stream_handlers => [cowboy_compress_h, stream_handler_h]
  44. }, Config);
  45. init_per_group(Name = h2c_compress, Config) ->
  46. Config1 = cowboy_test:init_http(Name, #{
  47. stream_handlers => [cowboy_compress_h, stream_handler_h]
  48. }, Config),
  49. lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
  50. end_per_group(Name, _) ->
  51. cowboy:stop_listener(Name).
  52. %% Tests.
  53. terminate_on_socket_close(Config) ->
  54. doc("Confirm terminate/3 is called when the socket gets closed brutally."),
  55. Self = self(),
  56. ConnPid = gun_open(Config),
  57. Ref = gun:get(ConnPid, "/long_polling", [
  58. {<<"accept-encoding">>, <<"gzip">>},
  59. {<<"x-test-case">>, <<"stream">>},
  60. {<<"x-test-pid">>, pid_to_list(Self)}
  61. ]),
  62. %% Confirm init/3 is called and receive the beginning of the response.
  63. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  64. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  65. %% Close the socket.
  66. ok = gun:close(ConnPid),
  67. %% Confirm terminate/3 is called.
  68. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  69. ok.