http_SUITE.erl 33 KB

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