http_SUITE.erl 36 KB

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