loop_handler_SUITE.erl 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. long_polling(Config) ->
  38. doc("Simple long-polling."),
  39. ConnPid = gun_open(Config),
  40. Ref = gun:get(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}]),
  41. {response, fin, 299, _} = gun:await(ConnPid, Ref),
  42. ok.
  43. long_polling_body(Config) ->
  44. doc("Long-polling with a body that falls within the configurable limits."),
  45. ConnPid = gun_open(Config),
  46. Ref = gun:post(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}],
  47. << 0:5000/unit:8 >>),
  48. {response, fin, 299, _} = gun:await(ConnPid, Ref),
  49. ok.
  50. long_polling_body_too_large(Config) ->
  51. doc("Long-polling with a body that exceeds the configurable limits."),
  52. ConnPid = gun_open(Config),
  53. Ref = gun:post(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}],
  54. << 0:100000/unit:8 >>),
  55. {response, fin, 500, _} = 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. loop_body(Config) ->
  65. doc("Check that a loop handler can read the request body in info/3."),
  66. ConnPid = gun_open(Config),
  67. Ref = gun:post(ConnPid, "/loop_body", [{<<"accept-encoding">>, <<"gzip">>}],
  68. << 0:100000/unit:8 >>),
  69. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  70. ok.
  71. loop_request_timeout(Config) ->
  72. doc("Ensure that the request_timeout isn't applied when a request is ongoing."),
  73. ConnPid = gun_open(Config),
  74. Ref = gun:get(ConnPid, "/loop_timeout", [{<<"accept-encoding">>, <<"gzip">>}]),
  75. {response, nofin, 200, _} = gun:await(ConnPid, Ref, 10000),
  76. ok.