loop_handler_SUITE.erl 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. -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, 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", [{<<"accept-encoding">>, <<"gzip">>}]),
  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", [{<<"accept-encoding">>, <<"gzip">>}],
  46. << 0:5000/unit:8 >>),
  47. {response, fin, 102, _} = gun:await(ConnPid, Ref),
  48. ok.
  49. long_polling_body_too_large(Config) ->
  50. doc("Long-polling with a body that exceeds the configurable limits."),
  51. ConnPid = gun_open(Config),
  52. Ref = gun:post(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}],
  53. << 0:100000/unit:8 >>),
  54. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  55. ok.
  56. long_polling_pipeline(Config) ->
  57. doc("Pipeline of long-polling calls."),
  58. ConnPid = gun_open(Config),
  59. Refs = [gun:get(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}])
  60. || _ <- lists:seq(1, 2)],
  61. _ = [{response, fin, 102, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  62. ok.
  63. loop_body(Config) ->
  64. doc("Check that a loop handler can read the request body in info/3."),
  65. ConnPid = gun_open(Config),
  66. Ref = gun:post(ConnPid, "/loop_body", [{<<"accept-encoding">>, <<"gzip">>}],
  67. << 0:100000/unit:8 >>),
  68. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  69. ok.
  70. loop_request_timeout(Config) ->
  71. doc("Ensure that the request_timeout isn't applied when a request is ongoing."),
  72. ConnPid = gun_open(Config),
  73. Ref = gun:get(ConnPid, "/loop_timeout", [{<<"accept-encoding">>, <<"gzip">>}]),
  74. {response, nofin, 200, _} = gun:await(ConnPid, Ref, 10000),
  75. ok.