stream_handler_SUITE.erl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. %% Copyright (c) 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(stream_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. -import(cowboy_test, [gun_down/1]).
  21. %% ct.
  22. all() ->
  23. cowboy_test:common_all().
  24. groups() ->
  25. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  26. %% We set this module as a logger in order to silence expected errors.
  27. init_per_group(Name = http, Config) ->
  28. cowboy_test:init_http(Name, #{
  29. logger => ?MODULE,
  30. stream_handlers => [stream_handler_h]
  31. }, Config);
  32. init_per_group(Name = https, Config) ->
  33. cowboy_test:init_https(Name, #{
  34. logger => ?MODULE,
  35. stream_handlers => [stream_handler_h]
  36. }, Config);
  37. init_per_group(Name = h2, Config) ->
  38. cowboy_test:init_http2(Name, #{
  39. logger => ?MODULE,
  40. stream_handlers => [stream_handler_h]
  41. }, Config);
  42. init_per_group(Name = h2c, Config) ->
  43. Config1 = cowboy_test:init_http(Name, #{
  44. logger => ?MODULE,
  45. stream_handlers => [stream_handler_h]
  46. }, Config),
  47. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  48. init_per_group(Name = http_compress, Config) ->
  49. cowboy_test:init_http(Name, #{
  50. logger => ?MODULE,
  51. stream_handlers => [cowboy_compress_h, stream_handler_h]
  52. }, Config);
  53. init_per_group(Name = https_compress, Config) ->
  54. cowboy_test:init_https(Name, #{
  55. logger => ?MODULE,
  56. stream_handlers => [cowboy_compress_h, stream_handler_h]
  57. }, Config);
  58. init_per_group(Name = h2_compress, Config) ->
  59. cowboy_test:init_http2(Name, #{
  60. logger => ?MODULE,
  61. stream_handlers => [cowboy_compress_h, stream_handler_h]
  62. }, Config);
  63. init_per_group(Name = h2c_compress, Config) ->
  64. Config1 = cowboy_test:init_http(Name, #{
  65. logger => ?MODULE,
  66. stream_handlers => [cowboy_compress_h, stream_handler_h]
  67. }, Config),
  68. lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
  69. end_per_group(Name, _) ->
  70. cowboy:stop_listener(Name).
  71. %% Logger function silencing the expected crashes.
  72. error("Unhandled exception " ++ _, [error, crash|_]) ->
  73. ok;
  74. error(Format, Args) ->
  75. error_logger:error_msg(Format, Args).
  76. %% Tests.
  77. crash_in_init(Config) ->
  78. doc("Confirm an error is sent when a stream handler crashes in init/3."),
  79. Self = self(),
  80. ConnPid = gun_open(Config),
  81. Ref = gun:get(ConnPid, "/long_polling", [
  82. {<<"accept-encoding">>, <<"gzip">>},
  83. {<<"x-test-case">>, <<"crash_in_init">>},
  84. {<<"x-test-pid">>, pid_to_list(Self)}
  85. ]),
  86. %% Confirm init/3 is called.
  87. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  88. %% Confirm terminate/3 is NOT called. We have no state to give to it.
  89. receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end,
  90. %% Confirm early_error/5 is called in HTTP/1.1's case.
  91. %% HTTP/2 does not send a response back so there is no early_error call.
  92. case config(protocol, Config) of
  93. http -> receive {Self, Pid, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end;
  94. http2 -> ok
  95. end,
  96. %% Receive a 500 error response.
  97. case gun:await(ConnPid, Ref) of
  98. {response, fin, 500, _} -> ok;
  99. {error, {stream_error, {stream_error, internal_error, _}}} -> ok
  100. end.
  101. crash_in_data(Config) ->
  102. doc("Confirm an error is sent when a stream handler crashes in data/4."),
  103. Self = self(),
  104. ConnPid = gun_open(Config),
  105. Ref = gun:post(ConnPid, "/long_polling", [
  106. {<<"accept-encoding">>, <<"gzip">>},
  107. {<<"content-length">>, <<"6">>},
  108. {<<"x-test-case">>, <<"crash_in_data">>},
  109. {<<"x-test-pid">>, pid_to_list(Self)}
  110. ]),
  111. %% Confirm init/3 is called.
  112. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  113. %% Send data to make the stream handler crash.
  114. gun:data(ConnPid, Ref, fin, <<"Hello!">>),
  115. %% Confirm terminate/3 is called, indicating the stream ended.
  116. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  117. %% Receive a 500 error response.
  118. case gun:await(ConnPid, Ref) of
  119. {response, fin, 500, _} -> ok;
  120. {error, {stream_error, {stream_error, internal_error, _}}} -> ok
  121. end.
  122. crash_in_info(Config) ->
  123. doc("Confirm an error is sent when a stream handler crashes in info/3."),
  124. Self = self(),
  125. ConnPid = gun_open(Config),
  126. Ref = gun:get(ConnPid, "/long_polling", [
  127. {<<"accept-encoding">>, <<"gzip">>},
  128. {<<"x-test-case">>, <<"crash_in_info">>},
  129. {<<"x-test-pid">>, pid_to_list(Self)}
  130. ]),
  131. %% Confirm init/3 is called.
  132. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  133. %% Send a message to make the stream handler crash.
  134. Pid ! {{Pid, 1}, crash},
  135. %% Confirm terminate/3 is called, indicating the stream ended.
  136. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  137. %% Receive a 500 error response.
  138. case gun:await(ConnPid, Ref) of
  139. {response, fin, 500, _} -> ok;
  140. {error, {stream_error, {stream_error, internal_error, _}}} -> ok
  141. end.
  142. crash_in_terminate(Config) ->
  143. doc("Confirm the state is correct when a stream handler crashes in terminate/3."),
  144. Self = self(),
  145. ConnPid = gun_open(Config),
  146. %% Do a first request.
  147. Ref1 = gun:get(ConnPid, "/hello_world", [
  148. {<<"accept-encoding">>, <<"gzip">>},
  149. {<<"x-test-case">>, <<"crash_in_terminate">>},
  150. {<<"x-test-pid">>, pid_to_list(Self)}
  151. ]),
  152. %% Confirm init/3 is called.
  153. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  154. %% Confirm terminate/3 is called.
  155. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  156. %% Receive the response.
  157. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  158. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref1),
  159. %% Do a second request to make sure the connection state is still good.
  160. Ref2 = gun:get(ConnPid, "/hello_world", [
  161. {<<"accept-encoding">>, <<"gzip">>},
  162. {<<"x-test-case">>, <<"crash_in_terminate">>},
  163. {<<"x-test-pid">>, pid_to_list(Self)}
  164. ]),
  165. %% Confirm init/3 is called. The pid shouldn't change.
  166. receive {Self, Pid, init, _, _, _} -> ok after 1000 -> error(timeout) end,
  167. %% Confirm terminate/3 is called.
  168. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  169. %% Receive the second response.
  170. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  171. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref2),
  172. ok.
  173. crash_in_early_error(Config) ->
  174. case config(protocol, Config) of
  175. http -> do_crash_in_early_error(Config);
  176. http2 -> doc("The callback early_error/5 is not currently used for HTTP/2.")
  177. end.
  178. do_crash_in_early_error(Config) ->
  179. doc("Confirm an error is sent when a stream handler crashes in early_error/5."
  180. "The connection is kept open by Cowboy."),
  181. Self = self(),
  182. ConnPid = gun_open(Config),
  183. Ref1 = gun:get(ConnPid, "/long_polling", [
  184. {<<"accept-encoding">>, <<"gzip">>},
  185. {<<"x-test-case">>, <<"crash_in_early_error">>},
  186. {<<"x-test-pid">>, pid_to_list(Self)}
  187. ]),
  188. %% Confirm init/3 is called.
  189. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  190. %% Confirm terminate/3 is NOT called. We have no state to give to it.
  191. receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end,
  192. %% Confirm early_error/5 is called.
  193. receive {Self, Pid, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  194. %% Receive a 500 error response.
  195. {response, fin, 500, _} = gun:await(ConnPid, Ref1),
  196. %% This error is not fatal. We should be able to repeat it on the same connection.
  197. Ref2 = gun:get(ConnPid, "/long_polling", [
  198. {<<"accept-encoding">>, <<"gzip">>},
  199. {<<"x-test-case">>, <<"crash_in_early_error">>},
  200. {<<"x-test-pid">>, pid_to_list(Self)}
  201. ]),
  202. %% Confirm init/3 is called.
  203. receive {Self, Pid, init, _, _, _} -> ok after 1000 -> error(timeout) end,
  204. %% Confirm terminate/3 is NOT called. We have no state to give to it.
  205. receive {Self, Pid, terminate, _, _, _} -> error(terminate) after 1000 -> ok end,
  206. %% Confirm early_error/5 is called.
  207. receive {Self, Pid, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  208. %% Receive a 500 error response.
  209. {response, fin, 500, _} = gun:await(ConnPid, Ref2),
  210. ok.
  211. crash_in_early_error_fatal(Config) ->
  212. case config(protocol, Config) of
  213. http -> do_crash_in_early_error_fatal(Config);
  214. http2 -> doc("The callback early_error/5 is not currently used for HTTP/2.")
  215. end.
  216. do_crash_in_early_error_fatal(Config) ->
  217. doc("Confirm an error is sent when a stream handler crashes in early_error/5."
  218. "The error was fatal and the connection is closed by Cowboy."),
  219. Self = self(),
  220. ConnPid = gun_open(Config),
  221. Ref = gun:get(ConnPid, "/long_polling", [
  222. {<<"accept-encoding">>, <<"gzip">>},
  223. {<<"host">>, <<"host:port">>},
  224. {<<"x-test-case">>, <<"crash_in_early_error_fatal">>},
  225. {<<"x-test-pid">>, pid_to_list(Self)}
  226. ]),
  227. %% Confirm init/3 is NOT called. The error occurs before we reach this step.
  228. receive {Self, _, init, _, _, _} -> error(init) after 1000 -> ok end,
  229. %% Confirm terminate/3 is NOT called. We have no state to give to it.
  230. receive {Self, _, terminate, _, _, _} -> error(terminate) after 1000 -> ok end,
  231. %% Confirm early_error/5 is called.
  232. receive {Self, _, early_error, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  233. %% Receive a 400 error response. We do not send a 500 when
  234. %% early_error/5 crashes, we send the original error.
  235. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  236. %% Confirm the connection gets closed.
  237. gun_down(ConnPid).
  238. early_error_stream_error_reason(Config) ->
  239. doc("Confirm that the stream_error given to early_error/5 is consistent between protocols."),
  240. Self = self(),
  241. ConnPid = gun_open(Config),
  242. %% We must use different solutions to hit early_error with a stream_error
  243. %% reason in both protocols.
  244. {Method, Headers, Status, Error} = case config(protocol, Config) of
  245. http -> {<<"GET">>, [{<<"host">>, <<"host:port">>}], 400, protocol_error};
  246. http2 -> {<<"TRACE">>, [], 501, no_error}
  247. end,
  248. Ref = gun:request(ConnPid, Method, "/long_polling", [
  249. {<<"accept-encoding">>, <<"gzip">>},
  250. {<<"x-test-case">>, <<"early_error_stream_error_reason">>},
  251. {<<"x-test-pid">>, pid_to_list(Self)}
  252. |Headers], <<>>),
  253. %% Confirm init/3 is NOT called. The error occurs before we reach this step.
  254. receive {Self, _, init, _, _, _} -> error(init) after 1000 -> ok end,
  255. %% Confirm terminate/3 is NOT called. We have no state to give to it.
  256. receive {Self, _, terminate, _, _, _} -> error(terminate) after 1000 -> ok end,
  257. %% Confirm early_error/5 is called.
  258. Reason = receive {Self, _, early_error, _, R, _, _, _} -> R after 1000 -> error(timeout) end,
  259. %% Confirm that the Reason is a {stream_error, Reason, Human}.
  260. {stream_error, Error, HumanReadable} = Reason,
  261. true = is_atom(HumanReadable),
  262. %% Receive a 400 or 501 error response.
  263. {response, fin, Status, _} = gun:await(ConnPid, Ref),
  264. ok.
  265. set_options_ignore_unknown(Config) ->
  266. doc("Confirm that unknown options are ignored when using the set_options commands."),
  267. Self = self(),
  268. ConnPid = gun_open(Config),
  269. Ref = gun:get(ConnPid, "/long_polling", [
  270. {<<"accept-encoding">>, <<"gzip">>},
  271. {<<"x-test-case">>, <<"set_options_ignore_unknown">>},
  272. {<<"x-test-pid">>, pid_to_list(Self)}
  273. ]),
  274. %% Confirm init/3 is called.
  275. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  276. %% Confirm terminate/3 is called, indicating the stream ended.
  277. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  278. %% Confirm the response is sent.
  279. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  280. {ok, _} = gun:await_body(ConnPid, Ref),
  281. ok.
  282. shutdown_on_stream_stop(Config) ->
  283. doc("Confirm supervised processes are shutdown when stopping the stream."),
  284. Self = self(),
  285. ConnPid = gun_open(Config),
  286. Ref = gun:get(ConnPid, "/long_polling", [
  287. {<<"accept-encoding">>, <<"gzip">>},
  288. {<<"x-test-case">>, <<"shutdown_on_stream_stop">>},
  289. {<<"x-test-pid">>, pid_to_list(Self)}
  290. ]),
  291. %% Confirm init/3 is called.
  292. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  293. %% Receive the pid of the newly started process and monitor it.
  294. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  295. MRef = monitor(process, Spawn),
  296. Spawn ! {Self, ready},
  297. %% Confirm terminate/3 is called, indicating the stream ended.
  298. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  299. %% We should receive a DOWN message soon after (or before) because the stream
  300. %% handler is stopping the stream immediately after the process started.
  301. receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end,
  302. %% The response is still sent.
  303. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  304. {ok, _} = gun:await_body(ConnPid, Ref),
  305. ok.
  306. shutdown_on_socket_close(Config) ->
  307. doc("Confirm supervised processes are shutdown when the socket closes."),
  308. Self = self(),
  309. ConnPid = gun_open(Config),
  310. _ = gun:get(ConnPid, "/long_polling", [
  311. {<<"accept-encoding">>, <<"gzip">>},
  312. {<<"x-test-case">>, <<"shutdown_on_socket_close">>},
  313. {<<"x-test-pid">>, pid_to_list(Self)}
  314. ]),
  315. %% Confirm init/3 is called.
  316. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  317. %% Receive the pid of the newly started process and monitor it.
  318. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  319. MRef = monitor(process, Spawn),
  320. Spawn ! {Self, ready},
  321. %% Close the socket.
  322. ok = gun:close(ConnPid),
  323. %% Confirm terminate/3 is called, indicating the stream ended.
  324. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  325. %% Confirm we receive a DOWN message for the child process.
  326. receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end,
  327. ok.
  328. shutdown_timeout_on_stream_stop(Config) ->
  329. doc("Confirm supervised processes are killed "
  330. "when the shutdown timeout triggers after stopping the stream."),
  331. Self = self(),
  332. ConnPid = gun_open(Config),
  333. Ref = gun:get(ConnPid, "/long_polling", [
  334. {<<"accept-encoding">>, <<"gzip">>},
  335. {<<"x-test-case">>, <<"shutdown_timeout_on_stream_stop">>},
  336. {<<"x-test-pid">>, pid_to_list(Self)}
  337. ]),
  338. %% Confirm init/3 is called.
  339. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  340. %% Receive the pid of the newly started process and monitor it.
  341. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  342. MRef = monitor(process, Spawn),
  343. Spawn ! {Self, ready},
  344. %% Confirm terminate/3 is called, indicating the stream ended.
  345. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  346. %% We should NOT receive a DOWN message immediately.
  347. receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end,
  348. %% We should received it now.
  349. receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end,
  350. %% The response is still sent.
  351. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  352. {ok, _} = gun:await_body(ConnPid, Ref),
  353. ok.
  354. shutdown_timeout_on_socket_close(Config) ->
  355. doc("Confirm supervised processes are killed "
  356. "when the shutdown timeout triggers after the socket has closed."),
  357. Self = self(),
  358. ConnPid = gun_open(Config),
  359. _ = gun:get(ConnPid, "/long_polling", [
  360. {<<"accept-encoding">>, <<"gzip">>},
  361. {<<"x-test-case">>, <<"shutdown_timeout_on_socket_close">>},
  362. {<<"x-test-pid">>, pid_to_list(Self)}
  363. ]),
  364. %% Confirm init/3 is called.
  365. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  366. %% Receive the pid of the newly started process and monitor it.
  367. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  368. MRef = monitor(process, Spawn),
  369. Spawn ! {Self, ready},
  370. %% Close the socket.
  371. ok = gun:close(ConnPid),
  372. %% Confirm terminate/3 is called, indicating the stream ended.
  373. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  374. %% We should NOT receive a DOWN message immediately.
  375. receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end,
  376. %% We should received it now.
  377. receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end,
  378. ok.
  379. switch_protocol_after_headers(Config) ->
  380. case config(protocol, Config) of
  381. http -> do_switch_protocol_after_response(
  382. <<"switch_protocol_after_headers">>, Config);
  383. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  384. end.
  385. switch_protocol_after_headers_data(Config) ->
  386. case config(protocol, Config) of
  387. http -> do_switch_protocol_after_response(
  388. <<"switch_protocol_after_headers_data">>, Config);
  389. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  390. end.
  391. switch_protocol_after_response(Config) ->
  392. case config(protocol, Config) of
  393. http -> do_switch_protocol_after_response(
  394. <<"switch_protocol_after_response">>, Config);
  395. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  396. end.
  397. do_switch_protocol_after_response(TestCase, Config) ->
  398. doc("The 101 informational response must not be sent when a response "
  399. "has already been sent before the switch_protocol is returned."),
  400. Self = self(),
  401. ConnPid = gun_open(Config),
  402. Ref = gun:get(ConnPid, "/long_polling", [
  403. {<<"accept-encoding">>, <<"gzip">>},
  404. {<<"x-test-case">>, TestCase},
  405. {<<"x-test-pid">>, pid_to_list(Self)}
  406. ]),
  407. %% Confirm init/3 is called and receive the response.
  408. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  409. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  410. Gzipped =
  411. lists:keyfind(<<"content-encoding">>, 1, Headers)
  412. =:= {<<"content-encoding">>, <<"gzip">>},
  413. case TestCase of
  414. <<"switch_protocol_after_headers">> ->
  415. ok;
  416. _ ->
  417. <<"{}">> = case gun:await_body(ConnPid, Ref) of
  418. {ok, Body} when Gzipped ->
  419. zlib:gunzip(Body);
  420. {ok, Body} ->
  421. Body
  422. end,
  423. ok
  424. end,
  425. {error, _} = gun:await(ConnPid, Ref),
  426. %% Confirm terminate/3 is called.
  427. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  428. %% Confirm takeover/7 is called.
  429. receive {Self, Pid, takeover, _, _, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  430. ok.
  431. terminate_on_socket_close(Config) ->
  432. doc("Confirm terminate/3 is called when the socket gets closed brutally."),
  433. Self = self(),
  434. ConnPid = gun_open(Config),
  435. Ref = gun:get(ConnPid, "/long_polling", [
  436. {<<"accept-encoding">>, <<"gzip">>},
  437. {<<"x-test-case">>, <<"terminate_on_socket_close">>},
  438. {<<"x-test-pid">>, pid_to_list(Self)}
  439. ]),
  440. %% Confirm init/3 is called and receive the beginning of the response.
  441. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  442. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  443. %% Close the socket.
  444. ok = gun:close(ConnPid),
  445. %% Confirm terminate/3 is called.
  446. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  447. ok.
  448. terminate_on_stop(Config) ->
  449. doc("Confirm terminate/3 is called after stop is returned."),
  450. Self = self(),
  451. ConnPid = gun_open(Config),
  452. Ref = gun:get(ConnPid, "/long_polling", [
  453. {<<"accept-encoding">>, <<"gzip">>},
  454. {<<"x-test-case">>, <<"terminate_on_stop">>},
  455. {<<"x-test-pid">>, pid_to_list(Self)}
  456. ]),
  457. %% Confirm init/3 is called and receive the response.
  458. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  459. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  460. %% Confirm the stream is still alive even though we
  461. %% received the response fully, and tell it to stop.
  462. Pid ! {{Pid, 1}, please_stop},
  463. receive {Self, Pid, info, _, please_stop, _} -> ok after 1000 -> error(timeout) end,
  464. %% Confirm terminate/3 is called.
  465. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  466. ok.
  467. terminate_on_switch_protocol(Config) ->
  468. case config(protocol, Config) of
  469. http -> do_terminate_on_switch_protocol(Config);
  470. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  471. end.
  472. do_terminate_on_switch_protocol(Config) ->
  473. doc("Confirm terminate/3 is called after switch_protocol is returned."),
  474. Self = self(),
  475. ConnPid = gun_open(Config),
  476. Ref = gun:get(ConnPid, "/long_polling", [
  477. {<<"accept-encoding">>, <<"gzip">>},
  478. {<<"x-test-case">>, <<"terminate_on_switch_protocol">>},
  479. {<<"x-test-pid">>, pid_to_list(Self)}
  480. ]),
  481. %% Confirm init/3 is called and receive the response.
  482. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  483. {inform, 101, _} = gun:await(ConnPid, Ref),
  484. %% Confirm terminate/3 is called.
  485. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  486. %% Confirm takeover/7 is called.
  487. receive {Self, Pid, takeover, _, _, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  488. ok.