http_SUITE.erl 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. Data ->
  353. {'HTTP/1.0', 200, _, Rest} = cow_http:parse_status_line(Data),
  354. {Headers, _} = cow_http:parse_headers(Rest),
  355. false = lists:keymember(<<"connection">>, 1, Headers)
  356. end,
  357. ok = raw_send(Client, Normal),
  358. case catch raw_recv_head(Client) of
  359. {'EXIT', _} -> closed;
  360. _ -> error(not_closed)
  361. end.
  362. http10_keepalive_forced(Config) ->
  363. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  364. Client = raw_open(Config),
  365. ok = raw_send(Client, Keepalive),
  366. case catch raw_recv_head(Client) of
  367. {'EXIT', _} -> error(closed);
  368. Data ->
  369. {'HTTP/1.0', 200, _, Rest} = cow_http:parse_status_line(Data),
  370. {Headers, _} = cow_http:parse_headers(Rest),
  371. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  372. end,
  373. ok = raw_send(Client, Keepalive),
  374. case catch raw_recv_head(Client) of
  375. {'EXIT', Err} -> error({closed, Err});
  376. _ -> ok
  377. end.
  378. keepalive_max(Config) ->
  379. ConnPid = gun_open(Config),
  380. Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
  381. || _ <- lists:seq(1, 99)],
  382. CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  383. _ = [begin
  384. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  385. false = lists:keymember(<<"connection">>, 1, Headers)
  386. end || Ref <- Refs],
  387. {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef),
  388. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  389. gun_down(ConnPid).
  390. keepalive_nl(Config) ->
  391. ConnPid = gun_open(Config),
  392. Refs = [begin
  393. Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  394. gun:dbg_send_raw(ConnPid, <<"\r\n">>),
  395. Ref
  396. end || _ <- lists:seq(1, 10)],
  397. _ = [begin
  398. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  399. false = lists:keymember(<<"connection">>, 1, Headers)
  400. end || Ref <- Refs],
  401. ok.
  402. keepalive_stream_loop(Config) ->
  403. ConnPid = gun_open(Config),
  404. Refs = [begin
  405. Ref = gun:post(ConnPid, "/loop_stream_recv",
  406. [{<<"content-type">>, <<"application/octet-stream">>}]),
  407. _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
  408. || ID <- lists:seq(1, 250)],
  409. gun:data(ConnPid, Ref, fin, <<>>),
  410. Ref
  411. end || _ <- lists:seq(1, 10)],
  412. _ = [begin
  413. {response, fin, 200, _} = gun:await(ConnPid, Ref)
  414. end || Ref <- Refs],
  415. ok.
  416. multipart(Config) ->
  417. ConnPid = gun_open(Config),
  418. Body = <<
  419. "This is a preamble."
  420. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  421. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  422. "\r\n--OHai--\r\n"
  423. "This is an epilogue."
  424. >>,
  425. Ref = gun:post(ConnPid, "/multipart",
  426. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  427. Body),
  428. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  429. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  430. Parts = binary_to_term(RespBody),
  431. Parts = [
  432. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  433. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  434. ],
  435. ok.
  436. multipart_chunked(Config) ->
  437. ConnPid = gun_open(Config),
  438. Body = <<
  439. "This is a preamble."
  440. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  441. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  442. "\r\n--OHai--\r\n"
  443. "This is an epilogue."
  444. >>,
  445. Ref = gun:post(ConnPid, "/multipart",
  446. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}]),
  447. gun:data(ConnPid, Ref, fin, Body),
  448. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  449. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  450. Parts = binary_to_term(RespBody),
  451. Parts = [
  452. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  453. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  454. ],
  455. ok.
  456. multipart_large(Config) ->
  457. ConnPid = gun_open(Config),
  458. Boundary = "----------",
  459. Big = << 0:9000000/unit:8 >>,
  460. Bigger = << 0:9999999/unit:8 >>,
  461. Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n",
  462. "--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n",
  463. "--", Boundary, "--\r\n"],
  464. Ref = gun:post(ConnPid, "/multipart/large",
  465. [{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}],
  466. Body),
  467. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  468. ok.
  469. do_nc(Config, Input) ->
  470. Cat = os:find_executable("cat"),
  471. Nc = os:find_executable("nc"),
  472. case {Cat, Nc} of
  473. {false, _} ->
  474. {skip, {notfound, cat}};
  475. {_, false} ->
  476. {skip, {notfound, nc}};
  477. _Good ->
  478. %% Throw garbage at the server then check if it's still up.
  479. StrPort = integer_to_list(config(port, Config)),
  480. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  481. || _ <- lists:seq(1, 100)],
  482. 200 = do_get("/", Config)
  483. end.
  484. nc_rand(Config) ->
  485. do_nc(Config, "/dev/urandom").
  486. nc_zero(Config) ->
  487. do_nc(Config, "/dev/zero").
  488. onresponse_capitalize(Config) ->
  489. Client = raw_open(Config),
  490. ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  491. Data = raw_recv_head(Client),
  492. false = nomatch =:= binary:match(Data, <<"Content-Length">>),
  493. ok.
  494. %% Hook for the above onresponse_capitalize test.
  495. do_onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  496. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  497. || {N, V} <- Headers],
  498. cowboy_req:reply(Status, Headers2, Body, Req).
  499. onresponse_crash(Config) ->
  500. ConnPid = gun_open(Config),
  501. Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"),
  502. {response, fin, 777, Headers} = gun:await(ConnPid, Ref),
  503. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  504. onresponse_reply(Config) ->
  505. ConnPid = gun_open(Config),
  506. Ref = gun:get(ConnPid, "/"),
  507. {response, nofin, 777, Headers} = gun:await(ConnPid, Ref),
  508. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  509. ok.
  510. %% Hook for the above onresponse tests.
  511. do_onresponse_hook(_, Headers, _, Req) ->
  512. cowboy_req:reply(<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req).
  513. parse_host(Config) ->
  514. ConnPid = gun_open(Config),
  515. Tests = [
  516. {<<"example.org:8080">>, <<"example.org\n8080">>},
  517. {<<"example.org">>, <<"example.org\n80">>},
  518. {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
  519. {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
  520. {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
  521. {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
  522. {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
  523. {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
  524. ],
  525. [begin
  526. Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
  527. [{<<"host">>, Host}]),
  528. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  529. {ok, Body} = gun:await_body(ConnPid, Ref)
  530. end || {Host, Body} <- Tests],
  531. ok.
  532. pipeline(Config) ->
  533. ConnPid = gun_open(Config),
  534. Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
  535. _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  536. ok.
  537. rest_param_all(Config) ->
  538. ConnPid = gun_open(Config),
  539. %% Accept without param.
  540. Ref1 = gun:get(ConnPid, "/param_all",
  541. [{<<"accept">>, <<"text/plain">>}]),
  542. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  543. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  544. %% Accept with param.
  545. Ref2 = gun:get(ConnPid, "/param_all",
  546. [{<<"accept">>, <<"text/plain;level=1">>}]),
  547. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  548. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  549. %% Accept with param and quality.
  550. Ref3 = gun:get(ConnPid, "/param_all",
  551. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  552. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  553. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  554. Ref4 = gun:get(ConnPid, "/param_all",
  555. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  556. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  557. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  558. %% Without Accept.
  559. Ref5 = gun:get(ConnPid, "/param_all"),
  560. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  561. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  562. %% Content-Type without param.
  563. Ref6 = gun:put(ConnPid, "/param_all",
  564. [{<<"content-type">>, <<"text/plain">>}]),
  565. gun:data(ConnPid, Ref6, fin, "Hello world!"),
  566. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  567. %% Content-Type with param.
  568. Ref7 = gun:put(ConnPid, "/param_all",
  569. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  570. gun:data(ConnPid, Ref7, fin, "Hello world!"),
  571. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  572. ok.
  573. rest_bad_accept(Config) ->
  574. ConnPid = gun_open(Config),
  575. Ref = gun:get(ConnPid, "/bad_accept",
  576. [{<<"accept">>, <<"1">>}]),
  577. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  578. ok.
  579. rest_bad_content_type(Config) ->
  580. ConnPid = gun_open(Config),
  581. Ref = gun:patch(ConnPid, "/bad_content_type",
  582. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  583. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  584. ok.
  585. rest_expires(Config) ->
  586. ConnPid = gun_open(Config),
  587. Ref = gun:get(ConnPid, "/rest_expires"),
  588. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  589. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  590. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  591. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  592. ok.
  593. rest_expires_binary(Config) ->
  594. ConnPid = gun_open(Config),
  595. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  596. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  597. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  598. ok.
  599. rest_keepalive(Config) ->
  600. ConnPid = gun_open(Config),
  601. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  602. _ = [begin
  603. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  604. false = lists:keymember(<<"connection">>, 1, Headers)
  605. end || Ref <- Refs],
  606. ok.
  607. rest_keepalive_post(Config) ->
  608. ConnPid = gun_open(Config),
  609. Refs = [begin
  610. Ref1 = gun:post(ConnPid, "/forbidden_post", [{<<"content-type">>, <<"text/plain">>}]),
  611. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  612. Ref2 = gun:post(ConnPid, "/simple_post", [{<<"content-type">>, <<"text/plain">>}]),
  613. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  614. {Ref1, Ref2}
  615. end || _ <- lists:seq(1, 5)],
  616. _ = [begin
  617. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  618. false = lists:keymember(<<"connection">>, 1, Headers1),
  619. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  620. false = lists:keymember(<<"connection">>, 1, Headers2)
  621. end || {Ref1, Ref2} <- Refs],
  622. ok.
  623. rest_missing_get_callbacks(Config) ->
  624. ConnPid = gun_open(Config),
  625. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  626. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  627. ok.
  628. rest_missing_put_callbacks(Config) ->
  629. ConnPid = gun_open(Config),
  630. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  631. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  632. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  633. ok.
  634. rest_nodelete(Config) ->
  635. ConnPid = gun_open(Config),
  636. Ref = gun:delete(ConnPid, "/nodelete"),
  637. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  638. ok.
  639. rest_options_default(Config) ->
  640. ConnPid = gun_open(Config),
  641. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  642. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  643. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  644. ok.
  645. rest_patch(Config) ->
  646. Tests = [
  647. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  648. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  649. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  650. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  651. ],
  652. ConnPid = gun_open(Config),
  653. _ = [begin
  654. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  655. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  656. end || {Status, Headers, Body} <- Tests],
  657. ok.
  658. rest_post_charset(Config) ->
  659. ConnPid = gun_open(Config),
  660. Ref = gun:post(ConnPid, "/post_charset",
  661. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  662. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  663. ok.
  664. rest_postonly(Config) ->
  665. ConnPid = gun_open(Config),
  666. Ref = gun:post(ConnPid, "/postonly",
  667. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  668. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  669. ok.
  670. rest_resource_get_etag(Config, Type) ->
  671. rest_resource_get_etag(Config, Type, []).
  672. rest_resource_get_etag(Config, Type, Headers) ->
  673. ConnPid = gun_open(Config),
  674. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  675. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  676. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  677. false -> {Status, false};
  678. {<<"etag">>, ETag} -> {Status, ETag}
  679. end.
  680. rest_resource_etags(Config) ->
  681. Tests = [
  682. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  683. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  684. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  685. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  686. {400, false, "binary-strong-unquoted"},
  687. {400, false, "binary-weak-unquoted"}
  688. ],
  689. _ = [{Status, ETag, Type} = begin
  690. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  691. {Ret, RespETag, Type}
  692. end || {Status, ETag, Type} <- Tests].
  693. rest_resource_etags_if_none_match(Config) ->
  694. Tests = [
  695. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  696. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  697. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  698. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  699. ],
  700. _ = [{Status, Type} = begin
  701. {Ret, _} = rest_resource_get_etag(Config, Type,
  702. [{<<"if-none-match">>, ETag}]),
  703. {Ret, Type}
  704. end || {Status, ETag, Type} <- Tests].
  705. set_env_dispatch(Config) ->
  706. ConnPid1 = gun_open(Config),
  707. Ref1 = gun:get(ConnPid1, "/"),
  708. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  709. ok = cowboy:set_env(set_env, dispatch,
  710. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  711. ConnPid2 = gun_open(Config),
  712. Ref2 = gun:get(ConnPid2, "/"),
  713. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  714. ok.
  715. set_resp_body(Config) ->
  716. ConnPid = gun_open(Config),
  717. Ref = gun:get(ConnPid, "/set_resp/body"),
  718. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  719. {ok, <<"A flameless dance does not equal a cycle">>}
  720. = gun:await_body(ConnPid, Ref),
  721. ok.
  722. set_resp_header(Config) ->
  723. ConnPid = gun_open(Config),
  724. Ref = gun:get(ConnPid, "/set_resp/header"),
  725. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  726. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  727. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  728. ok.
  729. set_resp_overwrite(Config) ->
  730. ConnPid = gun_open(Config),
  731. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  732. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  733. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  734. ok.
  735. slowloris(Config) ->
  736. Client = raw_open(Config),
  737. try
  738. [begin
  739. ok = raw_send(Client, [C]),
  740. receive after 250 -> ok end
  741. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  742. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  743. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  744. error(failure)
  745. catch error:{badmatch, _} ->
  746. ok
  747. end.
  748. slowloris2(Config) ->
  749. Client = raw_open(Config),
  750. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  751. receive after 300 -> ok end,
  752. ok = raw_send(Client, "Host: localhost\r\n"),
  753. receive after 300 -> ok end,
  754. Data = raw_recv_head(Client),
  755. {_, 408, _, _} = cow_http:parse_status_line(Data),
  756. ok.
  757. static_attribute_etag(Config) ->
  758. ConnPid = gun_open(Config),
  759. Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  760. Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  761. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  762. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  763. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  764. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  765. true = ETag =/= undefined,
  766. ok.
  767. static_function_etag(Config) ->
  768. ConnPid = gun_open(Config),
  769. Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
  770. Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
  771. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  772. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  773. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  774. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  775. true = ETag =/= undefined,
  776. ok.
  777. static_mimetypes_function(Config) ->
  778. ConnPid = gun_open(Config),
  779. Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
  780. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  781. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  782. ok.
  783. static_specify_file(Config) ->
  784. ConnPid = gun_open(Config),
  785. Ref = gun:get(ConnPid, "/static_specify_file"),
  786. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  787. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  788. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  789. ok.
  790. static_specify_file_catchall(Config) ->
  791. ConnPid = gun_open(Config),
  792. Ref = gun:get(ConnPid, "/static_specify_file/none"),
  793. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  794. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  795. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  796. ok.
  797. static_test_file(Config) ->
  798. ConnPid = gun_open(Config),
  799. Ref = gun:get(ConnPid, "/static/unknown"),
  800. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  801. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  802. ok.
  803. static_test_file_css(Config) ->
  804. ConnPid = gun_open(Config),
  805. Ref = gun:get(ConnPid, "/static/style.css"),
  806. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  807. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  808. ok.
  809. stream_body_set_resp(Config) ->
  810. ConnPid = gun_open(Config),
  811. Ref = gun:get(ConnPid, "/stream_body/set_resp"),
  812. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  813. {ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
  814. ok.
  815. stream_body_set_resp_close(Config) ->
  816. ConnPid = gun_open(Config),
  817. Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
  818. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  819. {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref),
  820. gun_down(ConnPid).
  821. stream_body_set_resp_chunked(Config) ->
  822. ConnPid = gun_open(Config),
  823. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  824. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  825. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  826. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  827. ok.
  828. stream_body_set_resp_chunked10(Config) ->
  829. ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
  830. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  831. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  832. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  833. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  834. gun_down(ConnPid).
  835. %% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
  836. streamed_response(Config) ->
  837. Client = raw_open(Config),
  838. ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  839. Data = raw_recv_head(Client),
  840. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  841. {Headers, Rest2} = cow_http:parse_headers(Rest),
  842. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  843. Rest2Size = byte_size(Rest2),
  844. ok = case <<"streamed_handler\r\nworks fine!">> of
  845. Rest2 -> ok;
  846. << Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
  847. end.
  848. te_chunked(Config) ->
  849. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  850. ConnPid = gun_open(Config),
  851. Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]),
  852. gun:data(ConnPid, Ref, fin, Body),
  853. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  854. {ok, Body} = gun:await_body(ConnPid, Ref),
  855. ok.
  856. do_body_to_chunks(_, <<>>, Acc) ->
  857. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  858. do_body_to_chunks(ChunkSize, Body, Acc) ->
  859. BodySize = byte_size(Body),
  860. ChunkSize2 = case BodySize < ChunkSize of
  861. true -> BodySize;
  862. false -> ChunkSize
  863. end,
  864. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  865. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  866. do_body_to_chunks(ChunkSize, Rest,
  867. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  868. te_chunked_chopped(Config) ->
  869. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  870. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  871. ConnPid = gun_open(Config),
  872. Ref = gun:post(ConnPid, "/echo/body",
  873. [{<<"content-type">>, <<"text/plain">>}]),
  874. _ = [begin
  875. ok = gun:dbg_send_raw(ConnPid, << C >>),
  876. receive after 10 -> ok end
  877. end || << C >> <= Body2],
  878. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  879. {ok, Body} = gun:await_body(ConnPid, Ref),
  880. ok.
  881. te_chunked_delayed(Config) ->
  882. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  883. Chunks = do_body_to_chunks(50, Body, []),
  884. ConnPid = gun_open(Config),
  885. Ref = gun:post(ConnPid, "/echo/body",
  886. [{<<"content-type">>, <<"text/plain">>}]),
  887. _ = [begin
  888. ok = gun:dbg_send_raw(ConnPid, Chunk),
  889. receive after 10 -> ok end
  890. end || Chunk <- Chunks],
  891. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  892. {ok, Body} = gun:await_body(ConnPid, Ref),
  893. ok.
  894. te_chunked_split_body(Config) ->
  895. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  896. Chunks = do_body_to_chunks(50, Body, []),
  897. ConnPid = gun_open(Config),
  898. Ref = gun:post(ConnPid, "/echo/body",
  899. [{<<"content-type">>, <<"text/plain">>}]),
  900. _ = [begin
  901. case Chunk of
  902. <<"0\r\n\r\n">> ->
  903. ok = gun:dbg_send_raw(ConnPid, Chunk);
  904. _ ->
  905. [Size, ChunkBody, <<>>] =
  906. binary:split(Chunk, [<<"\r\n">>], [global]),
  907. PartASize = random:uniform(byte_size(ChunkBody)),
  908. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  909. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  910. receive after 10 -> ok end,
  911. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  912. end
  913. end || Chunk <- Chunks],
  914. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  915. {ok, Body} = gun:await_body(ConnPid, Ref),
  916. ok.
  917. te_chunked_split_crlf(Config) ->
  918. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  919. Chunks = do_body_to_chunks(50, Body, []),
  920. ConnPid = gun_open(Config),
  921. Ref = gun:post(ConnPid, "/echo/body",
  922. [{<<"content-type">>, <<"text/plain">>}]),
  923. _ = [begin
  924. %% Split in the newline just before the end of the chunk.
  925. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  926. << Chunk2:Len/binary, End/binary >> = Chunk,
  927. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  928. receive after 10 -> ok end,
  929. ok = gun:dbg_send_raw(ConnPid, End)
  930. end || Chunk <- Chunks],
  931. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  932. {ok, Body} = gun:await_body(ConnPid, Ref),
  933. ok.
  934. te_identity(Config) ->
  935. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  936. ConnPid = gun_open(Config),
  937. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  938. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  939. {ok, Body} = gun:await_body(ConnPid, Ref),
  940. ok.