stream_handler_SUITE.erl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. flow_after_body_fully_read(Config) ->
  266. doc("A flow command may be returned even after the body was read fully."),
  267. Self = self(),
  268. ConnPid = gun_open(Config),
  269. Ref = gun:post(ConnPid, "/long_polling", [
  270. {<<"x-test-case">>, <<"flow_after_body_fully_read">>},
  271. {<<"x-test-pid">>, pid_to_list(Self)}
  272. ], <<"Hello world!">>),
  273. %% Receive a 200 response, sent after the second flow command,
  274. %% confirming that the flow command was accepted.
  275. {response, _, 200, _} = gun:await(ConnPid, Ref),
  276. gun:close(ConnPid).
  277. set_options_ignore_unknown(Config) ->
  278. doc("Confirm that unknown options are ignored when using the set_options commands."),
  279. Self = self(),
  280. ConnPid = gun_open(Config),
  281. Ref = gun:get(ConnPid, "/long_polling", [
  282. {<<"accept-encoding">>, <<"gzip">>},
  283. {<<"x-test-case">>, <<"set_options_ignore_unknown">>},
  284. {<<"x-test-pid">>, pid_to_list(Self)}
  285. ]),
  286. %% Confirm init/3 is called.
  287. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  288. %% Confirm terminate/3 is called, indicating the stream ended.
  289. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  290. %% Confirm the response is sent.
  291. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  292. {ok, _} = gun:await_body(ConnPid, Ref),
  293. ok.
  294. shutdown_on_stream_stop(Config) ->
  295. doc("Confirm supervised processes are shutdown when stopping the stream."),
  296. Self = self(),
  297. ConnPid = gun_open(Config),
  298. Ref = gun:get(ConnPid, "/long_polling", [
  299. {<<"accept-encoding">>, <<"gzip">>},
  300. {<<"x-test-case">>, <<"shutdown_on_stream_stop">>},
  301. {<<"x-test-pid">>, pid_to_list(Self)}
  302. ]),
  303. %% Confirm init/3 is called.
  304. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  305. %% Receive the pid of the newly started process and monitor it.
  306. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  307. MRef = monitor(process, Spawn),
  308. Spawn ! {Self, ready},
  309. %% Confirm terminate/3 is called, indicating the stream ended.
  310. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  311. %% We should receive a DOWN message soon after (or before) because the stream
  312. %% handler is stopping the stream immediately after the process started.
  313. receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end,
  314. %% The response is still sent.
  315. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  316. {ok, _} = gun:await_body(ConnPid, Ref),
  317. ok.
  318. shutdown_on_socket_close(Config) ->
  319. doc("Confirm supervised processes are shutdown when the socket closes."),
  320. Self = self(),
  321. ConnPid = gun_open(Config),
  322. _ = gun:get(ConnPid, "/long_polling", [
  323. {<<"accept-encoding">>, <<"gzip">>},
  324. {<<"x-test-case">>, <<"shutdown_on_socket_close">>},
  325. {<<"x-test-pid">>, pid_to_list(Self)}
  326. ]),
  327. %% Confirm init/3 is called.
  328. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  329. %% Receive the pid of the newly started process and monitor it.
  330. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  331. MRef = monitor(process, Spawn),
  332. Spawn ! {Self, ready},
  333. %% Close the socket.
  334. ok = gun:close(ConnPid),
  335. %% Confirm terminate/3 is called, indicating the stream ended.
  336. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  337. %% Confirm we receive a DOWN message for the child process.
  338. receive {'DOWN', MRef, process, Spawn, shutdown} -> ok after 1000 -> error(timeout) end,
  339. ok.
  340. shutdown_timeout_on_stream_stop(Config) ->
  341. doc("Confirm supervised processes are killed "
  342. "when the shutdown timeout triggers after stopping the stream."),
  343. Self = self(),
  344. ConnPid = gun_open(Config),
  345. Ref = gun:get(ConnPid, "/long_polling", [
  346. {<<"accept-encoding">>, <<"gzip">>},
  347. {<<"x-test-case">>, <<"shutdown_timeout_on_stream_stop">>},
  348. {<<"x-test-pid">>, pid_to_list(Self)}
  349. ]),
  350. %% Confirm init/3 is called.
  351. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  352. %% Receive the pid of the newly started process and monitor it.
  353. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  354. MRef = monitor(process, Spawn),
  355. Spawn ! {Self, ready},
  356. %% Confirm terminate/3 is called, indicating the stream ended.
  357. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  358. %% We should NOT receive a DOWN message immediately.
  359. receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end,
  360. %% We should received it now.
  361. receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end,
  362. %% The response is still sent.
  363. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  364. {ok, _} = gun:await_body(ConnPid, Ref),
  365. ok.
  366. shutdown_timeout_on_socket_close(Config) ->
  367. doc("Confirm supervised processes are killed "
  368. "when the shutdown timeout triggers after the socket has closed."),
  369. Self = self(),
  370. ConnPid = gun_open(Config),
  371. _ = gun:get(ConnPid, "/long_polling", [
  372. {<<"accept-encoding">>, <<"gzip">>},
  373. {<<"x-test-case">>, <<"shutdown_timeout_on_socket_close">>},
  374. {<<"x-test-pid">>, pid_to_list(Self)}
  375. ]),
  376. %% Confirm init/3 is called.
  377. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  378. %% Receive the pid of the newly started process and monitor it.
  379. Spawn = receive {Self, Pid, spawned, S} -> S after 1000 -> error(timeout) end,
  380. MRef = monitor(process, Spawn),
  381. Spawn ! {Self, ready},
  382. %% Close the socket.
  383. ok = gun:close(ConnPid),
  384. %% Confirm terminate/3 is called, indicating the stream ended.
  385. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  386. %% We should NOT receive a DOWN message immediately.
  387. receive {'DOWN', MRef, process, Spawn, killed} -> error(killed) after 1500 -> ok end,
  388. %% We should receive it now.
  389. receive {'DOWN', MRef, process, Spawn, killed} -> ok after 1000 -> error(timeout) end,
  390. ok.
  391. switch_protocol_after_headers(Config) ->
  392. case config(protocol, Config) of
  393. http -> do_switch_protocol_after_response(
  394. <<"switch_protocol_after_headers">>, Config);
  395. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  396. end.
  397. switch_protocol_after_headers_data(Config) ->
  398. case config(protocol, Config) of
  399. http -> do_switch_protocol_after_response(
  400. <<"switch_protocol_after_headers_data">>, Config);
  401. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  402. end.
  403. switch_protocol_after_response(Config) ->
  404. case config(protocol, Config) of
  405. http -> do_switch_protocol_after_response(
  406. <<"switch_protocol_after_response">>, Config);
  407. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  408. end.
  409. do_switch_protocol_after_response(TestCase, Config) ->
  410. doc("The 101 informational response must not be sent when a response "
  411. "has already been sent before the switch_protocol is returned."),
  412. Self = self(),
  413. ConnPid = gun_open(Config),
  414. Ref = gun:get(ConnPid, "/long_polling", [
  415. {<<"accept-encoding">>, <<"gzip">>},
  416. {<<"x-test-case">>, TestCase},
  417. {<<"x-test-pid">>, pid_to_list(Self)}
  418. ]),
  419. %% Confirm init/3 is called and receive the response.
  420. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  421. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  422. Gzipped =
  423. lists:keyfind(<<"content-encoding">>, 1, Headers)
  424. =:= {<<"content-encoding">>, <<"gzip">>},
  425. case TestCase of
  426. <<"switch_protocol_after_headers">> ->
  427. ok;
  428. _ ->
  429. <<"{}">> = case gun:await_body(ConnPid, Ref) of
  430. {ok, Body} when Gzipped ->
  431. zlib:gunzip(Body);
  432. {ok, Body} ->
  433. Body
  434. end,
  435. ok
  436. end,
  437. {error, _} = gun:await(ConnPid, Ref),
  438. %% Confirm terminate/3 is called.
  439. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  440. %% Confirm takeover/7 is called.
  441. receive {Self, Pid, takeover, _, _, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  442. ok.
  443. terminate_on_socket_close(Config) ->
  444. doc("Confirm terminate/3 is called when the socket gets closed brutally."),
  445. Self = self(),
  446. ConnPid = gun_open(Config),
  447. Ref = gun:get(ConnPid, "/long_polling", [
  448. {<<"accept-encoding">>, <<"gzip">>},
  449. {<<"x-test-case">>, <<"terminate_on_socket_close">>},
  450. {<<"x-test-pid">>, pid_to_list(Self)}
  451. ]),
  452. %% Confirm init/3 is called and receive the beginning of the response.
  453. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  454. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  455. %% Close the socket.
  456. ok = gun:close(ConnPid),
  457. %% Confirm terminate/3 is called.
  458. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  459. ok.
  460. terminate_on_stop(Config) ->
  461. doc("Confirm terminate/3 is called after stop is returned."),
  462. Self = self(),
  463. ConnPid = gun_open(Config),
  464. Ref = gun:get(ConnPid, "/long_polling", [
  465. {<<"accept-encoding">>, <<"gzip">>},
  466. {<<"x-test-case">>, <<"terminate_on_stop">>},
  467. {<<"x-test-pid">>, pid_to_list(Self)}
  468. ]),
  469. %% Confirm init/3 is called and receive the response.
  470. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  471. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  472. %% Confirm the stream is still alive even though we
  473. %% received the response fully, and tell it to stop.
  474. Pid ! {{Pid, 1}, please_stop},
  475. receive {Self, Pid, info, _, please_stop, _} -> ok after 1000 -> error(timeout) end,
  476. %% Confirm terminate/3 is called.
  477. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  478. ok.
  479. terminate_on_switch_protocol(Config) ->
  480. case config(protocol, Config) of
  481. http -> do_terminate_on_switch_protocol(Config);
  482. http2 -> doc("The switch_protocol command is not currently supported for HTTP/2.")
  483. end.
  484. do_terminate_on_switch_protocol(Config) ->
  485. doc("Confirm terminate/3 is called after switch_protocol is returned."),
  486. Self = self(),
  487. ConnPid = gun_open(Config),
  488. Ref = gun:get(ConnPid, "/long_polling", [
  489. {<<"accept-encoding">>, <<"gzip">>},
  490. {<<"x-test-case">>, <<"terminate_on_switch_protocol">>},
  491. {<<"x-test-pid">>, pid_to_list(Self)}
  492. ]),
  493. %% Confirm init/3 is called and receive the response.
  494. Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
  495. {inform, 101, _} = gun:await(ConnPid, Ref),
  496. %% Confirm terminate/3 is called.
  497. receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
  498. %% Confirm takeover/7 is called.
  499. receive {Self, Pid, takeover, _, _, _, _, _, _, _} -> ok after 1000 -> error(timeout) end,
  500. ok.