loop_handler_SUITE.erl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. %% Copyright (c) 2011-2014, 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, nowarn_export_all]).
  16. -import(cowboy_test, [config/2]).
  17. -import(cowboy_test, [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(cowboy_test:all(?MODULE)).
  24. init_per_group(Name, Config) ->
  25. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  26. end_per_group(Name, _) ->
  27. cowboy:stop_listener(Name).
  28. %% Dispatch configuration.
  29. init_dispatch(_) ->
  30. cowboy_router:compile([{'_', [
  31. {"/long_polling", long_polling_h, []},
  32. {"/loop_body", loop_handler_body_h, []},
  33. {"/loop_timeout", loop_handler_timeout_h, []}
  34. ]}]).
  35. %% Tests.
  36. long_polling(Config) ->
  37. doc("Simple long-polling."),
  38. ConnPid = gun_open(Config),
  39. Ref = gun:get(ConnPid, "/long_polling"),
  40. {response, fin, 102, _} = gun:await(ConnPid, Ref),
  41. ok.
  42. long_polling_body(Config) ->
  43. doc("Long-polling with a body that falls within the configurable limits."),
  44. ConnPid = gun_open(Config),
  45. Ref = gun:post(ConnPid, "/long_polling", [], << 0:5000/unit:8 >>),
  46. {response, fin, 102, _} = gun:await(ConnPid, Ref),
  47. ok.
  48. long_polling_body_too_large(Config) ->
  49. doc("Long-polling with a body that exceeds the configurable limits."),
  50. ConnPid = gun_open(Config),
  51. Ref = gun:post(ConnPid, "/long_polling", [], << 0:100000/unit:8 >>),
  52. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  53. ok.
  54. long_polling_pipeline(Config) ->
  55. doc("Pipeline of long-polling calls."),
  56. ConnPid = gun_open(Config),
  57. Refs = [gun:get(ConnPid, "/long_polling") || _ <- lists:seq(1, 2)],
  58. _ = [{response, fin, 102, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  59. ok.
  60. loop_body(Config) ->
  61. doc("Check that a loop handler can read the request body in info/3."),
  62. ConnPid = gun_open(Config),
  63. Ref = gun:post(ConnPid, "/loop_body", [], << 0:100000/unit:8 >>),
  64. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  65. ok.
  66. loop_timeout(Config) ->
  67. doc("Ensure that the loop handler timeout results in a 204 response."),
  68. ConnPid = gun_open(Config),
  69. Ref = gun:get(ConnPid, "/loop_timeout"),
  70. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  71. ok.