http_SUITE.erl 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. %% Copyright (c) 2011-2014, Loïc Hoguin <essen@ninenines.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(http_SUITE).
  16. -compile(export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(cowboy_test, [gun_open/1]).
  19. -import(cowboy_test, [gun_open/2]).
  20. -import(cowboy_test, [gun_down/1]).
  21. -import(cowboy_test, [raw_open/1]).
  22. -import(cowboy_test, [raw_send/2]).
  23. -import(cowboy_test, [raw_recv_head/1]).
  24. -import(cowboy_test, [raw_expect_recv/2]).
  25. %% ct.
  26. all() ->
  27. [
  28. {group, http},
  29. {group, https},
  30. {group, http_compress},
  31. {group, https_compress},
  32. {group, onresponse},
  33. {group, onresponse_capitalize},
  34. {group, parse_host},
  35. {group, set_env}
  36. ].
  37. groups() ->
  38. Tests = ct_helper:all(?MODULE) -- [
  39. onresponse_crash, onresponse_reply, onresponse_capitalize,
  40. parse_host, set_env_dispatch
  41. ],
  42. [
  43. {http, [parallel], Tests},
  44. {https, [parallel], Tests},
  45. {http_compress, [parallel], Tests},
  46. {https_compress, [parallel], Tests},
  47. {onresponse, [parallel], [
  48. onresponse_crash,
  49. onresponse_reply
  50. ]},
  51. {onresponse_capitalize, [parallel], [
  52. onresponse_capitalize
  53. ]},
  54. {parse_host, [], [
  55. parse_host
  56. ]},
  57. {set_env, [], [
  58. set_env_dispatch
  59. ]}
  60. ].
  61. init_per_suite(Config) ->
  62. Dir = config(priv_dir, Config) ++ "/static",
  63. ct_helper:create_static_dir(Dir),
  64. [{static_dir, Dir}|Config].
  65. end_per_suite(Config) ->
  66. ct_helper:delete_static_dir(config(static_dir, Config)).
  67. init_per_group(Name = http, Config) ->
  68. cowboy_test:init_http(Name, [
  69. {env, [{dispatch, init_dispatch(Config)}]}
  70. ], Config);
  71. init_per_group(Name = https, Config) ->
  72. cowboy_test:init_https(Name, [
  73. {env, [{dispatch, init_dispatch(Config)}]}
  74. ], Config);
  75. init_per_group(Name = http_compress, Config) ->
  76. cowboy_test:init_http(Name, [
  77. {env, [{dispatch, init_dispatch(Config)}]},
  78. {compress, true}
  79. ], Config);
  80. init_per_group(Name = https_compress, Config) ->
  81. cowboy_test:init_https(Name, [
  82. {env, [{dispatch, init_dispatch(Config)}]},
  83. {compress, true}
  84. ], Config);
  85. %% Most, if not all of these, should be in separate test suites.
  86. init_per_group(onresponse, Config) ->
  87. {ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
  88. {env, [{dispatch, init_dispatch(Config)}]},
  89. {onresponse, fun do_onresponse_hook/4}
  90. ]),
  91. Port = ranch:get_port(onresponse),
  92. [{type, tcp}, {port, Port}, {opts, []}|Config];
  93. init_per_group(onresponse_capitalize, Config) ->
  94. {ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
  95. {env, [{dispatch, init_dispatch(Config)}]},
  96. {onresponse, fun do_onresponse_capitalize_hook/4}
  97. ]),
  98. Port = ranch:get_port(onresponse_capitalize),
  99. [{type, tcp}, {port, Port}, {opts, []}|Config];
  100. init_per_group(parse_host, Config) ->
  101. Dispatch = cowboy_router:compile([
  102. {'_', [
  103. {"/req_attr", http_req_attr, []}
  104. ]}
  105. ]),
  106. {ok, _} = cowboy:start_http(parse_host, 100, [{port, 0}], [
  107. {env, [{dispatch, Dispatch}]}
  108. ]),
  109. Port = ranch:get_port(parse_host),
  110. [{type, tcp}, {port, Port}, {opts, []}|Config];
  111. init_per_group(set_env, Config) ->
  112. {ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
  113. {env, [{dispatch, []}]}
  114. ]),
  115. Port = ranch:get_port(set_env),
  116. [{type, tcp}, {port, Port}, {opts, []}|Config].
  117. end_per_group(Name, _) ->
  118. ok = cowboy:stop_listener(Name).
  119. %% Dispatch configuration.
  120. init_dispatch(Config) ->
  121. cowboy_router:compile([
  122. {"localhost", [
  123. {"/chunked_response", http_chunked, []},
  124. {"/streamed_response", http_streamed, []},
  125. {"/headers/dupe", http_handler,
  126. [{headers, [{<<"connection">>, <<"close">>}]}]},
  127. {"/set_resp/header", http_set_resp,
  128. [{headers, [{<<"vary">>, <<"Accept">>}]}]},
  129. {"/set_resp/overwrite", http_set_resp,
  130. [{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
  131. {"/set_resp/body", http_set_resp,
  132. [{body, <<"A flameless dance does not equal a cycle">>}]},
  133. {"/stream_body/set_resp", http_stream_body,
  134. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  135. {"/stream_body/set_resp_close",
  136. http_stream_body, [
  137. {reply, set_resp_close},
  138. {body, <<"stream_body_set_resp_close">>}]},
  139. {"/stream_body/set_resp_chunked",
  140. http_stream_body, [
  141. {reply, set_resp_chunked},
  142. {body, [<<"stream_body">>, <<"_set_resp_chunked">>]}]},
  143. {"/static/[...]", cowboy_static,
  144. {dir, config(static_dir, Config)}},
  145. {"/static_mimetypes_function/[...]", cowboy_static,
  146. {dir, config(static_dir, Config),
  147. [{mimetypes, ?MODULE, do_mimetypes_text_html}]}},
  148. {"/handler_errors", http_errors, []},
  149. {"/static_attribute_etag/[...]", cowboy_static,
  150. {dir, config(static_dir, Config)}},
  151. {"/static_function_etag/[...]", cowboy_static,
  152. {dir, config(static_dir, Config),
  153. [{etag, ?MODULE, do_etag_gen}]}},
  154. {"/static_specify_file/[...]", cowboy_static,
  155. {file, config(static_dir, Config) ++ "/style.css"}},
  156. {"/multipart", http_multipart, []},
  157. {"/multipart/large", http_multipart_stream, []},
  158. {"/echo/body", http_echo_body, []},
  159. {"/echo/body_qs", http_body_qs, []},
  160. {"/crash/content-length", input_crash_h, content_length},
  161. {"/param_all", rest_param_all, []},
  162. {"/bad_accept", rest_simple_resource, []},
  163. {"/bad_content_type", rest_patch_resource, []},
  164. {"/simple", rest_simple_resource, []},
  165. {"/forbidden_post", rest_forbidden_resource, [true]},
  166. {"/simple_post", rest_forbidden_resource, [false]},
  167. {"/missing_get_callbacks", rest_missing_callbacks, []},
  168. {"/missing_put_callbacks", rest_missing_callbacks, []},
  169. {"/nodelete", rest_nodelete_resource, []},
  170. {"/post_charset", rest_post_charset_resource, []},
  171. {"/postonly", rest_postonly_resource, []},
  172. {"/patch", rest_patch_resource, []},
  173. {"/resetags", rest_resource_etags, []},
  174. {"/rest_expires", rest_expires, []},
  175. {"/rest_expires_binary", rest_expires_binary, []},
  176. {"/rest_empty_resource", rest_empty_resource, []},
  177. {"/loop_stream_recv", http_loop_stream_recv, []},
  178. {"/", http_handler, []}
  179. ]}
  180. ]).
  181. %% Callbacks.
  182. do_etag_gen(_, _, _) ->
  183. {strong, <<"etag">>}.
  184. do_mimetypes_text_html(_) ->
  185. <<"text/html">>.
  186. %% Convenience functions.
  187. do_raw(Data, Config) ->
  188. Client = raw_open(Config),
  189. ok = raw_send(Client, Data),
  190. case catch raw_recv_head(Client) of
  191. {'EXIT', _} -> closed;
  192. Resp -> element(2, cow_http:parse_status_line(Resp))
  193. end.
  194. do_get(Path, Config) ->
  195. ConnPid = gun_open(Config),
  196. Ref = gun:get(ConnPid, Path),
  197. {response, _, Status, _} = gun:await(ConnPid, Ref),
  198. gun:close(ConnPid),
  199. Status.
  200. %% Tests.
  201. check_raw_status(Config) ->
  202. Huge = [$0 || _ <- lists:seq(1, 5000)],
  203. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  204. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  205. ResponsePacket =
  206. "HTTP/1.0 302 Found\r
  207. Location: http://www.google.co.il/\r
  208. Cache-Control: private\r
  209. Content-Type: text/html; charset=UTF-8\r
  210. Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com\r
  211. Date: Sun, 04 Dec 2011 15:55:01 GMT\r
  212. Server: gws\r
  213. Content-Length: 221\r
  214. X-XSS-Protection: 1; mode=block\r
  215. X-Frame-Options: SAMEORIGIN\r
  216. \r
  217. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  218. <TITLE>302 Moved</TITLE></HEAD><BODY>
  219. <H1>302 Moved</H1>
  220. The document has moved
  221. <A HREF=\"http://www.google.co.il/\">here</A>.
  222. </BODY></HTML>",
  223. Tests = [
  224. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  225. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  226. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  227. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  228. {400, "\n"},
  229. {400, "Garbage\r\n\r\n"},
  230. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  231. {400, " / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  232. {400, "GET HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  233. {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
  234. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  235. {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
  236. {400, ["POST /crash/content-length HTTP/1.1\r\nHost: localhost\r\nContent-Length: 5000,5000\r\n\r\n", Huge]},
  237. {505, ResponsePacket},
  238. {408, "GET / HTTP/1.1\r\n"},
  239. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  240. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  241. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  242. {414, Huge},
  243. {400, "GET / HTTP/1.1\r\n" ++ Huge},
  244. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  245. {closed, ""},
  246. {closed, "\r\n"},
  247. {closed, "\r\n\r\n"},
  248. {closed, "GET / HTTP/1.1"}
  249. ],
  250. _ = [{Status, Packet} = begin
  251. Ret = do_raw(Packet, Config),
  252. {Ret, Packet}
  253. end || {Status, Packet} <- Tests],
  254. ok.
  255. check_status(Config) ->
  256. Tests = [
  257. {200, "/"},
  258. {200, "/simple"},
  259. {400, "/static/%2f"},
  260. {400, "/static/%2e"},
  261. {400, "/static/%2e%2e"},
  262. {403, "/static/directory"},
  263. {403, "/static/directory/"},
  264. {403, "/static/unreadable"},
  265. {404, "/not/found"},
  266. {404, "/static/not_found"},
  267. {500, "/handler_errors?case=init_before_reply"}
  268. ],
  269. _ = [{Status, URL} = begin
  270. Ret = do_get(URL, Config),
  271. {Ret, URL}
  272. end || {Status, URL} <- Tests].
  273. chunked_response(Config) ->
  274. ConnPid = gun_open(Config),
  275. Ref = gun:get(ConnPid, "/chunked_response"),
  276. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  277. true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  278. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
  279. ok.
  280. %% Check if sending requests whose size is around the MTU breaks something.
  281. echo_body(Config) ->
  282. MTU = ct_helper:get_loopback_mtu(),
  283. _ = [begin
  284. Body = list_to_binary(lists:duplicate(Size, $a)),
  285. ConnPid = gun_open(Config),
  286. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  287. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  288. {ok, Body} = gun:await_body(ConnPid, Ref)
  289. end || Size <- lists:seq(MTU - 500, MTU)],
  290. ok.
  291. %% Check if sending request whose size is bigger than 1000000 bytes causes 413
  292. echo_body_max_length(Config) ->
  293. ConnPid = gun_open(Config),
  294. Ref = gun:post(ConnPid, "/echo/body", [], << 0:2000000/unit:8 >>),
  295. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  296. ok.
  297. % check if body_qs echo's back results
  298. echo_body_qs(Config) ->
  299. ConnPid = gun_open(Config),
  300. Ref = gun:post(ConnPid, "/echo/body_qs", [], <<"echo=67890">>),
  301. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  302. {ok, <<"67890">>} = gun:await_body(ConnPid, Ref),
  303. ok.
  304. echo_body_qs_max_length(Config) ->
  305. ConnPid = gun_open(Config),
  306. Ref = gun:post(ConnPid, "/echo/body_qs", [], << "echo=", 0:2000000/unit:8 >>),
  307. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  308. ok.
  309. error_init_after_reply(Config) ->
  310. ConnPid = gun_open(Config),
  311. Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
  312. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  313. gun_down(ConnPid).
  314. headers_dupe(Config) ->
  315. ConnPid = gun_open(Config),
  316. Ref = gun:get(ConnPid, "/headers/dupe"),
  317. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  318. %% Ensure that only one connection header was received.
  319. [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
  320. gun_down(ConnPid).
  321. http10_chunkless(Config) ->
  322. ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
  323. Ref = gun:get(ConnPid, "/chunked_response"),
  324. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  325. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  326. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
  327. gun_down(ConnPid).
  328. http10_hostless(Config) ->
  329. Name = http10_hostless,
  330. Port10 = config(port, Config) + 10,
  331. Transport = case config(type, Config) of
  332. tcp -> ranch_tcp;
  333. ssl -> ranch_ssl
  334. end,
  335. ranch:start_listener(Name, 5, Transport,
  336. config(opts, Config) ++ [{port, Port10}],
  337. cowboy_protocol, [
  338. {env, [{dispatch, cowboy_router:compile([
  339. {'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
  340. {max_keepalive, 50},
  341. {timeout, 500}]
  342. ),
  343. 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  344. [{port, Port10}|Config]),
  345. cowboy:stop_listener(http10_hostless).
  346. http10_keepalive_default(Config) ->
  347. Normal = "GET / HTTP/1.0\r\nhost: localhost\r\n\r\n",
  348. Client = raw_open(Config),
  349. ok = raw_send(Client, Normal),
  350. case catch raw_recv_head(Client) of
  351. {'EXIT', _} -> error(closed);
  352. _ -> ok
  353. end,
  354. ok = raw_send(Client, Normal),
  355. case catch raw_recv_head(Client) of
  356. {'EXIT', _} -> closed;
  357. _ -> error(not_closed)
  358. end.
  359. http10_keepalive_forced(Config) ->
  360. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  361. Client = raw_open(Config),
  362. ok = raw_send(Client, Keepalive),
  363. case catch raw_recv_head(Client) of
  364. {'EXIT', _} -> error(closed);
  365. _ -> ok
  366. end,
  367. ok = raw_send(Client, Keepalive),
  368. case catch raw_recv_head(Client) of
  369. {'EXIT', Err} -> error({closed, Err});
  370. _ -> ok
  371. end.
  372. keepalive_max(Config) ->
  373. ConnPid = gun_open(Config),
  374. Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
  375. || _ <- lists:seq(1, 99)],
  376. CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  377. _ = [begin
  378. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  379. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  380. end || Ref <- Refs],
  381. {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef),
  382. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  383. gun_down(ConnPid).
  384. keepalive_nl(Config) ->
  385. ConnPid = gun_open(Config),
  386. Refs = [begin
  387. Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  388. gun:dbg_send_raw(ConnPid, <<"\r\n">>),
  389. Ref
  390. end || _ <- lists:seq(1, 10)],
  391. _ = [begin
  392. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  393. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  394. end || Ref <- Refs],
  395. ok.
  396. keepalive_stream_loop(Config) ->
  397. ConnPid = gun_open(Config),
  398. Refs = [begin
  399. Ref = gun:post(ConnPid, "/loop_stream_recv",
  400. [{<<"content-type">>, <<"application/octet-stream">>}]),
  401. _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
  402. || ID <- lists:seq(1, 250)],
  403. gun:data(ConnPid, Ref, fin, <<>>),
  404. Ref
  405. end || _ <- lists:seq(1, 10)],
  406. _ = [begin
  407. {response, fin, 200, _} = gun:await(ConnPid, Ref)
  408. end || Ref <- Refs],
  409. ok.
  410. multipart(Config) ->
  411. ConnPid = gun_open(Config),
  412. Body = <<
  413. "This is a preamble."
  414. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  415. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  416. "\r\n--OHai--\r\n"
  417. "This is an epilogue."
  418. >>,
  419. Ref = gun:post(ConnPid, "/multipart",
  420. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  421. Body),
  422. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  423. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  424. Parts = binary_to_term(RespBody),
  425. Parts = [
  426. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  427. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  428. ],
  429. ok.
  430. multipart_chunked(Config) ->
  431. ConnPid = gun_open(Config),
  432. Body = <<
  433. "This is a preamble."
  434. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  435. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  436. "\r\n--OHai--\r\n"
  437. "This is an epilogue."
  438. >>,
  439. Ref = gun:post(ConnPid, "/multipart",
  440. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}]),
  441. gun:data(ConnPid, Ref, fin, Body),
  442. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  443. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  444. Parts = binary_to_term(RespBody),
  445. Parts = [
  446. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  447. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  448. ],
  449. ok.
  450. multipart_large(Config) ->
  451. ConnPid = gun_open(Config),
  452. Boundary = "----------",
  453. Big = << 0:9000000/unit:8 >>,
  454. Bigger = << 0:9999999/unit:8 >>,
  455. Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n",
  456. "--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n",
  457. "--", Boundary, "--\r\n"],
  458. Ref = gun:post(ConnPid, "/multipart/large",
  459. [{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}],
  460. Body),
  461. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  462. ok.
  463. do_nc(Config, Input) ->
  464. Cat = os:find_executable("cat"),
  465. Nc = os:find_executable("nc"),
  466. case {Cat, Nc} of
  467. {false, _} ->
  468. {skip, {notfound, cat}};
  469. {_, false} ->
  470. {skip, {notfound, nc}};
  471. _Good ->
  472. %% Throw garbage at the server then check if it's still up.
  473. StrPort = integer_to_list(config(port, Config)),
  474. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  475. || _ <- lists:seq(1, 100)],
  476. 200 = do_get("/", Config)
  477. end.
  478. nc_rand(Config) ->
  479. do_nc(Config, "/dev/urandom").
  480. nc_zero(Config) ->
  481. do_nc(Config, "/dev/zero").
  482. onresponse_capitalize(Config) ->
  483. Client = raw_open(Config),
  484. ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  485. Data = raw_recv_head(Client),
  486. false = nomatch =:= binary:match(Data, <<"Content-Length">>),
  487. ok.
  488. %% Hook for the above onresponse_capitalize test.
  489. do_onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  490. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  491. || {N, V} <- Headers],
  492. cowboy_req:reply(Status, Headers2, Body, Req).
  493. onresponse_crash(Config) ->
  494. ConnPid = gun_open(Config),
  495. Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"),
  496. {response, fin, 777, Headers} = gun:await(ConnPid, Ref),
  497. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  498. onresponse_reply(Config) ->
  499. ConnPid = gun_open(Config),
  500. Ref = gun:get(ConnPid, "/"),
  501. {response, nofin, 777, Headers} = gun:await(ConnPid, Ref),
  502. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  503. ok.
  504. %% Hook for the above onresponse tests.
  505. do_onresponse_hook(_, Headers, _, Req) ->
  506. cowboy_req:reply(<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req).
  507. parse_host(Config) ->
  508. ConnPid = gun_open(Config),
  509. Tests = [
  510. {<<"example.org:8080">>, <<"example.org\n8080">>},
  511. {<<"example.org">>, <<"example.org\n80">>},
  512. {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
  513. {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
  514. {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
  515. {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
  516. {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
  517. {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
  518. ],
  519. [begin
  520. Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
  521. [{<<"host">>, Host}]),
  522. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  523. {ok, Body} = gun:await_body(ConnPid, Ref)
  524. end || {Host, Body} <- Tests],
  525. ok.
  526. pipeline(Config) ->
  527. ConnPid = gun_open(Config),
  528. Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
  529. _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  530. ok.
  531. rest_param_all(Config) ->
  532. ConnPid = gun_open(Config),
  533. %% Accept without param.
  534. Ref1 = gun:get(ConnPid, "/param_all",
  535. [{<<"accept">>, <<"text/plain">>}]),
  536. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  537. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  538. %% Accept with param.
  539. Ref2 = gun:get(ConnPid, "/param_all",
  540. [{<<"accept">>, <<"text/plain;level=1">>}]),
  541. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  542. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  543. %% Accept with param and quality.
  544. Ref3 = gun:get(ConnPid, "/param_all",
  545. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  546. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  547. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  548. Ref4 = gun:get(ConnPid, "/param_all",
  549. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  550. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  551. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  552. %% Without Accept.
  553. Ref5 = gun:get(ConnPid, "/param_all"),
  554. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  555. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  556. %% Content-Type without param.
  557. Ref6 = gun:put(ConnPid, "/param_all",
  558. [{<<"content-type">>, <<"text/plain">>}]),
  559. gun:data(ConnPid, Ref6, fin, "Hello world!"),
  560. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  561. %% Content-Type with param.
  562. Ref7 = gun:put(ConnPid, "/param_all",
  563. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  564. gun:data(ConnPid, Ref7, fin, "Hello world!"),
  565. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  566. ok.
  567. rest_bad_accept(Config) ->
  568. ConnPid = gun_open(Config),
  569. Ref = gun:get(ConnPid, "/bad_accept",
  570. [{<<"accept">>, <<"1">>}]),
  571. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  572. ok.
  573. rest_bad_content_type(Config) ->
  574. ConnPid = gun_open(Config),
  575. Ref = gun:patch(ConnPid, "/bad_content_type",
  576. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  577. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  578. ok.
  579. rest_expires(Config) ->
  580. ConnPid = gun_open(Config),
  581. Ref = gun:get(ConnPid, "/rest_expires"),
  582. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  583. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  584. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  585. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  586. ok.
  587. rest_expires_binary(Config) ->
  588. ConnPid = gun_open(Config),
  589. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  590. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  591. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  592. ok.
  593. rest_keepalive(Config) ->
  594. ConnPid = gun_open(Config),
  595. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  596. _ = [begin
  597. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  598. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  599. end || Ref <- Refs],
  600. ok.
  601. rest_keepalive_post(Config) ->
  602. ConnPid = gun_open(Config),
  603. Refs = [begin
  604. Ref1 = gun:post(ConnPid, "/forbidden_post", [{<<"content-type">>, <<"text/plain">>}]),
  605. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  606. Ref2 = gun:post(ConnPid, "/simple_post", [{<<"content-type">>, <<"text/plain">>}]),
  607. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  608. {Ref1, Ref2}
  609. end || _ <- lists:seq(1, 5)],
  610. _ = [begin
  611. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  612. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
  613. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  614. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers2)
  615. end || {Ref1, Ref2} <- Refs],
  616. ok.
  617. rest_missing_get_callbacks(Config) ->
  618. ConnPid = gun_open(Config),
  619. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  620. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  621. ok.
  622. rest_missing_put_callbacks(Config) ->
  623. ConnPid = gun_open(Config),
  624. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  625. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  626. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  627. ok.
  628. rest_nodelete(Config) ->
  629. ConnPid = gun_open(Config),
  630. Ref = gun:delete(ConnPid, "/nodelete"),
  631. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  632. ok.
  633. rest_options_default(Config) ->
  634. ConnPid = gun_open(Config),
  635. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  636. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  637. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  638. ok.
  639. rest_patch(Config) ->
  640. Tests = [
  641. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  642. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  643. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  644. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  645. ],
  646. ConnPid = gun_open(Config),
  647. _ = [begin
  648. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  649. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  650. end || {Status, Headers, Body} <- Tests],
  651. ok.
  652. rest_post_charset(Config) ->
  653. ConnPid = gun_open(Config),
  654. Ref = gun:post(ConnPid, "/post_charset",
  655. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  656. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  657. ok.
  658. rest_postonly(Config) ->
  659. ConnPid = gun_open(Config),
  660. Ref = gun:post(ConnPid, "/postonly",
  661. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  662. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  663. ok.
  664. rest_resource_get_etag(Config, Type) ->
  665. rest_resource_get_etag(Config, Type, []).
  666. rest_resource_get_etag(Config, Type, Headers) ->
  667. ConnPid = gun_open(Config),
  668. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  669. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  670. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  671. false -> {Status, false};
  672. {<<"etag">>, ETag} -> {Status, ETag}
  673. end.
  674. rest_resource_etags(Config) ->
  675. Tests = [
  676. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  677. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  678. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  679. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  680. {400, false, "binary-strong-unquoted"},
  681. {400, false, "binary-weak-unquoted"}
  682. ],
  683. _ = [{Status, ETag, Type} = begin
  684. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  685. {Ret, RespETag, Type}
  686. end || {Status, ETag, Type} <- Tests].
  687. rest_resource_etags_if_none_match(Config) ->
  688. Tests = [
  689. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  690. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  691. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  692. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  693. ],
  694. _ = [{Status, Type} = begin
  695. {Ret, _} = rest_resource_get_etag(Config, Type,
  696. [{<<"if-none-match">>, ETag}]),
  697. {Ret, Type}
  698. end || {Status, ETag, Type} <- Tests].
  699. set_env_dispatch(Config) ->
  700. ConnPid1 = gun_open(Config),
  701. Ref1 = gun:get(ConnPid1, "/"),
  702. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  703. ok = cowboy:set_env(set_env, dispatch,
  704. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  705. ConnPid2 = gun_open(Config),
  706. Ref2 = gun:get(ConnPid2, "/"),
  707. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  708. ok.
  709. set_resp_body(Config) ->
  710. ConnPid = gun_open(Config),
  711. Ref = gun:get(ConnPid, "/set_resp/body"),
  712. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  713. {ok, <<"A flameless dance does not equal a cycle">>}
  714. = gun:await_body(ConnPid, Ref),
  715. ok.
  716. set_resp_header(Config) ->
  717. ConnPid = gun_open(Config),
  718. Ref = gun:get(ConnPid, "/set_resp/header"),
  719. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  720. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  721. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  722. ok.
  723. set_resp_overwrite(Config) ->
  724. ConnPid = gun_open(Config),
  725. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  726. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  727. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  728. ok.
  729. slowloris(Config) ->
  730. Client = raw_open(Config),
  731. try
  732. [begin
  733. ok = raw_send(Client, [C]),
  734. receive after 250 -> ok end
  735. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  736. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  737. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  738. error(failure)
  739. catch error:{badmatch, _} ->
  740. ok
  741. end.
  742. slowloris2(Config) ->
  743. Client = raw_open(Config),
  744. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  745. receive after 300 -> ok end,
  746. ok = raw_send(Client, "Host: localhost\r\n"),
  747. receive after 300 -> ok end,
  748. Data = raw_recv_head(Client),
  749. {_, 408, _, _} = cow_http:parse_status_line(Data),
  750. ok.
  751. static_attribute_etag(Config) ->
  752. ConnPid = gun_open(Config),
  753. Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  754. Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  755. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  756. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  757. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  758. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  759. true = ETag =/= undefined,
  760. ok.
  761. static_function_etag(Config) ->
  762. ConnPid = gun_open(Config),
  763. Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
  764. Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
  765. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  766. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  767. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  768. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  769. true = ETag =/= undefined,
  770. ok.
  771. static_mimetypes_function(Config) ->
  772. ConnPid = gun_open(Config),
  773. Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
  774. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  775. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  776. ok.
  777. static_specify_file(Config) ->
  778. ConnPid = gun_open(Config),
  779. Ref = gun:get(ConnPid, "/static_specify_file"),
  780. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  781. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  782. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  783. ok.
  784. static_specify_file_catchall(Config) ->
  785. ConnPid = gun_open(Config),
  786. Ref = gun:get(ConnPid, "/static_specify_file/none"),
  787. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  788. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  789. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  790. ok.
  791. static_test_file(Config) ->
  792. ConnPid = gun_open(Config),
  793. Ref = gun:get(ConnPid, "/static/unknown"),
  794. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  795. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  796. ok.
  797. static_test_file_css(Config) ->
  798. ConnPid = gun_open(Config),
  799. Ref = gun:get(ConnPid, "/static/style.css"),
  800. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  801. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  802. ok.
  803. stream_body_set_resp(Config) ->
  804. ConnPid = gun_open(Config),
  805. Ref = gun:get(ConnPid, "/stream_body/set_resp"),
  806. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  807. {ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
  808. ok.
  809. stream_body_set_resp_close(Config) ->
  810. ConnPid = gun_open(Config),
  811. Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
  812. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  813. {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref),
  814. gun_down(ConnPid).
  815. stream_body_set_resp_chunked(Config) ->
  816. ConnPid = gun_open(Config),
  817. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  818. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  819. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  820. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  821. ok.
  822. stream_body_set_resp_chunked10(Config) ->
  823. ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
  824. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  825. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  826. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  827. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  828. gun_down(ConnPid).
  829. %% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
  830. streamed_response(Config) ->
  831. Client = raw_open(Config),
  832. ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  833. Data = raw_recv_head(Client),
  834. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  835. {Headers, Rest2} = cow_http:parse_headers(Rest),
  836. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  837. Rest2Size = byte_size(Rest2),
  838. ok = case <<"streamed_handler\r\nworks fine!">> of
  839. Rest2 -> ok;
  840. << Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
  841. end.
  842. te_chunked(Config) ->
  843. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  844. ConnPid = gun_open(Config),
  845. Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]),
  846. gun:data(ConnPid, Ref, fin, Body),
  847. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  848. {ok, Body} = gun:await_body(ConnPid, Ref),
  849. ok.
  850. do_body_to_chunks(_, <<>>, Acc) ->
  851. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  852. do_body_to_chunks(ChunkSize, Body, Acc) ->
  853. BodySize = byte_size(Body),
  854. ChunkSize2 = case BodySize < ChunkSize of
  855. true -> BodySize;
  856. false -> ChunkSize
  857. end,
  858. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  859. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  860. do_body_to_chunks(ChunkSize, Rest,
  861. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  862. te_chunked_chopped(Config) ->
  863. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  864. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  865. ConnPid = gun_open(Config),
  866. Ref = gun:post(ConnPid, "/echo/body",
  867. [{<<"content-type">>, <<"text/plain">>}]),
  868. _ = [begin
  869. ok = gun:dbg_send_raw(ConnPid, << C >>),
  870. receive after 10 -> ok end
  871. end || << C >> <= Body2],
  872. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  873. {ok, Body} = gun:await_body(ConnPid, Ref),
  874. ok.
  875. te_chunked_delayed(Config) ->
  876. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  877. Chunks = do_body_to_chunks(50, Body, []),
  878. ConnPid = gun_open(Config),
  879. Ref = gun:post(ConnPid, "/echo/body",
  880. [{<<"content-type">>, <<"text/plain">>}]),
  881. _ = [begin
  882. ok = gun:dbg_send_raw(ConnPid, Chunk),
  883. receive after 10 -> ok end
  884. end || Chunk <- Chunks],
  885. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  886. {ok, Body} = gun:await_body(ConnPid, Ref),
  887. ok.
  888. te_chunked_split_body(Config) ->
  889. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  890. Chunks = do_body_to_chunks(50, Body, []),
  891. ConnPid = gun_open(Config),
  892. Ref = gun:post(ConnPid, "/echo/body",
  893. [{<<"content-type">>, <<"text/plain">>}]),
  894. _ = [begin
  895. case Chunk of
  896. <<"0\r\n\r\n">> ->
  897. ok = gun:dbg_send_raw(ConnPid, Chunk);
  898. _ ->
  899. [Size, ChunkBody, <<>>] =
  900. binary:split(Chunk, [<<"\r\n">>], [global]),
  901. PartASize = random:uniform(byte_size(ChunkBody)),
  902. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  903. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  904. receive after 10 -> ok end,
  905. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  906. end
  907. end || Chunk <- Chunks],
  908. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  909. {ok, Body} = gun:await_body(ConnPid, Ref),
  910. ok.
  911. te_chunked_split_crlf(Config) ->
  912. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  913. Chunks = do_body_to_chunks(50, Body, []),
  914. ConnPid = gun_open(Config),
  915. Ref = gun:post(ConnPid, "/echo/body",
  916. [{<<"content-type">>, <<"text/plain">>}]),
  917. _ = [begin
  918. %% Split in the newline just before the end of the chunk.
  919. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  920. << Chunk2:Len/binary, End/binary >> = Chunk,
  921. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  922. receive after 10 -> ok end,
  923. ok = gun:dbg_send_raw(ConnPid, End)
  924. end || Chunk <- Chunks],
  925. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  926. {ok, Body} = gun:await_body(ConnPid, Ref),
  927. ok.
  928. te_identity(Config) ->
  929. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  930. ConnPid = gun_open(Config),
  931. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  932. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  933. {ok, Body} = gun:await_body(ConnPid, Ref),
  934. ok.