http_SUITE.erl 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. %% Tests.
  25. -export([check_raw_status/1]).
  26. -export([check_status/1]).
  27. -export([chunked_response/1]).
  28. -export([echo_body/1]).
  29. -export([error_chain_handle_after_reply/1]).
  30. -export([error_chain_handle_before_reply/1]).
  31. -export([error_handle_after_reply/1]).
  32. -export([error_init_after_reply/1]).
  33. -export([error_init_reply_handle_error/1]).
  34. -export([headers_dupe/1]).
  35. -export([http10_chunkless/1]).
  36. -export([http10_hostless/1]).
  37. -export([keepalive_max/1]).
  38. -export([keepalive_nl/1]).
  39. -export([multipart/1]).
  40. -export([nc_rand/1]).
  41. -export([nc_zero/1]).
  42. -export([onrequest/1]).
  43. -export([onrequest_reply/1]).
  44. -export([onresponse_crash/1]).
  45. -export([onresponse_reply/1]).
  46. -export([pipeline/1]).
  47. -export([rest_bad_accept/1]).
  48. -export([rest_expires/1]).
  49. -export([rest_keepalive/1]).
  50. -export([rest_keepalive_post/1]).
  51. -export([rest_missing_get_callbacks/1]).
  52. -export([rest_missing_put_callbacks/1]).
  53. -export([rest_nodelete/1]).
  54. -export([rest_resource_etags/1]).
  55. -export([rest_resource_etags_if_none_match/1]).
  56. -export([set_resp_body/1]).
  57. -export([set_resp_header/1]).
  58. -export([set_resp_overwrite/1]).
  59. -export([slowloris/1]).
  60. -export([slowloris2/1]).
  61. -export([static_attribute_etag/1]).
  62. -export([static_function_etag/1]).
  63. -export([static_mimetypes_function/1]).
  64. -export([static_specify_file/1]).
  65. -export([static_specify_file_catchall/1]).
  66. -export([static_test_file/1]).
  67. -export([static_test_file_css/1]).
  68. -export([stream_body_set_resp/1]).
  69. -export([stream_body_set_resp_close/1]).
  70. -export([te_chunked/1]).
  71. -export([te_chunked_delayed/1]).
  72. -export([te_identity/1]).
  73. %% ct.
  74. all() ->
  75. [{group, http}, {group, https}, {group, onrequest}, {group, onresponse}].
  76. groups() ->
  77. Tests = [
  78. check_raw_status,
  79. check_status,
  80. chunked_response,
  81. echo_body,
  82. error_chain_handle_after_reply,
  83. error_chain_handle_before_reply,
  84. error_handle_after_reply,
  85. error_init_after_reply,
  86. error_init_reply_handle_error,
  87. headers_dupe,
  88. http10_chunkless,
  89. http10_hostless,
  90. keepalive_max,
  91. keepalive_nl,
  92. multipart,
  93. nc_rand,
  94. nc_zero,
  95. pipeline,
  96. rest_bad_accept,
  97. rest_expires,
  98. rest_keepalive,
  99. rest_keepalive_post,
  100. rest_missing_get_callbacks,
  101. rest_missing_put_callbacks,
  102. rest_nodelete,
  103. rest_resource_etags,
  104. rest_resource_etags_if_none_match,
  105. set_resp_body,
  106. set_resp_header,
  107. set_resp_overwrite,
  108. slowloris,
  109. slowloris2,
  110. static_attribute_etag,
  111. static_function_etag,
  112. static_mimetypes_function,
  113. static_specify_file,
  114. static_specify_file_catchall,
  115. static_test_file,
  116. static_test_file_css,
  117. stream_body_set_resp,
  118. stream_body_set_resp_close,
  119. te_chunked,
  120. te_chunked_delayed,
  121. te_identity
  122. ],
  123. [
  124. {http, [], Tests},
  125. {https, [], Tests},
  126. {onrequest, [], [
  127. onrequest,
  128. onrequest_reply
  129. ]},
  130. {onresponse, [], [
  131. onresponse_crash,
  132. onresponse_reply
  133. ]}
  134. ].
  135. init_per_suite(Config) ->
  136. application:start(inets),
  137. application:start(crypto),
  138. application:start(ranch),
  139. application:start(cowboy),
  140. Config.
  141. end_per_suite(_Config) ->
  142. application:stop(cowboy),
  143. application:stop(ranch),
  144. application:stop(crypto),
  145. application:stop(inets),
  146. ok.
  147. init_per_group(http, Config) ->
  148. Port = 33080,
  149. Transport = ranch_tcp,
  150. Config1 = init_static_dir(Config),
  151. {ok, _} = cowboy:start_http(http, 100, [{port, Port}], [
  152. {env, [{dispatch, init_dispatch(Config1)}]},
  153. {max_keepalive, 50},
  154. {timeout, 500}
  155. ]),
  156. {ok, Client} = cowboy_client:init([]),
  157. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  158. {transport, Transport}, {client, Client}|Config1];
  159. init_per_group(https, Config) ->
  160. Port = 33081,
  161. Transport = ranch_ssl,
  162. Opts = [
  163. {certfile, ?config(data_dir, Config) ++ "cert.pem"},
  164. {keyfile, ?config(data_dir, Config) ++ "key.pem"},
  165. {password, "cowboy"}
  166. ],
  167. Config1 = init_static_dir(Config),
  168. application:start(public_key),
  169. application:start(ssl),
  170. {ok, _} = cowboy:start_https(https, 100, Opts ++ [{port, Port}], [
  171. {env, [{dispatch, init_dispatch(Config1)}]},
  172. {max_keepalive, 50},
  173. {timeout, 500}
  174. ]),
  175. {ok, Client} = cowboy_client:init(Opts),
  176. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  177. {transport, Transport}, {client, Client}|Config1];
  178. init_per_group(onrequest, Config) ->
  179. Port = 33082,
  180. Transport = ranch_tcp,
  181. {ok, _} = cowboy:start_http(onrequest, 100, [{port, Port}], [
  182. {env, [{dispatch, init_dispatch(Config)}]},
  183. {max_keepalive, 50},
  184. {onrequest, fun onrequest_hook/1},
  185. {timeout, 500}
  186. ]),
  187. {ok, Client} = cowboy_client:init([]),
  188. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  189. {transport, Transport}, {client, Client}|Config];
  190. init_per_group(onresponse, Config) ->
  191. Port = 33083,
  192. Transport = ranch_tcp,
  193. {ok, _} = cowboy:start_http(onresponse, 100, [{port, Port}], [
  194. {env, [{dispatch, init_dispatch(Config)}]},
  195. {max_keepalive, 50},
  196. {onresponse, fun onresponse_hook/4},
  197. {timeout, 500}
  198. ]),
  199. {ok, Client} = cowboy_client:init([]),
  200. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  201. {transport, Transport}, {client, Client}|Config].
  202. end_per_group(https, Config) ->
  203. cowboy:stop_listener(https),
  204. application:stop(ssl),
  205. application:stop(public_key),
  206. end_static_dir(Config),
  207. ok;
  208. end_per_group(http, Config) ->
  209. cowboy:stop_listener(http),
  210. end_static_dir(Config);
  211. end_per_group(Name, _) ->
  212. cowboy:stop_listener(Name),
  213. ok.
  214. %% Dispatch configuration.
  215. init_dispatch(Config) ->
  216. [
  217. {[<<"localhost">>], [
  218. {[<<"chunked_response">>], chunked_handler, []},
  219. {[<<"init_shutdown">>], http_handler_init_shutdown, []},
  220. {[<<"long_polling">>], http_handler_long_polling, []},
  221. {[<<"headers">>, <<"dupe">>], http_handler,
  222. [{headers, [{<<"connection">>, <<"close">>}]}]},
  223. {[<<"set_resp">>, <<"header">>], http_handler_set_resp,
  224. [{headers, [{<<"vary">>, <<"Accept">>}]}]},
  225. {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
  226. [{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
  227. {[<<"set_resp">>, <<"body">>], http_handler_set_resp,
  228. [{body, <<"A flameless dance does not equal a cycle">>}]},
  229. {[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body,
  230. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  231. {[<<"stream_body">>, <<"set_resp_close">>],
  232. http_handler_stream_body, [
  233. {reply, set_resp_close},
  234. {body, <<"stream_body_set_resp_close">>}]},
  235. {[<<"static">>, '...'], cowboy_static,
  236. [{directory, ?config(static_dir, Config)},
  237. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  238. {[<<"static_mimetypes_function">>, '...'], cowboy_static,
  239. [{directory, ?config(static_dir, Config)},
  240. {mimetypes, {fun(Path, data) when is_binary(Path) ->
  241. [<<"text/html">>] end, data}}]},
  242. {[<<"handler_errors">>], http_handler_errors, []},
  243. {[<<"static_attribute_etag">>, '...'], cowboy_static,
  244. [{directory, ?config(static_dir, Config)},
  245. {etag, {attributes, [filepath, filesize, inode, mtime]}}]},
  246. {[<<"static_function_etag">>, '...'], cowboy_static,
  247. [{directory, ?config(static_dir, Config)},
  248. {etag, {fun static_function_etag/2, etag_data}}]},
  249. {[<<"static_specify_file">>, '...'], cowboy_static,
  250. [{directory, ?config(static_dir, Config)},
  251. {mimetypes, [{<<".css">>, [<<"text/css">>]}]},
  252. {file, <<"test_file.css">>}]},
  253. {[<<"multipart">>], http_handler_multipart, []},
  254. {[<<"echo">>, <<"body">>], http_handler_echo_body, []},
  255. {[<<"bad_accept">>], rest_simple_resource, []},
  256. {[<<"simple">>], rest_simple_resource, []},
  257. {[<<"forbidden_post">>], rest_forbidden_resource, [true]},
  258. {[<<"simple_post">>], rest_forbidden_resource, [false]},
  259. {[<<"missing_get_callbacks">>], rest_missing_callbacks, []},
  260. {[<<"missing_put_callbacks">>], rest_missing_callbacks, []},
  261. {[<<"nodelete">>], rest_nodelete_resource, []},
  262. {[<<"resetags">>], rest_resource_etags, []},
  263. {[<<"rest_expires">>], rest_expires, []},
  264. {[<<"loop_timeout">>], http_handler_loop_timeout, []},
  265. {[], http_handler, []}
  266. ]}
  267. ].
  268. init_static_dir(Config) ->
  269. Dir = filename:join(?config(priv_dir, Config), "static"),
  270. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  271. ok = file:make_dir(Dir),
  272. ok = file:write_file(Level1("test_file"), "test_file\n"),
  273. ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
  274. ok = file:write_file(Level1("test_noread"), "test_noread\n"),
  275. ok = file:change_mode(Level1("test_noread"), 8#0333),
  276. ok = file:write_file(Level1("test.html"), "test.html\n"),
  277. ok = file:make_dir(Level1("test_dir")),
  278. [{static_dir, Dir}|Config].
  279. end_static_dir(Config) ->
  280. Dir = ?config(static_dir, Config),
  281. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  282. ok = file:delete(Level1("test_file")),
  283. ok = file:delete(Level1("test_file.css")),
  284. ok = file:delete(Level1("test_noread")),
  285. ok = file:delete(Level1("test.html")),
  286. ok = file:del_dir(Level1("test_dir")),
  287. ok = file:del_dir(Dir),
  288. Config.
  289. %% Convenience functions.
  290. quick_raw(Data, Config) ->
  291. Client = ?config(client, Config),
  292. Transport = ?config(transport, Config),
  293. {ok, Client2} = cowboy_client:connect(
  294. Transport, "localhost", ?config(port, Config), Client),
  295. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  296. case cowboy_client:response(Client3) of
  297. {ok, Status, _, _} -> Status;
  298. {error, _} -> closed
  299. end.
  300. build_url(Path, Config) ->
  301. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  302. {port, Port} = lists:keyfind(port, 1, Config),
  303. PortBin = list_to_binary(integer_to_list(Port)),
  304. PathBin = list_to_binary(Path),
  305. << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
  306. quick_get(URL, Config) ->
  307. Client = ?config(client, Config),
  308. {ok, Client2} = cowboy_client:request(<<"GET">>,
  309. build_url(URL, Config), Client),
  310. {ok, Status, _, _} = cowboy_client:response(Client2),
  311. Status.
  312. body_to_chunks(_, <<>>, Acc) ->
  313. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  314. body_to_chunks(ChunkSize, Body, Acc) ->
  315. BodySize = byte_size(Body),
  316. ChunkSize2 = case BodySize < ChunkSize of
  317. true -> BodySize;
  318. false -> ChunkSize
  319. end,
  320. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  321. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  322. body_to_chunks(ChunkSize, Rest,
  323. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  324. %% Tests.
  325. check_raw_status(Config) ->
  326. Huge = [$0 || _ <- lists:seq(1, 5000)],
  327. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  328. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  329. ResponsePacket =
  330. "HTTP/1.0 302 Found\r
  331. Location: http://www.google.co.il/\r
  332. Cache-Control: private\r
  333. Content-Type: text/html; charset=UTF-8\r
  334. 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
  335. Date: Sun, 04 Dec 2011 15:55:01 GMT\r
  336. Server: gws\r
  337. Content-Length: 221\r
  338. X-XSS-Protection: 1; mode=block\r
  339. X-Frame-Options: SAMEORIGIN\r
  340. \r
  341. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  342. <TITLE>302 Moved</TITLE></HEAD><BODY>
  343. <H1>302 Moved</H1>
  344. The document has moved
  345. <A HREF=\"http://www.google.co.il/\">here</A>.
  346. </BODY></HTML>",
  347. Tests = [
  348. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  349. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  350. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  351. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  352. {400, "\n"},
  353. {400, "Garbage\r\n\r\n"},
  354. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  355. {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
  356. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  357. {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
  358. {505, ResponsePacket},
  359. {408, "GET / HTTP/1.1\r\n"},
  360. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  361. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  362. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  363. {414, Huge},
  364. {400, "GET / HTTP/1.1\r\n" ++ Huge},
  365. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  366. {closed, ""},
  367. {closed, "\r\n"},
  368. {closed, "\r\n\r\n"},
  369. {closed, "GET / HTTP/1.1"}
  370. ],
  371. _ = [{Status, Packet} = begin
  372. Ret = quick_raw(Packet, Config),
  373. {Ret, Packet}
  374. end || {Status, Packet} <- Tests].
  375. check_status(Config) ->
  376. Tests = [
  377. {102, "/long_polling"},
  378. {200, "/"},
  379. {200, "/simple"},
  380. {204, "/loop_timeout"},
  381. {400, "/static/%2f"},
  382. {400, "/static/%2e"},
  383. {400, "/static/%2e%2e"},
  384. {403, "/static/test_dir"},
  385. {403, "/static/test_dir/"},
  386. {403, "/static/test_noread"},
  387. {404, "/not/found"},
  388. {404, "/static/not_found"},
  389. {500, "/handler_errors?case=handler_before_reply"},
  390. {500, "/handler_errors?case=init_before_reply"},
  391. {666, "/init_shutdown"}
  392. ],
  393. _ = [{Status, URL} = begin
  394. Ret = quick_get(URL, Config),
  395. {Ret, URL}
  396. end || {Status, URL} <- Tests].
  397. %% @todo Convert to cowboy_client.
  398. chunked_response(Config) ->
  399. {ok, {{"HTTP/1.1", 200, "OK"}, _, "chunked_handler\r\nworks fine!"}}
  400. = httpc:request(binary_to_list(build_url("/chunked_response", Config))).
  401. %% Check if sending requests whose size is around the MTU breaks something.
  402. echo_body(Config) ->
  403. Client = ?config(client, Config),
  404. {ok, [{mtu, MTU}]} = inet:ifget("lo", [mtu]),
  405. [begin
  406. Body = list_to_binary(lists:duplicate(Size, $a)),
  407. {ok, Client2} = cowboy_client:request(<<"POST">>,
  408. build_url("/echo/body", Config),
  409. [{<<"connection">>, <<"close">>}],
  410. Body, Client),
  411. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  412. {ok, Body, _} = cowboy_client:response_body(Client3)
  413. end || Size <- lists:seq(MTU - 500, MTU)].
  414. error_chain_handle_after_reply(Config) ->
  415. Client = ?config(client, Config),
  416. {ok, Client2} = cowboy_client:request(<<"GET">>,
  417. build_url("/", Config), Client),
  418. {ok, Client3} = cowboy_client:request(<<"GET">>,
  419. build_url("/handler_errors?case=handle_after_reply", Config), Client2),
  420. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  421. {ok, 200, _, Client5} = cowboy_client:response(Client4),
  422. {error, closed} = cowboy_client:response(Client5).
  423. error_chain_handle_before_reply(Config) ->
  424. Client = ?config(client, Config),
  425. {ok, Client2} = cowboy_client:request(<<"GET">>,
  426. build_url("/", Config), Client),
  427. {ok, Client3} = cowboy_client:request(<<"GET">>,
  428. build_url("/handler_errors?case=handle_before_reply", Config), Client2),
  429. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  430. {ok, 500, _, Client5} = cowboy_client:response(Client4),
  431. {error, closed} = cowboy_client:response(Client5).
  432. error_handle_after_reply(Config) ->
  433. Client = ?config(client, Config),
  434. {ok, Client2} = cowboy_client:request(<<"GET">>,
  435. build_url("/handler_errors?case=handle_after_reply", Config), Client),
  436. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  437. {error, closed} = cowboy_client:response(Client3).
  438. error_init_after_reply(Config) ->
  439. Client = ?config(client, Config),
  440. {ok, Client2} = cowboy_client:request(<<"GET">>,
  441. build_url("/handler_errors?case=init_after_reply", Config), Client),
  442. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  443. {error, closed} = cowboy_client:response(Client3).
  444. error_init_reply_handle_error(Config) ->
  445. Client = ?config(client, Config),
  446. {ok, Client2} = cowboy_client:request(<<"GET">>,
  447. build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
  448. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  449. {error, closed} = cowboy_client:response(Client3).
  450. headers_dupe(Config) ->
  451. Client = ?config(client, Config),
  452. {ok, Client2} = cowboy_client:request(<<"GET">>,
  453. build_url("/headers/dupe", Config), Client),
  454. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  455. {<<"connection">>, <<"close">>}
  456. = lists:keyfind(<<"connection">>, 1, Headers),
  457. Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
  458. 1 = length(Connections),
  459. {error, closed} = cowboy_client:response(Client3).
  460. http10_chunkless(Config) ->
  461. Client = ?config(client, Config),
  462. Transport = ?config(transport, Config),
  463. {ok, Client2} = cowboy_client:connect(
  464. Transport, "localhost", ?config(port, Config), Client),
  465. Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
  466. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  467. {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
  468. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  469. %% Hack: we just try to get 28 bytes and compare.
  470. {ok, Transport, Socket} = cowboy_client:transport(Client4),
  471. Buffer = element(7, Client4),
  472. Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
  473. {ok, Recv} -> << Buffer/binary, Recv/binary >>;
  474. _ -> Buffer
  475. end,
  476. <<"chunked_handler\r\nworks fine!">> = Buffer2.
  477. http10_hostless(Config) ->
  478. Port10 = ?config(port, Config) + 10,
  479. Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
  480. ranch:start_listener(Name, 5,
  481. ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}],
  482. cowboy_protocol, [
  483. {env, [{dispatch, [{'_', [
  484. {[<<"http1.0">>, <<"hostless">>], http_handler, []}]}]}]},
  485. {max_keepalive, 50},
  486. {timeout, 500}]
  487. ),
  488. 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  489. [{port, Port10}|Config]),
  490. cowboy:stop_listener(http10).
  491. keepalive_max(Config) ->
  492. Client = ?config(client, Config),
  493. URL = build_url("/", Config),
  494. ok = keepalive_max_loop(Client, URL, 50).
  495. keepalive_max_loop(_, _, 0) ->
  496. ok;
  497. keepalive_max_loop(Client, URL, N) ->
  498. Headers = [{<<"connection">>, <<"keep-alive">>}],
  499. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  500. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  501. Expected = case N of
  502. 1 -> <<"close">>;
  503. N -> <<"keep-alive">>
  504. end,
  505. {<<"connection">>, Expected}
  506. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  507. keepalive_max_loop(Client3, URL, N - 1).
  508. keepalive_nl(Config) ->
  509. Client = ?config(client, Config),
  510. URL = build_url("/", Config),
  511. ok = keepalive_nl_loop(Client, URL, 10).
  512. keepalive_nl_loop(_, _, 0) ->
  513. ok;
  514. keepalive_nl_loop(Client, URL, N) ->
  515. Headers = [{<<"connection">>, <<"keep-alive">>}],
  516. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  517. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  518. {<<"connection">>, <<"keep-alive">>}
  519. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  520. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  521. ok = Transport:send(Socket, <<"\r\n">>), %% empty line
  522. keepalive_nl_loop(Client3, URL, N - 1).
  523. multipart(Config) ->
  524. Client = ?config(client, Config),
  525. Body = <<
  526. "This is a preamble."
  527. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  528. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  529. "\r\n--OHai--"
  530. "This is an epiloque."
  531. >>,
  532. {ok, Client2} = cowboy_client:request(<<"POST">>,
  533. build_url("/multipart", Config),
  534. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  535. Body, Client),
  536. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  537. {ok, RespBody, _} = cowboy_client:response_body(Client3),
  538. Parts = binary_to_term(RespBody),
  539. Parts = [
  540. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  541. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  542. ].
  543. nc_reqs(Config, Input) ->
  544. Cat = os:find_executable("cat"),
  545. Nc = os:find_executable("nc"),
  546. case {Cat, Nc} of
  547. {false, _} ->
  548. {skip, {notfound, cat}};
  549. {_, false} ->
  550. {skip, {notfound, nc}};
  551. _Good ->
  552. %% Throw garbage at the server then check if it's still up.
  553. {port, Port} = lists:keyfind(port, 1, Config),
  554. StrPort = integer_to_list(Port),
  555. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  556. || _ <- lists:seq(1, 100)],
  557. 200 = quick_get("/", Config)
  558. end.
  559. nc_rand(Config) ->
  560. nc_reqs(Config, "/dev/urandom").
  561. nc_zero(Config) ->
  562. nc_reqs(Config, "/dev/zero").
  563. onrequest(Config) ->
  564. Client = ?config(client, Config),
  565. {ok, Client2} = cowboy_client:request(<<"GET">>,
  566. build_url("/", Config), Client),
  567. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  568. {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
  569. {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
  570. onrequest_reply(Config) ->
  571. Client = ?config(client, Config),
  572. {ok, Client2} = cowboy_client:request(<<"GET">>,
  573. build_url("/?reply=1", Config), Client),
  574. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  575. {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
  576. {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
  577. %% Hook for the above onrequest tests.
  578. onrequest_hook(Req) ->
  579. case cowboy_req:qs_val(<<"reply">>, Req) of
  580. {undefined, Req2} ->
  581. cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
  582. {_, Req2} ->
  583. {ok, Req3} = cowboy_req:reply(
  584. 200, [], <<"replied!">>, Req2),
  585. Req3
  586. end.
  587. onresponse_crash(Config) ->
  588. Client = ?config(client, Config),
  589. {ok, Client2} = cowboy_client:request(<<"GET">>,
  590. build_url("/handler_errors?case=init_before_reply", Config), Client),
  591. {ok, 777, Headers, _} = cowboy_client:response(Client2),
  592. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  593. onresponse_reply(Config) ->
  594. Client = ?config(client, Config),
  595. {ok, Client2} = cowboy_client:request(<<"GET">>,
  596. build_url("/", Config), Client),
  597. {ok, 777, Headers, Client3} = cowboy_client:response(Client2),
  598. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  599. %% Make sure we don't get the body initially sent.
  600. {error, closed} = cowboy_client:response_body(Client3).
  601. %% Hook for the above onresponse tests.
  602. onresponse_hook(_, Headers, _, Req) ->
  603. {ok, Req2} = cowboy_req:reply(
  604. <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
  605. Req2.
  606. pipeline(Config) ->
  607. Client = ?config(client, Config),
  608. {ok, Client2} = cowboy_client:request(<<"GET">>,
  609. build_url("/", Config), Client),
  610. {ok, Client3} = cowboy_client:request(<<"GET">>,
  611. build_url("/", Config), Client2),
  612. {ok, Client4} = cowboy_client:request(<<"GET">>,
  613. build_url("/", Config), Client3),
  614. {ok, Client5} = cowboy_client:request(<<"GET">>,
  615. build_url("/", Config), Client4),
  616. {ok, Client6} = cowboy_client:request(<<"GET">>,
  617. build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
  618. {ok, 200, _, Client7} = cowboy_client:response(Client6),
  619. {ok, 200, _, Client8} = cowboy_client:response(Client7),
  620. {ok, 200, _, Client9} = cowboy_client:response(Client8),
  621. {ok, 200, _, Client10} = cowboy_client:response(Client9),
  622. {ok, 200, _, Client11} = cowboy_client:response(Client10),
  623. {error, closed} = cowboy_client:response(Client11).
  624. rest_bad_accept(Config) ->
  625. Client = ?config(client, Config),
  626. {ok, Client2} = cowboy_client:request(<<"GET">>,
  627. build_url("/bad_accept", Config),
  628. [{<<"accept">>, <<"1">>}],
  629. Client),
  630. {ok, 400, _, _} = cowboy_client:response(Client2).
  631. rest_expires(Config) ->
  632. Client = ?config(client, Config),
  633. {ok, Client2} = cowboy_client:request(<<"GET">>,
  634. build_url("/rest_expires", Config), Client),
  635. {ok, 200, RespHeaders, _} = cowboy_client:response(Client2),
  636. {_, Expires} = lists:keyfind(<<"expires">>, 1, RespHeaders),
  637. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, RespHeaders),
  638. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  639. ok.
  640. rest_keepalive(Config) ->
  641. Client = ?config(client, Config),
  642. URL = build_url("/simple", Config),
  643. ok = rest_keepalive_loop(Client, URL, 10).
  644. rest_keepalive_loop(_, _, 0) ->
  645. ok;
  646. rest_keepalive_loop(Client, URL, N) ->
  647. Headers = [{<<"connection">>, <<"keep-alive">>}],
  648. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  649. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  650. {<<"connection">>, <<"keep-alive">>}
  651. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  652. rest_keepalive_loop(Client3, URL, N - 1).
  653. rest_keepalive_post(Config) ->
  654. Client = ?config(client, Config),
  655. ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
  656. rest_keepalive_post_loop(_, _, _, 0) ->
  657. ok;
  658. rest_keepalive_post_loop(Config, Client, simple_post, N) ->
  659. Headers = [
  660. {<<"connection">>, <<"keep-alive">>},
  661. {<<"content-type">>, <<"text/plain">>}
  662. ],
  663. {ok, Client2} = cowboy_client:request(<<"POST">>,
  664. build_url("/simple_post", Config), Headers, "12345", Client),
  665. {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
  666. {<<"connection">>, <<"keep-alive">>}
  667. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  668. rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
  669. rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
  670. Headers = [
  671. {<<"connection">>, <<"keep-alive">>},
  672. {<<"content-type">>, <<"text/plain">>}
  673. ],
  674. {ok, Client2} = cowboy_client:request(<<"POST">>,
  675. build_url("/forbidden_post", Config), Headers, "12345", Client),
  676. {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
  677. {<<"connection">>, <<"keep-alive">>}
  678. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  679. rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
  680. rest_missing_get_callbacks(Config) ->
  681. Client = ?config(client, Config),
  682. {ok, Client2} = cowboy_client:request(<<"GET">>,
  683. build_url("/missing_get_callbacks", Config), Client),
  684. {ok, 500, _, _} = cowboy_client:response(Client2).
  685. rest_missing_put_callbacks(Config) ->
  686. Client = ?config(client, Config),
  687. {ok, Client2} = cowboy_client:request(<<"PUT">>,
  688. build_url("/missing_put_callbacks", Config),
  689. [{<<"content-type">>, <<"application/json">>}],
  690. <<"{}">>, Client),
  691. {ok, 500, _, _} = cowboy_client:response(Client2).
  692. rest_nodelete(Config) ->
  693. Client = ?config(client, Config),
  694. {ok, Client2} = cowboy_client:request(<<"DELETE">>,
  695. build_url("/nodelete", Config), Client),
  696. {ok, 500, _, _} = cowboy_client:response(Client2).
  697. rest_resource_get_etag(Config, Type) ->
  698. rest_resource_get_etag(Config, Type, []).
  699. rest_resource_get_etag(Config, Type, Headers) ->
  700. Client = ?config(client, Config),
  701. {ok, Client2} = cowboy_client:request(<<"GET">>,
  702. build_url("/resetags?type=" ++ Type, Config), Headers, Client),
  703. {ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
  704. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  705. false -> {Status, false};
  706. {<<"etag">>, ETag} -> {Status, ETag}
  707. end.
  708. rest_resource_etags(Config) ->
  709. Tests = [
  710. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  711. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  712. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  713. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  714. {500, false, "binary-strong-unquoted"},
  715. {500, false, "binary-weak-unquoted"}
  716. ],
  717. _ = [{Status, ETag, Type} = begin
  718. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  719. {Ret, RespETag, Type}
  720. end || {Status, ETag, Type} <- Tests].
  721. rest_resource_etags_if_none_match(Config) ->
  722. Tests = [
  723. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  724. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  725. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  726. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  727. ],
  728. _ = [{Status, Type} = begin
  729. {Ret, _} = rest_resource_get_etag(Config, Type,
  730. [{<<"if-none-match">>, ETag}]),
  731. {Ret, Type}
  732. end || {Status, ETag, Type} <- Tests].
  733. set_resp_body(Config) ->
  734. Client = ?config(client, Config),
  735. {ok, Client2} = cowboy_client:request(<<"GET">>,
  736. build_url("/set_resp/body", Config), Client),
  737. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  738. {ok, <<"A flameless dance does not equal a cycle">>, _}
  739. = cowboy_client:response_body(Client3).
  740. set_resp_header(Config) ->
  741. Client = ?config(client, Config),
  742. {ok, Client2} = cowboy_client:request(<<"GET">>,
  743. build_url("/set_resp/header", Config), Client),
  744. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  745. {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  746. {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
  747. set_resp_overwrite(Config) ->
  748. Client = ?config(client, Config),
  749. {ok, Client2} = cowboy_client:request(<<"GET">>,
  750. build_url("/set_resp/overwrite", Config), Client),
  751. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  752. {<<"server">>, <<"DesireDrive/1.0">>}
  753. = lists:keyfind(<<"server">>, 1, Headers).
  754. slowloris(Config) ->
  755. Client = ?config(client, Config),
  756. Transport = ?config(transport, Config),
  757. {ok, Client2} = cowboy_client:connect(
  758. Transport, "localhost", ?config(port, Config), Client),
  759. try
  760. [begin
  761. {ok, _} = cowboy_client:raw_request([C], Client2),
  762. receive after 25 -> ok end
  763. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  764. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  765. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  766. error(failure)
  767. catch error:{badmatch, _} ->
  768. ok
  769. end.
  770. slowloris2(Config) ->
  771. Client = ?config(client, Config),
  772. Transport = ?config(transport, Config),
  773. {ok, Client2} = cowboy_client:connect(
  774. Transport, "localhost", ?config(port, Config), Client),
  775. {ok, _} = cowboy_client:raw_request("GET / HTTP/1.1\r\n", Client2),
  776. receive after 300 -> ok end,
  777. {ok, _} = cowboy_client:raw_request("Host: localhost\r\n", Client2),
  778. receive after 300 -> ok end,
  779. {ok, 408, _, _} = cowboy_client:response(Client2).
  780. static_attribute_etag(Config) ->
  781. Client = ?config(client, Config),
  782. {ok, Client2} = cowboy_client:request(<<"GET">>,
  783. build_url("/static_attribute_etag/test.html", Config), Client),
  784. {ok, Client3} = cowboy_client:request(<<"GET">>,
  785. build_url("/static_attribute_etag/test.html", Config), Client2),
  786. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  787. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  788. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  789. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  790. false = ETag1 =:= undefined,
  791. ETag1 = ETag2.
  792. static_function_etag(Config) ->
  793. Client = ?config(client, Config),
  794. {ok, Client2} = cowboy_client:request(<<"GET">>,
  795. build_url("/static_function_etag/test.html", Config), Client),
  796. {ok, Client3} = cowboy_client:request(<<"GET">>,
  797. build_url("/static_function_etag/test.html", Config), Client2),
  798. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  799. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  800. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  801. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  802. false = ETag1 =:= undefined,
  803. ETag1 = ETag2.
  804. %% Callback function for generating the ETag for the above test.
  805. static_function_etag(Arguments, etag_data) ->
  806. {_, Filepath} = lists:keyfind(filepath, 1, Arguments),
  807. {_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
  808. {_, _INode} = lists:keyfind(inode, 1, Arguments),
  809. {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
  810. ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
  811. [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
  812. {strong, iolist_to_binary(Checksum)}.
  813. static_mimetypes_function(Config) ->
  814. Client = ?config(client, Config),
  815. {ok, Client2} = cowboy_client:request(<<"GET">>,
  816. build_url("/static_mimetypes_function/test.html", Config), Client),
  817. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  818. {<<"content-type">>, <<"text/html">>}
  819. = lists:keyfind(<<"content-type">>, 1, Headers).
  820. static_specify_file(Config) ->
  821. Client = ?config(client, Config),
  822. {ok, Client2} = cowboy_client:request(<<"GET">>,
  823. build_url("/static_specify_file", Config), Client),
  824. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  825. {<<"content-type">>, <<"text/css">>}
  826. = lists:keyfind(<<"content-type">>, 1, Headers),
  827. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  828. static_specify_file_catchall(Config) ->
  829. Client = ?config(client, Config),
  830. {ok, Client2} = cowboy_client:request(<<"GET">>,
  831. build_url("/static_specify_file/none", Config), Client),
  832. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  833. {<<"content-type">>, <<"text/css">>}
  834. = lists:keyfind(<<"content-type">>, 1, Headers),
  835. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  836. static_test_file(Config) ->
  837. Client = ?config(client, Config),
  838. {ok, Client2} = cowboy_client:request(<<"GET">>,
  839. build_url("/static/test_file", Config), Client),
  840. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  841. {<<"content-type">>, <<"application/octet-stream">>}
  842. = lists:keyfind(<<"content-type">>, 1, Headers).
  843. static_test_file_css(Config) ->
  844. Client = ?config(client, Config),
  845. {ok, Client2} = cowboy_client:request(<<"GET">>,
  846. build_url("/static/test_file.css", Config), Client),
  847. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  848. {<<"content-type">>, <<"text/css">>}
  849. = lists:keyfind(<<"content-type">>, 1, Headers).
  850. stream_body_set_resp(Config) ->
  851. Client = ?config(client, Config),
  852. {ok, Client2} = cowboy_client:request(<<"GET">>,
  853. build_url("/stream_body/set_resp", Config), Client),
  854. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  855. {ok, <<"stream_body_set_resp">>, _}
  856. = cowboy_client:response_body(Client3).
  857. stream_body_set_resp_close(Config) ->
  858. Client = ?config(client, Config),
  859. {ok, Client2} = cowboy_client:request(<<"GET">>,
  860. build_url("/stream_body/set_resp_close", Config), Client),
  861. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  862. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  863. case element(7, Client3) of
  864. <<"stream_body_set_resp_close">> ->
  865. ok;
  866. Buffer ->
  867. {ok, Rest} = Transport:recv(Socket, 26 - size(Buffer), 1000),
  868. <<"stream_body_set_resp_close">> = << Buffer/binary, Rest/binary >>,
  869. ok
  870. end,
  871. {error, closed} = Transport:recv(Socket, 0, 1000).
  872. te_chunked(Config) ->
  873. Client = ?config(client, Config),
  874. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  875. Chunks = body_to_chunks(50, Body, []),
  876. {ok, Client2} = cowboy_client:request(<<"GET">>,
  877. build_url("/echo/body", Config),
  878. [{<<"transfer-encoding">>, <<"chunked">>}],
  879. Chunks, Client),
  880. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  881. {ok, Body, _} = cowboy_client:response_body(Client3).
  882. te_chunked_delayed(Config) ->
  883. Client = ?config(client, Config),
  884. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  885. Chunks = body_to_chunks(50, Body, []),
  886. {ok, Client2} = cowboy_client:request(<<"GET">>,
  887. build_url("/echo/body", Config),
  888. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  889. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  890. _ = [begin
  891. ok = Transport:send(Socket, Chunk),
  892. ok = timer:sleep(10)
  893. end || Chunk <- Chunks],
  894. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  895. {ok, Body, _} = cowboy_client:response_body(Client3).
  896. te_identity(Config) ->
  897. Client = ?config(client, Config),
  898. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  899. {ok, Client2} = cowboy_client:request(<<"GET">>,
  900. build_url("/echo/body", Config), [], Body, Client),
  901. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  902. {ok, Body, _} = cowboy_client:response_body(Client3).