| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 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 |
|
| 15 |
-module(loop_handler_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
-import(cowboy_test, [gun_open/1]). |
| 22 |
|
| 23 |
%% ct. |
| 24 |
|
| 25 |
all() -> |
| 26 |
cowboy_test:common_all(). |
| 27 |
|
| 28 |
groups() -> |
| 29 |
cowboy_test:common_groups(ct_helper:all(?MODULE)). |
| 30 |
|
| 31 |
init_per_group(Name, Config) -> |
| 32 |
cowboy_test:init_common_groups(Name, Config, ?MODULE). |
| 33 |
|
| 34 |
end_per_group(Name, _) -> |
| 35 |
cowboy_test:stop_group(Name). |
| 36 |
|
| 37 |
%% Dispatch configuration. |
| 38 |
|
| 39 |
init_dispatch(_) -> |
| 40 |
cowboy_router:compile([{'_', [ |
| 41 |
{"/long_polling", long_polling_h, []}, |
| 42 |
{"/loop_body", loop_handler_body_h, []}, |
| 43 |
{"/loop_request_timeout", loop_handler_timeout_h, []}, |
| 44 |
{"/loop_timeout_init", loop_handler_timeout_init_h, []}, |
| 45 |
{"/loop_timeout_info", loop_handler_timeout_info_h, []}, |
| 46 |
{"/loop_timeout_hibernate", loop_handler_timeout_hibernate_h, []} |
| 47 |
]}]). |
| 48 |
|
| 49 |
%% Tests. |
| 50 |
|
| 51 |
info_read_body(Config) -> |
| 52 |
doc("Check that a loop handler can read the request body in info/3."), |
| 53 |
ConnPid = gun_open(Config), |
| 54 |
Ref = gun:post(ConnPid, "/loop_body", [{<<"accept-encoding">>, <<"gzip">>}], |
| 55 |
<< 0:100000/unit:8 >>), |
| 56 |
{response, fin, 200, _} = gun:await(ConnPid, Ref), |
| 57 |
ok. |
| 58 |
|
| 59 |
long_polling(Config) -> |
| 60 |
doc("Simple long-polling."), |
| 61 |
ConnPid = gun_open(Config), |
| 62 |
Ref = gun:get(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}]), |
| 63 |
{response, fin, 299, _} = gun:await(ConnPid, Ref), |
| 64 |
ok. |
| 65 |
|
| 66 |
long_polling_unread_body(Config) -> |
| 67 |
doc("Long-polling with a body that is not read by the handler."), |
| 68 |
ConnPid = gun_open(Config), |
| 69 |
Ref = gun:post(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}], |
| 70 |
<< 0:100000/unit:8 >>), |
| 71 |
{response, fin, 299, _} = gun:await(ConnPid, Ref), |
| 72 |
ok. |
| 73 |
|
| 74 |
long_polling_pipeline(Config) -> |
| 75 |
doc("Pipeline of long-polling calls."), |
| 76 |
ConnPid = gun_open(Config), |
| 77 |
Refs = [gun:get(ConnPid, "/long_polling", [{<<"accept-encoding">>, <<"gzip">>}]) |
| 78 |
|| _ <- lists:seq(1, 2)], |
| 79 |
_ = [{response, fin, 299, _} = gun:await(ConnPid, Ref) || Ref <- Refs], |
| 80 |
ok. |
| 81 |
|
| 82 |
request_timeout(Config) -> |
| 83 |
doc("Ensure that the request_timeout isn't applied when a request is ongoing."), |
| 84 |
ConnPid = gun_open(Config), |
| 85 |
Ref = gun:get(ConnPid, "/loop_request_timeout", [{<<"accept-encoding">>, <<"gzip">>}]), |
| 86 |
{response, nofin, 200, _} = gun:await(ConnPid, Ref, 10000), |
| 87 |
ok. |
| 88 |
|
| 89 |
timeout_hibernate(Config) -> |
| 90 |
doc("Ensure that loop handler idle timeouts don't trigger after hibernate is returned."), |
| 91 |
ConnPid = gun_open(Config), |
| 92 |
Ref = gun:get(ConnPid, "/loop_timeout_hibernate", [{<<"accept-encoding">>, <<"gzip">>}]), |
| 93 |
{response, fin, 200, _} = gun:await(ConnPid, Ref), |
| 94 |
ok. |
| 95 |
|
| 96 |
timeout_info(Config) -> |
| 97 |
doc("Ensure that loop handler idle timeouts trigger on time when set in info/3."), |
| 98 |
ConnPid = gun_open(Config), |
| 99 |
Ref = gun:get(ConnPid, "/loop_timeout_info", [{<<"accept-encoding">>, <<"gzip">>}]), |
| 100 |
{response, fin, 299, _} = gun:await(ConnPid, Ref), |
| 101 |
ok. |
| 102 |
|
| 103 |
timeout_init(Config) -> |
| 104 |
doc("Ensure that loop handler idle timeouts trigger on time when set in init/2."), |
| 105 |
ConnPid = gun_open(Config), |
| 106 |
Ref = gun:get(ConnPid, "/loop_timeout_init?timeout=1000", |
| 107 |
[{<<"accept-encoding">>, <<"gzip">>}]), |
| 108 |
{response, fin, 200, _} = gun:await(ConnPid, Ref), |
| 109 |
Ref2 = gun:get(ConnPid, "/loop_timeout_init?timeout=100", |
| 110 |
[{<<"accept-encoding">>, <<"gzip">>}]), |
| 111 |
{response, fin, 299, _} = gun:await(ConnPid, Ref2), |
| 112 |
ok. |