http_SUITE.erl 36 KB

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