http_SUITE.erl 46 KB

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