http_SUITE.erl 44 KB

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