loop_handler_SUITE.erl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. %% Copyright (c) 2011-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(loop_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_group(Name, Config) ->
  26. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  27. end_per_group(Name, _) ->
  28. cowboy:stop_listener(Name).
  29. %% Dispatch configuration.
  30. init_dispatch(_) ->
  31. cowboy_router:compile([{'_', [
  32. {"/long_polling", long_polling_h, []},
  33. {"/loop_body", loop_handler_body_h, []},
  34. {"/loop_timeout", loop_handler_timeout_h, []}
  35. ]}]).
  36. %% Tests.
  37. info_read_body(Config) ->
  38. doc("Check that a loop handler can read the request body in info/3."),
  39. ConnPid = gun_open(Config),
  40. Ref = gun:post(ConnPid, "/loop_body", [{<<"accept-encoding">>, <<"gzip">>}],
  41. << 0:100000/unit:8 >>),
  42. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  43. ok.
  44. long_polling(Config) ->
  45. doc("Simple long-polling."),
  46. ConnPid = gun_open(Config),
  47. Ref = gun:get(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}]),
  48. {response, fin, 299, _} = gun:await(ConnPid, Ref),
  49. ok.
  50. long_polling_unread_body(Config) ->
  51. doc("Long-polling with a body that is not read by the handler."),
  52. ConnPid = gun_open(Config),
  53. Ref = gun:post(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}],
  54. << 0:100000/unit:8 >>),
  55. {response, fin, 299, _} = gun:await(ConnPid, Ref),
  56. ok.
  57. long_polling_pipeline(Config) ->
  58. doc("Pipeline of long-polling calls."),
  59. ConnPid = gun_open(Config),
  60. Refs = [gun:get(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}])
  61. || _ <- lists:seq(1, 2)],
  62. _ = [{response, fin, 299, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  63. ok.
  64. request_timeout(Config) ->
  65. doc("Ensure that the request_timeout isn't applied when a request is ongoing."),
  66. ConnPid = gun_open(Config),
  67. Ref = gun:get(ConnPid, "/loop_timeout", [{<<"accept-encoding">>, <<"gzip">>}]),
  68. {response, nofin, 200, _} = gun:await(ConnPid, Ref, 10000),
  69. ok.