http_SUITE.erl 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. %% Copyright (c) 2011-2013, 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. -include_lib("common_test/include/ct.hrl").
  17. %% ct.
  18. -export([all/0]).
  19. -export([groups/0]).
  20. -export([init_per_suite/1]).
  21. -export([end_per_suite/1]).
  22. -export([init_per_group/2]).
  23. -export([end_per_group/2]).
  24. %% Callbacks.
  25. -export([etag_gen/3]).
  26. -export([mimetypes_text_html/1]).
  27. %% Tests.
  28. -export([check_raw_status/1]).
  29. -export([check_status/1]).
  30. -export([chunked_response/1]).
  31. -export([echo_body/1]).
  32. -export([echo_body_max_length/1]).
  33. -export([echo_body_qs/1]).
  34. -export([echo_body_qs_max_length/1]).
  35. -export([error_chain_handle_after_reply/1]).
  36. -export([error_chain_handle_before_reply/1]).
  37. -export([error_handle_after_reply/1]).
  38. -export([error_init_after_reply/1]).
  39. -export([error_init_reply_handle_error/1]).
  40. -export([headers_dupe/1]).
  41. -export([http10_chunkless/1]).
  42. -export([http10_hostless/1]).
  43. -export([keepalive_max/1]).
  44. -export([keepalive_nl/1]).
  45. -export([keepalive_stream_loop/1]).
  46. -export([multipart/1]).
  47. -export([nc_rand/1]).
  48. -export([nc_zero/1]).
  49. -export([onrequest/1]).
  50. -export([onrequest_reply/1]).
  51. -export([onresponse_capitalize/1]).
  52. -export([onresponse_crash/1]).
  53. -export([onresponse_reply/1]).
  54. -export([parse_host/1]).
  55. -export([pipeline/1]).
  56. -export([pipeline_long_polling/1]).
  57. -export([rest_bad_accept/1]).
  58. -export([rest_bad_content_type/1]).
  59. -export([rest_expires/1]).
  60. -export([rest_keepalive/1]).
  61. -export([rest_keepalive_post/1]).
  62. -export([rest_missing_get_callbacks/1]).
  63. -export([rest_missing_put_callbacks/1]).
  64. -export([rest_nodelete/1]).
  65. -export([rest_options_default/1]).
  66. -export([rest_param_all/1]).
  67. -export([rest_patch/1]).
  68. -export([rest_post_charset/1]).
  69. -export([rest_postonly/1]).
  70. -export([rest_resource_etags/1]).
  71. -export([rest_resource_etags_if_none_match/1]).
  72. -export([set_env_dispatch/1]).
  73. -export([set_resp_body/1]).
  74. -export([set_resp_header/1]).
  75. -export([set_resp_overwrite/1]).
  76. -export([slowloris/1]).
  77. -export([slowloris2/1]).
  78. -export([static_attribute_etag/1]).
  79. -export([static_function_etag/1]).
  80. -export([static_mimetypes_function/1]).
  81. -export([static_specify_file/1]).
  82. -export([static_specify_file_catchall/1]).
  83. -export([static_test_file/1]).
  84. -export([static_test_file_css/1]).
  85. -export([stream_body_set_resp/1]).
  86. -export([stream_body_set_resp_close/1]).
  87. -export([stream_body_set_resp_chunked/1]).
  88. -export([stream_body_set_resp_chunked10/1]).
  89. -export([streamed_response/1]).
  90. -export([te_chunked/1]).
  91. -export([te_chunked_chopped/1]).
  92. -export([te_chunked_delayed/1]).
  93. -export([te_chunked_split_body/1]).
  94. -export([te_chunked_split_crlf/1]).
  95. -export([te_identity/1]).
  96. %% ct.
  97. all() ->
  98. [
  99. {group, http},
  100. {group, https},
  101. {group, http_compress},
  102. {group, https_compress},
  103. {group, onrequest},
  104. {group, onresponse},
  105. {group, onresponse_capitalize},
  106. {group, parse_host},
  107. {group, set_env}
  108. ].
  109. groups() ->
  110. Tests = [
  111. check_raw_status,
  112. check_status,
  113. chunked_response,
  114. echo_body,
  115. echo_body_max_length,
  116. echo_body_qs,
  117. echo_body_qs_max_length,
  118. error_chain_handle_after_reply,
  119. error_chain_handle_before_reply,
  120. error_handle_after_reply,
  121. error_init_after_reply,
  122. error_init_reply_handle_error,
  123. headers_dupe,
  124. http10_chunkless,
  125. http10_hostless,
  126. keepalive_max,
  127. keepalive_nl,
  128. keepalive_stream_loop,
  129. multipart,
  130. nc_rand,
  131. nc_zero,
  132. pipeline,
  133. pipeline_long_polling,
  134. rest_bad_accept,
  135. rest_bad_content_type,
  136. rest_expires,
  137. rest_keepalive,
  138. rest_keepalive_post,
  139. rest_missing_get_callbacks,
  140. rest_missing_put_callbacks,
  141. rest_nodelete,
  142. rest_options_default,
  143. rest_param_all,
  144. rest_patch,
  145. rest_post_charset,
  146. rest_postonly,
  147. rest_resource_etags,
  148. rest_resource_etags_if_none_match,
  149. set_resp_body,
  150. set_resp_header,
  151. set_resp_overwrite,
  152. slowloris,
  153. slowloris2,
  154. static_attribute_etag,
  155. static_function_etag,
  156. static_mimetypes_function,
  157. static_specify_file,
  158. static_specify_file_catchall,
  159. static_test_file,
  160. static_test_file_css,
  161. stream_body_set_resp,
  162. stream_body_set_resp_close,
  163. stream_body_set_resp_chunked,
  164. stream_body_set_resp_chunked10,
  165. streamed_response,
  166. te_chunked,
  167. te_chunked_chopped,
  168. te_chunked_delayed,
  169. te_chunked_split_body,
  170. te_chunked_split_crlf,
  171. te_identity
  172. ],
  173. [
  174. {http, [parallel], Tests},
  175. {https, [parallel], Tests},
  176. {http_compress, [parallel], Tests},
  177. {https_compress, [parallel], Tests},
  178. {onrequest, [parallel], [
  179. onrequest,
  180. onrequest_reply
  181. ]},
  182. {onresponse, [parallel], [
  183. onresponse_crash,
  184. onresponse_reply
  185. ]},
  186. {onresponse_capitalize, [parallel], [
  187. onresponse_capitalize
  188. ]},
  189. {parse_host, [], [
  190. parse_host
  191. ]},
  192. {set_env, [], [
  193. set_env_dispatch
  194. ]}
  195. ].
  196. init_per_suite(Config) ->
  197. application:start(crypto),
  198. application:start(cowlib),
  199. application:start(ranch),
  200. application:start(cowboy),
  201. Dir = ?config(priv_dir, Config) ++ "/static",
  202. ct_helper:create_static_dir(Dir),
  203. [{static_dir, Dir}|Config].
  204. end_per_suite(Config) ->
  205. Dir = ?config(static_dir, Config),
  206. ct_helper:delete_static_dir(Dir),
  207. application:stop(cowboy),
  208. application:stop(ranch),
  209. application:stop(cowlib),
  210. application:stop(crypto),
  211. ok.
  212. init_per_group(http, Config) ->
  213. Transport = ranch_tcp,
  214. {ok, _} = cowboy:start_http(http, 100, [{port, 0}], [
  215. {env, [{dispatch, init_dispatch(Config)}]},
  216. {max_keepalive, 50},
  217. {timeout, 500}
  218. ]),
  219. Port = ranch:get_port(http),
  220. {ok, Client} = cowboy_client:init([]),
  221. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  222. {transport, Transport}, {client, Client}|Config];
  223. init_per_group(https, Config) ->
  224. Transport = ranch_ssl,
  225. {_, Cert, Key} = ct_helper:make_certs(),
  226. Opts = [{cert, Cert}, {key, Key}],
  227. application:start(asn1),
  228. application:start(public_key),
  229. application:start(ssl),
  230. {ok, _} = cowboy:start_https(https, 100, Opts ++ [{port, 0}], [
  231. {env, [{dispatch, init_dispatch(Config)}]},
  232. {max_keepalive, 50},
  233. {timeout, 500}
  234. ]),
  235. Port = ranch:get_port(https),
  236. {ok, Client} = cowboy_client:init(Opts),
  237. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  238. {transport, Transport}, {client, Client}|Config];
  239. init_per_group(http_compress, Config) ->
  240. Transport = ranch_tcp,
  241. {ok, _} = cowboy:start_http(http_compress, 100, [{port, 0}], [
  242. {compress, true},
  243. {env, [{dispatch, init_dispatch(Config)}]},
  244. {max_keepalive, 50},
  245. {timeout, 500}
  246. ]),
  247. Port = ranch:get_port(http_compress),
  248. {ok, Client} = cowboy_client:init([]),
  249. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  250. {transport, Transport}, {client, Client}|Config];
  251. init_per_group(https_compress, Config) ->
  252. Transport = ranch_ssl,
  253. {_, Cert, Key} = ct_helper:make_certs(),
  254. Opts = [{cert, Cert}, {key, Key}],
  255. application:start(asn1),
  256. application:start(public_key),
  257. application:start(ssl),
  258. {ok, _} = cowboy:start_https(https_compress, 100, Opts ++ [{port, 0}], [
  259. {compress, true},
  260. {env, [{dispatch, init_dispatch(Config)}]},
  261. {max_keepalive, 50},
  262. {timeout, 500}
  263. ]),
  264. Port = ranch:get_port(https_compress),
  265. {ok, Client} = cowboy_client:init(Opts),
  266. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  267. {transport, Transport}, {client, Client}|Config];
  268. init_per_group(onrequest, Config) ->
  269. Transport = ranch_tcp,
  270. {ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
  271. {env, [{dispatch, init_dispatch(Config)}]},
  272. {max_keepalive, 50},
  273. {onrequest, fun onrequest_hook/1},
  274. {timeout, 500}
  275. ]),
  276. Port = ranch:get_port(onrequest),
  277. {ok, Client} = cowboy_client:init([]),
  278. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  279. {transport, Transport}, {client, Client}|Config];
  280. init_per_group(onresponse, Config) ->
  281. Transport = ranch_tcp,
  282. {ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
  283. {env, [{dispatch, init_dispatch(Config)}]},
  284. {max_keepalive, 50},
  285. {onresponse, fun onresponse_hook/4},
  286. {timeout, 500}
  287. ]),
  288. Port = ranch:get_port(onresponse),
  289. {ok, Client} = cowboy_client:init([]),
  290. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  291. {transport, Transport}, {client, Client}|Config];
  292. init_per_group(onresponse_capitalize, Config) ->
  293. Transport = ranch_tcp,
  294. {ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
  295. {env, [{dispatch, init_dispatch(Config)}]},
  296. {max_keepalive, 50},
  297. {onresponse, fun onresponse_capitalize_hook/4},
  298. {timeout, 500}
  299. ]),
  300. Port = ranch:get_port(onresponse_capitalize),
  301. {ok, Client} = cowboy_client:init([]),
  302. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  303. {transport, Transport}, {client, Client}|Config];
  304. init_per_group(parse_host, Config) ->
  305. Transport = ranch_tcp,
  306. Dispatch = cowboy_router:compile([
  307. {'_', [
  308. {"/req_attr", http_req_attr, []}
  309. ]}
  310. ]),
  311. {ok, _} = cowboy:start_http(http, 100, [{port, 0}], [
  312. {env, [{dispatch, Dispatch}]},
  313. {max_keepalive, 50},
  314. {timeout, 500}
  315. ]),
  316. Port = ranch:get_port(http),
  317. {ok, Client} = cowboy_client:init([]),
  318. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  319. {transport, Transport}, {client, Client}|Config];
  320. init_per_group(set_env, Config) ->
  321. Transport = ranch_tcp,
  322. {ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
  323. {env, [{dispatch, []}]},
  324. {max_keepalive, 50},
  325. {timeout, 500}
  326. ]),
  327. Port = ranch:get_port(set_env),
  328. {ok, Client} = cowboy_client:init([]),
  329. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  330. {transport, Transport}, {client, Client}|Config].
  331. end_per_group(Name, _) when Name =:= https; Name =:= https_compress ->
  332. cowboy:stop_listener(Name),
  333. application:stop(ssl),
  334. application:stop(public_key),
  335. application:stop(asn1),
  336. ok;
  337. end_per_group(Name, _) ->
  338. cowboy:stop_listener(Name),
  339. ok.
  340. %% Dispatch configuration.
  341. init_dispatch(Config) ->
  342. cowboy_router:compile([
  343. {"localhost", [
  344. {"/chunked_response", http_chunked, []},
  345. {"/streamed_response", http_streamed, []},
  346. {"/init_shutdown", http_init_shutdown, []},
  347. {"/long_polling", http_long_polling, []},
  348. {"/headers/dupe", http_handler,
  349. [{headers, [{<<"connection">>, <<"close">>}]}]},
  350. {"/set_resp/header", http_set_resp,
  351. [{headers, [{<<"vary">>, <<"Accept">>}]}]},
  352. {"/set_resp/overwrite", http_set_resp,
  353. [{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
  354. {"/set_resp/body", http_set_resp,
  355. [{body, <<"A flameless dance does not equal a cycle">>}]},
  356. {"/stream_body/set_resp", http_stream_body,
  357. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  358. {"/stream_body/set_resp_close",
  359. http_stream_body, [
  360. {reply, set_resp_close},
  361. {body, <<"stream_body_set_resp_close">>}]},
  362. {"/stream_body/set_resp_chunked",
  363. http_stream_body, [
  364. {reply, set_resp_chunked},
  365. {body, [<<"stream_body">>, <<"_set_resp_chunked">>]}]},
  366. {"/static/[...]", cowboy_static,
  367. {dir, ?config(static_dir, Config)}},
  368. {"/static_mimetypes_function/[...]", cowboy_static,
  369. {dir, ?config(static_dir, Config),
  370. [{mimetypes, ?MODULE, mimetypes_text_html}]}},
  371. {"/handler_errors", http_errors, []},
  372. {"/static_attribute_etag/[...]", cowboy_static,
  373. {dir, ?config(static_dir, Config)}},
  374. {"/static_function_etag/[...]", cowboy_static,
  375. {dir, ?config(static_dir, Config),
  376. [{etag, ?MODULE, etag_gen}]}},
  377. {"/static_specify_file/[...]", cowboy_static,
  378. {file, ?config(static_dir, Config) ++ "/style.css"}},
  379. {"/multipart", http_multipart, []},
  380. {"/echo/body", http_echo_body, []},
  381. {"/echo/body_qs", http_body_qs, []},
  382. {"/param_all", rest_param_all, []},
  383. {"/bad_accept", rest_simple_resource, []},
  384. {"/bad_content_type", rest_patch_resource, []},
  385. {"/simple", rest_simple_resource, []},
  386. {"/forbidden_post", rest_forbidden_resource, [true]},
  387. {"/simple_post", rest_forbidden_resource, [false]},
  388. {"/missing_get_callbacks", rest_missing_callbacks, []},
  389. {"/missing_put_callbacks", rest_missing_callbacks, []},
  390. {"/nodelete", rest_nodelete_resource, []},
  391. {"/post_charset", rest_post_charset_resource, []},
  392. {"/postonly", rest_postonly_resource, []},
  393. {"/patch", rest_patch_resource, []},
  394. {"/resetags", rest_resource_etags, []},
  395. {"/rest_expires", rest_expires, []},
  396. {"/rest_empty_resource", rest_empty_resource, []},
  397. {"/loop_recv", http_loop_recv, []},
  398. {"/loop_stream_recv", http_loop_stream_recv, []},
  399. {"/loop_timeout", http_loop_timeout, []},
  400. {"/", http_handler, []}
  401. ]}
  402. ]).
  403. etag_gen(_, _, _) ->
  404. {strong, <<"etag">>}.
  405. mimetypes_text_html(_) ->
  406. <<"text/html">>.
  407. %% Convenience functions.
  408. quick_raw(Data, Config) ->
  409. Client = ?config(client, Config),
  410. Transport = ?config(transport, Config),
  411. {ok, Client2} = cowboy_client:connect(
  412. Transport, "localhost", ?config(port, Config), Client),
  413. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  414. case cowboy_client:response(Client3) of
  415. {ok, Status, _, _} -> Status;
  416. {error, _} -> closed
  417. end.
  418. build_url(Path, Config) ->
  419. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  420. {port, Port} = lists:keyfind(port, 1, Config),
  421. PortBin = list_to_binary(integer_to_list(Port)),
  422. PathBin = list_to_binary(Path),
  423. << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
  424. quick_get(URL, Config) ->
  425. Client = ?config(client, Config),
  426. {ok, Client2} = cowboy_client:request(<<"GET">>,
  427. build_url(URL, Config), Client),
  428. {ok, Status, _, _} = cowboy_client:response(Client2),
  429. Status.
  430. body_to_chunks(_, <<>>, Acc) ->
  431. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  432. body_to_chunks(ChunkSize, Body, Acc) ->
  433. BodySize = byte_size(Body),
  434. ChunkSize2 = case BodySize < ChunkSize of
  435. true -> BodySize;
  436. false -> ChunkSize
  437. end,
  438. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  439. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  440. body_to_chunks(ChunkSize, Rest,
  441. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  442. %% Tests.
  443. check_raw_status(Config) ->
  444. Huge = [$0 || _ <- lists:seq(1, 5000)],
  445. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  446. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  447. ResponsePacket =
  448. "HTTP/1.0 302 Found\r
  449. Location: http://www.google.co.il/\r
  450. Cache-Control: private\r
  451. Content-Type: text/html; charset=UTF-8\r
  452. 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
  453. Date: Sun, 04 Dec 2011 15:55:01 GMT\r
  454. Server: gws\r
  455. Content-Length: 221\r
  456. X-XSS-Protection: 1; mode=block\r
  457. X-Frame-Options: SAMEORIGIN\r
  458. \r
  459. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  460. <TITLE>302 Moved</TITLE></HEAD><BODY>
  461. <H1>302 Moved</H1>
  462. The document has moved
  463. <A HREF=\"http://www.google.co.il/\">here</A>.
  464. </BODY></HTML>",
  465. Tests = [
  466. {102, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n"
  467. "Content-Length: 5000\r\n\r\n", 0:5000/unit:8 >>},
  468. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  469. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  470. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  471. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  472. {200, <<"POST /loop_recv HTTP/1.1\r\nHost: localhost\r\n"
  473. "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>},
  474. {400, "\n"},
  475. {400, "Garbage\r\n\r\n"},
  476. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  477. {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
  478. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  479. {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
  480. {505, ResponsePacket},
  481. {408, "GET / HTTP/1.1\r\n"},
  482. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  483. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  484. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  485. {414, Huge},
  486. {400, "GET / HTTP/1.1\r\n" ++ Huge},
  487. {500, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n"
  488. "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>},
  489. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  490. {closed, ""},
  491. {closed, "\r\n"},
  492. {closed, "\r\n\r\n"},
  493. {closed, "GET / HTTP/1.1"}
  494. ],
  495. _ = [{Status, Packet} = begin
  496. Ret = quick_raw(Packet, Config),
  497. {Ret, Packet}
  498. end || {Status, Packet} <- Tests],
  499. ok.
  500. check_status(Config) ->
  501. Tests = [
  502. {102, "/long_polling"},
  503. {200, "/"},
  504. {200, "/simple"},
  505. {204, "/loop_timeout"},
  506. {400, "/static/%2f"},
  507. {400, "/static/%2e"},
  508. {400, "/static/%2e%2e"},
  509. {403, "/static/directory"},
  510. {403, "/static/directory/"},
  511. {403, "/static/unreadable"},
  512. {404, "/not/found"},
  513. {404, "/static/not_found"},
  514. {500, "/handler_errors?case=handle_before_reply"},
  515. {500, "/handler_errors?case=init_before_reply"},
  516. {666, "/init_shutdown"}
  517. ],
  518. _ = [{Status, URL} = begin
  519. Ret = quick_get(URL, Config),
  520. {Ret, URL}
  521. end || {Status, URL} <- Tests].
  522. chunked_response(Config) ->
  523. Client = ?config(client, Config),
  524. {ok, Client2} = cowboy_client:request(<<"GET">>,
  525. build_url("/chunked_response", Config), Client),
  526. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  527. true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  528. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  529. {ok, <<"11\r\nchunked_handler\r\n\r\nB\r\nworks fine!\r\n0\r\n\r\n">>}
  530. = Transport:recv(Socket, 44, 1000),
  531. {error, closed} = cowboy_client:response(Client3).
  532. %% Check if sending requests whose size is around the MTU breaks something.
  533. echo_body(Config) ->
  534. Client = ?config(client, Config),
  535. MTU = ct_helper:get_loopback_mtu(),
  536. _ = [begin
  537. Body = list_to_binary(lists:duplicate(Size, $a)),
  538. {ok, Client2} = cowboy_client:request(<<"POST">>,
  539. build_url("/echo/body", Config),
  540. [{<<"connection">>, <<"close">>}],
  541. Body, Client),
  542. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  543. {ok, Body, _} = cowboy_client:response_body(Client3)
  544. end || Size <- lists:seq(MTU - 500, MTU)],
  545. ok.
  546. %% Check if sending request whose size is bigger than 1000000 bytes causes 413
  547. echo_body_max_length(Config) ->
  548. Client = ?config(client, Config),
  549. Body = <<$a:8000008>>,
  550. {ok, Client2} = cowboy_client:request(<<"POST">>,
  551. build_url("/echo/body", Config),
  552. [{<<"connection">>, <<"close">>}],
  553. Body, Client),
  554. {ok, 413, _, _} = cowboy_client:response(Client2).
  555. % check if body_qs echo's back results
  556. echo_body_qs(Config) ->
  557. Client = ?config(client, Config),
  558. Body = <<"echo=67890">>,
  559. {ok, Client2} = cowboy_client:request(<<"POST">>,
  560. build_url("/echo/body_qs", Config),
  561. [{<<"connection">>, <<"close">>}],
  562. Body, Client),
  563. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  564. {ok, <<"67890">>, _} = cowboy_client:response_body(Client3).
  565. %% Check if sending request whose size is bigger 16000 bytes causes 413
  566. echo_body_qs_max_length(Config) ->
  567. Client = ?config(client, Config),
  568. DefaultMaxBodyQsLength = 16000,
  569. % subtract "echo=" minus 1 byte from max to hit the limit
  570. Bits = (DefaultMaxBodyQsLength - 4) * 8,
  571. AppendedBody = <<$a:Bits>>,
  572. Body = <<"echo=", AppendedBody/binary>>,
  573. {ok, Client2} = cowboy_client:request(<<"POST">>,
  574. build_url("/echo/body_qs", Config),
  575. [{<<"connection">>, <<"close">>}],
  576. Body, Client),
  577. {ok, 413, _, _} = cowboy_client:response(Client2).
  578. error_chain_handle_after_reply(Config) ->
  579. Client = ?config(client, Config),
  580. {ok, Client2} = cowboy_client:request(<<"GET">>,
  581. build_url("/", Config), Client),
  582. {ok, Client3} = cowboy_client:request(<<"GET">>,
  583. build_url("/handler_errors?case=handle_after_reply", Config), Client2),
  584. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  585. {ok, 200, _, Client5} = cowboy_client:response(Client4),
  586. {error, closed} = cowboy_client:response(Client5).
  587. error_chain_handle_before_reply(Config) ->
  588. Client = ?config(client, Config),
  589. {ok, Client2} = cowboy_client:request(<<"GET">>,
  590. build_url("/", Config), Client),
  591. {ok, Client3} = cowboy_client:request(<<"GET">>,
  592. build_url("/handler_errors?case=handle_before_reply", Config), Client2),
  593. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  594. {ok, 500, _, Client5} = cowboy_client:response(Client4),
  595. {error, closed} = cowboy_client:response(Client5).
  596. error_handle_after_reply(Config) ->
  597. Client = ?config(client, Config),
  598. {ok, Client2} = cowboy_client:request(<<"GET">>,
  599. build_url("/handler_errors?case=handle_after_reply", Config), Client),
  600. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  601. {error, closed} = cowboy_client:response(Client3).
  602. error_init_after_reply(Config) ->
  603. Client = ?config(client, Config),
  604. {ok, Client2} = cowboy_client:request(<<"GET">>,
  605. build_url("/handler_errors?case=init_after_reply", Config), Client),
  606. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  607. {error, closed} = cowboy_client:response(Client3).
  608. error_init_reply_handle_error(Config) ->
  609. Client = ?config(client, Config),
  610. {ok, Client2} = cowboy_client:request(<<"GET">>,
  611. build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
  612. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  613. {error, closed} = cowboy_client:response(Client3).
  614. headers_dupe(Config) ->
  615. Client = ?config(client, Config),
  616. {ok, Client2} = cowboy_client:request(<<"GET">>,
  617. build_url("/headers/dupe", Config), Client),
  618. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  619. {<<"connection">>, <<"close">>}
  620. = lists:keyfind(<<"connection">>, 1, Headers),
  621. Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
  622. 1 = length(Connections),
  623. {error, closed} = cowboy_client:response(Client3).
  624. http10_chunkless(Config) ->
  625. Client = ?config(client, Config),
  626. Transport = ?config(transport, Config),
  627. {ok, Client2} = cowboy_client:connect(
  628. Transport, "localhost", ?config(port, Config), Client),
  629. Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
  630. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  631. {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
  632. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  633. %% Hack: we just try to get 28 bytes and compare.
  634. {ok, Transport, Socket} = cowboy_client:transport(Client4),
  635. Buffer = element(7, Client4),
  636. Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
  637. {ok, Recv} -> << Buffer/binary, Recv/binary >>;
  638. _ -> Buffer
  639. end,
  640. <<"chunked_handler\r\nworks fine!">> = Buffer2.
  641. http10_hostless(Config) ->
  642. Port10 = ?config(port, Config) + 10,
  643. Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
  644. ranch:start_listener(Name, 5,
  645. ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}],
  646. cowboy_protocol, [
  647. {env, [{dispatch, cowboy_router:compile([
  648. {'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
  649. {max_keepalive, 50},
  650. {timeout, 500}]
  651. ),
  652. 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  653. [{port, Port10}|Config]),
  654. cowboy:stop_listener(http10).
  655. keepalive_max(Config) ->
  656. Client = ?config(client, Config),
  657. URL = build_url("/", Config),
  658. ok = keepalive_max_loop(Client, URL, 50).
  659. keepalive_max_loop(Client, _, 0) ->
  660. {error, closed} = cowboy_client:response(Client),
  661. ok;
  662. keepalive_max_loop(Client, URL, N) ->
  663. Headers = [{<<"connection">>, <<"keep-alive">>}],
  664. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  665. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  666. Expected = case N of
  667. 1 -> <<"close">>;
  668. N -> <<"keep-alive">>
  669. end,
  670. {<<"connection">>, Expected}
  671. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  672. keepalive_max_loop(Client3, URL, N - 1).
  673. keepalive_nl(Config) ->
  674. Client = ?config(client, Config),
  675. URL = build_url("/", Config),
  676. ok = keepalive_nl_loop(Client, URL, 10).
  677. keepalive_nl_loop(Client, _, 0) ->
  678. {error, closed} = cowboy_client:response(Client),
  679. ok;
  680. keepalive_nl_loop(Client, URL, N) ->
  681. Headers = [{<<"connection">>, <<"keep-alive">>}],
  682. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  683. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  684. {<<"connection">>, <<"keep-alive">>}
  685. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  686. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  687. ok = Transport:send(Socket, <<"\r\n">>), %% empty line
  688. keepalive_nl_loop(Client3, URL, N - 1).
  689. keepalive_stream_loop(Config) ->
  690. Client = ?config(client, Config),
  691. Transport = ?config(transport, Config),
  692. {ok, Client2} = cowboy_client:connect(
  693. Transport, "localhost", ?config(port, Config), Client),
  694. keepalive_stream_loop(Client2, 10).
  695. keepalive_stream_loop(Client, 0) ->
  696. {error, closed} = cowboy_client:response(Client),
  697. ok;
  698. keepalive_stream_loop(Client, N) ->
  699. {ok, _} = cowboy_client:raw_request("POST /loop_stream_recv HTTP/1.1\r\n"
  700. "Host: localhost\r\n"
  701. "Connection: keepalive\r\n"
  702. "Transfer-Encoding: chunked\r\n\r\n", Client),
  703. _ = [{ok, _} = cowboy_client:raw_request(<<"4\r\n",Id:32,"\r\n">>, Client) ||
  704. Id <- lists:seq(1, 250)],
  705. {ok, _} = cowboy_client:raw_request(<<"0\r\n\r\n">>, Client),
  706. {ok, 200, _, _} = cowboy_client:response(Client),
  707. keepalive_stream_loop(Client, N-1).
  708. multipart(Config) ->
  709. Client = ?config(client, Config),
  710. Body = <<
  711. "This is a preamble."
  712. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  713. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  714. "\r\n--OHai--"
  715. "This is an epiloque."
  716. >>,
  717. {ok, Client2} = cowboy_client:request(<<"POST">>,
  718. build_url("/multipart", Config),
  719. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  720. Body, Client),
  721. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  722. {ok, RespBody, _} = cowboy_client:response_body(Client3),
  723. Parts = binary_to_term(RespBody),
  724. Parts = [
  725. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  726. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  727. ].
  728. nc_reqs(Config, Input) ->
  729. Cat = os:find_executable("cat"),
  730. Nc = os:find_executable("nc"),
  731. case {Cat, Nc} of
  732. {false, _} ->
  733. {skip, {notfound, cat}};
  734. {_, false} ->
  735. {skip, {notfound, nc}};
  736. _Good ->
  737. %% Throw garbage at the server then check if it's still up.
  738. {port, Port} = lists:keyfind(port, 1, Config),
  739. StrPort = integer_to_list(Port),
  740. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  741. || _ <- lists:seq(1, 100)],
  742. 200 = quick_get("/", Config)
  743. end.
  744. nc_rand(Config) ->
  745. nc_reqs(Config, "/dev/urandom").
  746. nc_zero(Config) ->
  747. nc_reqs(Config, "/dev/zero").
  748. onrequest(Config) ->
  749. Client = ?config(client, Config),
  750. {ok, Client2} = cowboy_client:request(<<"GET">>,
  751. build_url("/", Config), Client),
  752. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  753. {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
  754. {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
  755. onrequest_reply(Config) ->
  756. Client = ?config(client, Config),
  757. {ok, Client2} = cowboy_client:request(<<"GET">>,
  758. build_url("/?reply=1", Config), Client),
  759. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  760. {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
  761. {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
  762. %% Hook for the above onrequest tests.
  763. onrequest_hook(Req) ->
  764. case cowboy_req:qs_val(<<"reply">>, Req) of
  765. {undefined, Req2} ->
  766. cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
  767. {_, Req2} ->
  768. {ok, Req3} = cowboy_req:reply(
  769. 200, [], <<"replied!">>, Req2),
  770. Req3
  771. end.
  772. onresponse_capitalize(Config) ->
  773. Client = ?config(client, Config),
  774. {ok, Client2} = cowboy_client:request(<<"GET">>,
  775. build_url("/", Config), Client),
  776. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  777. {ok, Data} = Transport:recv(Socket, 0, 1000),
  778. false = nomatch =:= binary:match(Data, <<"Content-Length">>).
  779. %% Hook for the above onresponse_capitalize test.
  780. onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  781. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  782. || {N, V} <- Headers],
  783. {ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req),
  784. Req2.
  785. onresponse_crash(Config) ->
  786. Client = ?config(client, Config),
  787. {ok, Client2} = cowboy_client:request(<<"GET">>,
  788. build_url("/handler_errors?case=init_before_reply", Config), Client),
  789. {ok, 777, Headers, _} = cowboy_client:response(Client2),
  790. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  791. onresponse_reply(Config) ->
  792. Client = ?config(client, Config),
  793. {ok, Client2} = cowboy_client:request(<<"GET">>,
  794. build_url("/", Config), Client),
  795. {ok, 777, Headers, Client3} = cowboy_client:response(Client2),
  796. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  797. %% Make sure we don't get the body initially sent.
  798. {error, closed} = cowboy_client:response_body(Client3).
  799. %% Hook for the above onresponse tests.
  800. onresponse_hook(_, Headers, _, Req) ->
  801. {ok, Req2} = cowboy_req:reply(
  802. <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
  803. Req2.
  804. parse_host(Config) ->
  805. Tests = [
  806. {<<"example.org\n8080">>, <<"example.org:8080">>},
  807. {<<"example.org\n80">>, <<"example.org">>},
  808. {<<"192.0.2.1\n8080">>, <<"192.0.2.1:8080">>},
  809. {<<"192.0.2.1\n80">>, <<"192.0.2.1">>},
  810. {<<"[2001:db8::1]\n8080">>, <<"[2001:db8::1]:8080">>},
  811. {<<"[2001:db8::1]\n80">>, <<"[2001:db8::1]">>},
  812. {<<"[::ffff:192.0.2.1]\n8080">>, <<"[::ffff:192.0.2.1]:8080">>},
  813. {<<"[::ffff:192.0.2.1]\n80">>, <<"[::ffff:192.0.2.1]">>}
  814. ],
  815. [begin
  816. Client = ?config(client, Config),
  817. {ok, Client2} = cowboy_client:request(<<"GET">>,
  818. build_url("/req_attr?attr=host_and_port", Config),
  819. [{<<"host">>, Host}],
  820. Client),
  821. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  822. {ok, Value, Client4} = cowboy_client:response_body(Client3),
  823. {error, closed} = cowboy_client:response(Client4),
  824. Value
  825. end || {Value, Host} <- Tests].
  826. pipeline(Config) ->
  827. Client = ?config(client, Config),
  828. {ok, Client2} = cowboy_client:request(<<"GET">>,
  829. build_url("/", Config), Client),
  830. {ok, Client3} = cowboy_client:request(<<"GET">>,
  831. build_url("/", Config), Client2),
  832. {ok, Client4} = cowboy_client:request(<<"GET">>,
  833. build_url("/", Config), Client3),
  834. {ok, Client5} = cowboy_client:request(<<"GET">>,
  835. build_url("/", Config), Client4),
  836. {ok, Client6} = cowboy_client:request(<<"GET">>,
  837. build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
  838. {ok, 200, _, Client7} = cowboy_client:response(Client6),
  839. {ok, 200, _, Client8} = cowboy_client:response(Client7),
  840. {ok, 200, _, Client9} = cowboy_client:response(Client8),
  841. {ok, 200, _, Client10} = cowboy_client:response(Client9),
  842. {ok, 200, _, Client11} = cowboy_client:response(Client10),
  843. {error, closed} = cowboy_client:response(Client11).
  844. pipeline_long_polling(Config) ->
  845. Client = ?config(client, Config),
  846. {ok, Client2} = cowboy_client:request(<<"GET">>,
  847. build_url("/long_polling", Config), Client),
  848. {ok, Client3} = cowboy_client:request(<<"GET">>,
  849. build_url("/long_polling", Config), Client2),
  850. {ok, 102, _, Client4} = cowboy_client:response(Client3),
  851. {ok, 102, _, Client5} = cowboy_client:response(Client4),
  852. {error, closed} = cowboy_client:response(Client5).
  853. rest_param_all(Config) ->
  854. Client = ?config(client, Config),
  855. URL = build_url("/param_all", Config),
  856. % Accept without param
  857. {ok, Client2} = cowboy_client:request(<<"GET">>, URL,
  858. [{<<"accept">>, <<"text/plain">>}], Client),
  859. Client3 = check_response(Client2, <<"[]">>),
  860. % Accept with param
  861. {ok, Client4} = cowboy_client:request(<<"GET">>, URL,
  862. [{<<"accept">>, <<"text/plain;level=1">>}], Client3),
  863. Client5 = check_response(Client4, <<"level=1">>),
  864. % Accept with param and quality
  865. {ok, Client6} = cowboy_client:request(<<"GET">>, URL,
  866. [{<<"accept">>,
  867. <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}],
  868. Client5),
  869. Client7 = check_response(Client6, <<"level=1">>),
  870. {ok, Client8} = cowboy_client:request(<<"GET">>, URL,
  871. [{<<"accept">>,
  872. <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}],
  873. Client7),
  874. Client9 = check_response(Client8, <<"level=2">>),
  875. % Without Accept
  876. {ok, Client10} = cowboy_client:request(<<"GET">>, URL, [], Client9),
  877. Client11 = check_response(Client10, <<"'*'">>),
  878. % Content-Type without param
  879. {ok, Client12} = cowboy_client:request(<<"PUT">>, URL,
  880. [{<<"content-type">>, <<"text/plain">>}], Client11),
  881. {ok, 204, _, Client13} = cowboy_client:response(Client12),
  882. % Content-Type with param
  883. {ok, Client14} = cowboy_client:request(<<"PUT">>, URL,
  884. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}], Client13),
  885. {ok, 204, _, _} = cowboy_client:response(Client14).
  886. check_response(Client, Body) ->
  887. {ok, 200, _, Client2} = cowboy_client:response(Client),
  888. {ok, Body, Client3} = cowboy_client:response_body(Client2),
  889. Client3.
  890. rest_bad_accept(Config) ->
  891. Client = ?config(client, Config),
  892. {ok, Client2} = cowboy_client:request(<<"GET">>,
  893. build_url("/bad_accept", Config),
  894. [{<<"accept">>, <<"1">>}],
  895. Client),
  896. {ok, 400, _, _} = cowboy_client:response(Client2).
  897. rest_bad_content_type(Config) ->
  898. Client = ?config(client, Config),
  899. {ok, Client2} = cowboy_client:request(<<"PATCH">>,
  900. build_url("/bad_content_type", Config),
  901. [{<<"content-type">>, <<"text/plain, text/html">>}],
  902. <<"Whatever">>, Client),
  903. {ok, 415, _, _} = cowboy_client:response(Client2).
  904. rest_expires(Config) ->
  905. Client = ?config(client, Config),
  906. {ok, Client2} = cowboy_client:request(<<"GET">>,
  907. build_url("/rest_expires", Config), Client),
  908. {ok, 200, RespHeaders, _} = cowboy_client:response(Client2),
  909. {_, Expires} = lists:keyfind(<<"expires">>, 1, RespHeaders),
  910. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, RespHeaders),
  911. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  912. ok.
  913. rest_keepalive(Config) ->
  914. Client = ?config(client, Config),
  915. URL = build_url("/simple", Config),
  916. ok = rest_keepalive_loop(Client, URL, 10).
  917. rest_keepalive_loop(_, _, 0) ->
  918. ok;
  919. rest_keepalive_loop(Client, URL, N) ->
  920. Headers = [{<<"connection">>, <<"keep-alive">>}],
  921. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  922. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  923. {<<"connection">>, <<"keep-alive">>}
  924. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  925. rest_keepalive_loop(Client3, URL, N - 1).
  926. rest_keepalive_post(Config) ->
  927. Client = ?config(client, Config),
  928. ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
  929. rest_keepalive_post_loop(_, _, _, 0) ->
  930. ok;
  931. rest_keepalive_post_loop(Config, Client, simple_post, N) ->
  932. Headers = [
  933. {<<"connection">>, <<"keep-alive">>},
  934. {<<"content-type">>, <<"text/plain">>}
  935. ],
  936. {ok, Client2} = cowboy_client:request(<<"POST">>,
  937. build_url("/simple_post", Config), Headers, "12345", Client),
  938. {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
  939. {<<"connection">>, <<"keep-alive">>}
  940. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  941. rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
  942. rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
  943. Headers = [
  944. {<<"connection">>, <<"keep-alive">>},
  945. {<<"content-type">>, <<"text/plain">>}
  946. ],
  947. {ok, Client2} = cowboy_client:request(<<"POST">>,
  948. build_url("/forbidden_post", Config), Headers, "12345", Client),
  949. {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
  950. {<<"connection">>, <<"keep-alive">>}
  951. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  952. rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
  953. rest_missing_get_callbacks(Config) ->
  954. Client = ?config(client, Config),
  955. {ok, Client2} = cowboy_client:request(<<"GET">>,
  956. build_url("/missing_get_callbacks", Config), Client),
  957. {ok, 500, _, _} = cowboy_client:response(Client2).
  958. rest_missing_put_callbacks(Config) ->
  959. Client = ?config(client, Config),
  960. {ok, Client2} = cowboy_client:request(<<"PUT">>,
  961. build_url("/missing_put_callbacks", Config),
  962. [{<<"content-type">>, <<"application/json">>}],
  963. <<"{}">>, Client),
  964. {ok, 500, _, _} = cowboy_client:response(Client2).
  965. rest_nodelete(Config) ->
  966. Client = ?config(client, Config),
  967. {ok, Client2} = cowboy_client:request(<<"DELETE">>,
  968. build_url("/nodelete", Config), Client),
  969. {ok, 500, _, _} = cowboy_client:response(Client2).
  970. rest_options_default(Config) ->
  971. Client = ?config(client, Config),
  972. {ok, Client2} = cowboy_client:request(<<"OPTIONS">>,
  973. build_url("/rest_empty_resource", Config), Client),
  974. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  975. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers).
  976. rest_patch(Config) ->
  977. Tests = [
  978. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  979. {422, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  980. {400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
  981. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  982. ],
  983. Client = ?config(client, Config),
  984. _ = [begin
  985. {ok, Client2} = cowboy_client:request(<<"PATCH">>,
  986. build_url("/patch", Config), Headers, Body, Client),
  987. {ok, Status, _, _} = cowboy_client:response(Client2),
  988. ok
  989. end || {Status, Headers, Body} <- Tests].
  990. rest_post_charset(Config) ->
  991. Client = ?config(client, Config),
  992. Headers = [
  993. {<<"content-type">>, <<"text/plain;charset=UTF-8">>}
  994. ],
  995. {ok, Client2} = cowboy_client:request(<<"POST">>,
  996. build_url("/post_charset", Config), Headers, "12345", Client),
  997. {ok, 204, _, _} = cowboy_client:response(Client2).
  998. rest_postonly(Config) ->
  999. Client = ?config(client, Config),
  1000. Headers = [
  1001. {<<"content-type">>, <<"text/plain">>}
  1002. ],
  1003. {ok, Client2} = cowboy_client:request(<<"POST">>,
  1004. build_url("/postonly", Config), Headers, "12345", Client),
  1005. {ok, 204, _, _} = cowboy_client:response(Client2).
  1006. rest_resource_get_etag(Config, Type) ->
  1007. rest_resource_get_etag(Config, Type, []).
  1008. rest_resource_get_etag(Config, Type, Headers) ->
  1009. Client = ?config(client, Config),
  1010. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1011. build_url("/resetags?type=" ++ Type, Config), Headers, Client),
  1012. {ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
  1013. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  1014. false -> {Status, false};
  1015. {<<"etag">>, ETag} -> {Status, ETag}
  1016. end.
  1017. rest_resource_etags(Config) ->
  1018. Tests = [
  1019. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  1020. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  1021. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  1022. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  1023. {500, false, "binary-strong-unquoted"},
  1024. {500, false, "binary-weak-unquoted"}
  1025. ],
  1026. _ = [{Status, ETag, Type} = begin
  1027. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  1028. {Ret, RespETag, Type}
  1029. end || {Status, ETag, Type} <- Tests].
  1030. rest_resource_etags_if_none_match(Config) ->
  1031. Tests = [
  1032. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  1033. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  1034. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  1035. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  1036. ],
  1037. _ = [{Status, Type} = begin
  1038. {Ret, _} = rest_resource_get_etag(Config, Type,
  1039. [{<<"if-none-match">>, ETag}]),
  1040. {Ret, Type}
  1041. end || {Status, ETag, Type} <- Tests].
  1042. set_env_dispatch(Config) ->
  1043. Client = ?config(client, Config),
  1044. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1045. build_url("/", Config), Client),
  1046. {ok, 400, _, _} = cowboy_client:response(Client2),
  1047. ok = cowboy:set_env(set_env, dispatch,
  1048. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  1049. {ok, Client3} = cowboy_client:request(<<"GET">>,
  1050. build_url("/", Config), Client),
  1051. {ok, 200, _, _} = cowboy_client:response(Client3).
  1052. set_resp_body(Config) ->
  1053. Client = ?config(client, Config),
  1054. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1055. build_url("/set_resp/body", Config), Client),
  1056. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1057. {ok, <<"A flameless dance does not equal a cycle">>, _}
  1058. = cowboy_client:response_body(Client3).
  1059. set_resp_header(Config) ->
  1060. Client = ?config(client, Config),
  1061. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1062. build_url("/set_resp/header", Config), Client),
  1063. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1064. {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  1065. {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
  1066. set_resp_overwrite(Config) ->
  1067. Client = ?config(client, Config),
  1068. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1069. build_url("/set_resp/overwrite", Config), Client),
  1070. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1071. {<<"server">>, <<"DesireDrive/1.0">>}
  1072. = lists:keyfind(<<"server">>, 1, Headers).
  1073. slowloris(Config) ->
  1074. Client = ?config(client, Config),
  1075. Transport = ?config(transport, Config),
  1076. {ok, Client2} = cowboy_client:connect(
  1077. Transport, "localhost", ?config(port, Config), Client),
  1078. try
  1079. [begin
  1080. {ok, _} = cowboy_client:raw_request([C], Client2),
  1081. receive after 25 -> ok end
  1082. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  1083. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  1084. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  1085. error(failure)
  1086. catch error:{badmatch, _} ->
  1087. ok
  1088. end.
  1089. slowloris2(Config) ->
  1090. Client = ?config(client, Config),
  1091. Transport = ?config(transport, Config),
  1092. {ok, Client2} = cowboy_client:connect(
  1093. Transport, "localhost", ?config(port, Config), Client),
  1094. {ok, _} = cowboy_client:raw_request("GET / HTTP/1.1\r\n", Client2),
  1095. receive after 300 -> ok end,
  1096. {ok, _} = cowboy_client:raw_request("Host: localhost\r\n", Client2),
  1097. receive after 300 -> ok end,
  1098. {ok, 408, _, _} = cowboy_client:response(Client2).
  1099. static_attribute_etag(Config) ->
  1100. Client = ?config(client, Config),
  1101. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1102. build_url("/static_attribute_etag/index.html", Config), Client),
  1103. {ok, Client3} = cowboy_client:request(<<"GET">>,
  1104. build_url("/static_attribute_etag/index.html", Config), Client2),
  1105. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  1106. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  1107. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  1108. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  1109. false = ETag1 =:= undefined,
  1110. ETag1 = ETag2.
  1111. static_function_etag(Config) ->
  1112. Client = ?config(client, Config),
  1113. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1114. build_url("/static_function_etag/index.html", Config), Client),
  1115. {ok, Client3} = cowboy_client:request(<<"GET">>,
  1116. build_url("/static_function_etag/index.html", Config), Client2),
  1117. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  1118. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  1119. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  1120. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  1121. false = ETag1 =:= undefined,
  1122. ETag1 = ETag2.
  1123. static_mimetypes_function(Config) ->
  1124. Client = ?config(client, Config),
  1125. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1126. build_url("/static_mimetypes_function/index.html", Config), Client),
  1127. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1128. {<<"content-type">>, <<"text/html">>}
  1129. = lists:keyfind(<<"content-type">>, 1, Headers).
  1130. static_specify_file(Config) ->
  1131. Client = ?config(client, Config),
  1132. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1133. build_url("/static_specify_file", Config), Client),
  1134. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  1135. {<<"content-type">>, <<"text/css">>}
  1136. = lists:keyfind(<<"content-type">>, 1, Headers),
  1137. {ok, <<"body{color:red}\n">>, _} = cowboy_client:response_body(Client3).
  1138. static_specify_file_catchall(Config) ->
  1139. Client = ?config(client, Config),
  1140. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1141. build_url("/static_specify_file/none", Config), Client),
  1142. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  1143. {<<"content-type">>, <<"text/css">>}
  1144. = lists:keyfind(<<"content-type">>, 1, Headers),
  1145. {ok, <<"body{color:red}\n">>, _} = cowboy_client:response_body(Client3).
  1146. static_test_file(Config) ->
  1147. Client = ?config(client, Config),
  1148. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1149. build_url("/static/unknown", Config), Client),
  1150. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1151. {<<"content-type">>, <<"application/octet-stream">>}
  1152. = lists:keyfind(<<"content-type">>, 1, Headers).
  1153. static_test_file_css(Config) ->
  1154. Client = ?config(client, Config),
  1155. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1156. build_url("/static/style.css", Config), Client),
  1157. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1158. {<<"content-type">>, <<"text/css">>}
  1159. = lists:keyfind(<<"content-type">>, 1, Headers).
  1160. stream_body_set_resp(Config) ->
  1161. Client = ?config(client, Config),
  1162. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1163. build_url("/stream_body/set_resp", Config), Client),
  1164. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1165. {ok, <<"stream_body_set_resp">>, _}
  1166. = cowboy_client:response_body(Client3).
  1167. stream_body_set_resp_close(Config) ->
  1168. Client = ?config(client, Config),
  1169. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1170. build_url("/stream_body/set_resp_close", Config), Client),
  1171. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1172. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  1173. case element(7, Client3) of
  1174. <<"stream_body_set_resp_close">> ->
  1175. ok;
  1176. Buffer ->
  1177. {ok, Rest} = Transport:recv(Socket, 26 - byte_size(Buffer), 1000),
  1178. <<"stream_body_set_resp_close">> = << Buffer/binary, Rest/binary >>,
  1179. ok
  1180. end,
  1181. {error, closed} = Transport:recv(Socket, 0, 1000).
  1182. stream_body_set_resp_chunked(Config) ->
  1183. Client = ?config(client, Config),
  1184. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1185. build_url("/stream_body/set_resp_chunked", Config), Client),
  1186. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  1187. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  1188. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  1189. case element(7, Client3) of
  1190. <<"B\r\nstream_body\r\n11\r\n_set_resp_chunked\r\n0\r\n\r\n">> ->
  1191. ok;
  1192. Buffer ->
  1193. {ok, Rest} = Transport:recv(Socket, 44 - byte_size(Buffer), 1000),
  1194. <<"B\r\nstream_body\r\n11\r\n_set_resp_chunked\r\n0\r\n\r\n">>
  1195. = <<Buffer/binary, Rest/binary>>,
  1196. ok
  1197. end.
  1198. stream_body_set_resp_chunked10(Config) ->
  1199. Client = ?config(client, Config),
  1200. Transport = ?config(transport, Config),
  1201. {ok, Client2} = cowboy_client:connect(
  1202. Transport, "localhost", ?config(port, Config), Client),
  1203. Data = ["GET /stream_body/set_resp_chunked HTTP/1.0\r\n",
  1204. "Host: localhost\r\n\r\n"],
  1205. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  1206. {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
  1207. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  1208. {ok, Transport, Socket} = cowboy_client:transport(Client4),
  1209. case element(7, Client4) of
  1210. <<"stream_body_set_resp_chunked">> ->
  1211. ok;
  1212. Buffer ->
  1213. {ok, Rest} = Transport:recv(Socket, 28 - byte_size(Buffer), 1000),
  1214. <<"stream_body_set_resp_chunked">>
  1215. = <<Buffer/binary, Rest/binary>>,
  1216. ok
  1217. end,
  1218. {error, closed} = Transport:recv(Socket, 0, 1000).
  1219. streamed_response(Config) ->
  1220. Client = ?config(client, Config),
  1221. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1222. build_url("/streamed_response", Config), Client),
  1223. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  1224. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  1225. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  1226. {ok, <<"streamed_handler\r\nworks fine!">>}
  1227. = Transport:recv(Socket, 29, 1000),
  1228. {error, closed} = cowboy_client:response(Client3).
  1229. te_chunked(Config) ->
  1230. Client = ?config(client, Config),
  1231. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1232. Chunks = body_to_chunks(50, Body, []),
  1233. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1234. build_url("/echo/body", Config),
  1235. [{<<"transfer-encoding">>, <<"chunked">>}],
  1236. Chunks, Client),
  1237. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1238. {ok, Body, _} = cowboy_client:response_body(Client3).
  1239. te_chunked_chopped(Config) ->
  1240. Client = ?config(client, Config),
  1241. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1242. Body2 = iolist_to_binary(body_to_chunks(50, Body, [])),
  1243. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1244. build_url("/echo/body", Config),
  1245. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  1246. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  1247. _ = [begin
  1248. ok = Transport:send(Socket, << C >>),
  1249. ok = timer:sleep(10)
  1250. end || << C >> <= Body2],
  1251. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1252. {ok, Body, _} = cowboy_client:response_body(Client3).
  1253. te_chunked_delayed(Config) ->
  1254. Client = ?config(client, Config),
  1255. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1256. Chunks = body_to_chunks(50, Body, []),
  1257. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1258. build_url("/echo/body", Config),
  1259. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  1260. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  1261. _ = [begin
  1262. ok = Transport:send(Socket, Chunk),
  1263. ok = timer:sleep(10)
  1264. end || Chunk <- Chunks],
  1265. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1266. {ok, Body, _} = cowboy_client:response_body(Client3).
  1267. te_chunked_split_body(Config) ->
  1268. Client = ?config(client, Config),
  1269. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1270. Chunks = body_to_chunks(50, Body, []),
  1271. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1272. build_url("/echo/body", Config),
  1273. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  1274. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  1275. _ = [begin
  1276. case Chunk of
  1277. %% Final chunk.
  1278. <<"0\r\n\r\n">> ->
  1279. ok = Transport:send(Socket, Chunk);
  1280. _ ->
  1281. %% Chunk of form <<"9\r\nChunkBody\r\n">>.
  1282. [Size, ChunkBody, <<>>] =
  1283. binary:split(Chunk, [<<"\r\n">>], [global]),
  1284. PartASize = random:uniform(byte_size(ChunkBody)),
  1285. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  1286. ok = Transport:send(Socket, [Size, <<"\r\n">>, PartA]),
  1287. ok = timer:sleep(10),
  1288. ok = Transport:send(Socket, [PartB, <<"\r\n">>])
  1289. end
  1290. end || Chunk <- Chunks],
  1291. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1292. {ok, Body, _} = cowboy_client:response_body(Client3).
  1293. te_chunked_split_crlf(Config) ->
  1294. Client = ?config(client, Config),
  1295. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1296. Chunks = body_to_chunks(50, Body, []),
  1297. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1298. build_url("/echo/body", Config),
  1299. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  1300. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  1301. _ = [begin
  1302. %% <<"\r\n">> is last 2 bytes of Chunk split before or after <<"\r">>.
  1303. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  1304. <<Chunk2:Len/binary, End/binary>> = Chunk,
  1305. ok = Transport:send(Socket, Chunk2),
  1306. ok = timer:sleep(10),
  1307. ok = Transport:send(Socket, End)
  1308. end || Chunk <- Chunks],
  1309. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1310. {ok, Body, _} = cowboy_client:response_body(Client3).
  1311. te_identity(Config) ->
  1312. Client = ?config(client, Config),
  1313. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1314. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1315. build_url("/echo/body", Config), [], Body, Client),
  1316. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1317. {ok, Body, _} = cowboy_client:response_body(Client3).