http_SUITE.erl 51 KB

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