http_SUITE.erl 37 KB

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