http_SUITE.erl 39 KB

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