http_SUITE.erl 36 KB

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