http_SUITE.erl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.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([error_chain_handle_after_reply/1]).
  29. -export([error_chain_handle_before_reply/1]).
  30. -export([error_handle_after_reply/1]).
  31. -export([error_init_after_reply/1]).
  32. -export([error_init_reply_handle_error/1]).
  33. -export([headers_dupe/1]).
  34. -export([http10_chunkless/1]).
  35. -export([http10_hostless/1]).
  36. -export([keepalive_max/1]).
  37. -export([keepalive_nl/1]).
  38. -export([multipart/1]).
  39. -export([nc_rand/1]).
  40. -export([nc_zero/1]).
  41. -export([onrequest/1]).
  42. -export([onrequest_reply/1]).
  43. -export([pipeline/1]).
  44. -export([rest_keepalive/1]).
  45. -export([rest_keepalive_post/1]).
  46. -export([rest_nodelete/1]).
  47. -export([rest_resource_etags/1]).
  48. -export([rest_resource_etags_if_none_match/1]).
  49. -export([set_resp_body/1]).
  50. -export([set_resp_header/1]).
  51. -export([set_resp_overwrite/1]).
  52. -export([static_attribute_etag/1]).
  53. -export([static_function_etag/1]).
  54. -export([static_mimetypes_function/1]).
  55. -export([static_test_file/1]).
  56. -export([static_test_file_css/1]).
  57. -export([static_specify_file/1]).
  58. -export([static_specify_file_catchall/1]).
  59. -export([stream_body_set_resp/1]).
  60. -export([te_chunked/1]).
  61. -export([te_chunked_delayed/1]).
  62. -export([te_identity/1]).
  63. %% ct.
  64. all() ->
  65. [{group, http}, {group, https}, {group, hooks}].
  66. groups() ->
  67. Tests = [
  68. check_raw_status,
  69. check_status,
  70. chunked_response,
  71. error_chain_handle_after_reply,
  72. error_chain_handle_before_reply,
  73. error_handle_after_reply,
  74. error_init_after_reply,
  75. error_init_reply_handle_error,
  76. headers_dupe,
  77. http10_chunkless,
  78. http10_hostless,
  79. keepalive_max,
  80. keepalive_nl,
  81. multipart,
  82. nc_rand,
  83. nc_zero,
  84. pipeline,
  85. rest_keepalive,
  86. rest_keepalive_post,
  87. rest_nodelete,
  88. rest_resource_etags,
  89. rest_resource_etags_if_none_match,
  90. set_resp_body,
  91. set_resp_header,
  92. set_resp_overwrite,
  93. static_attribute_etag,
  94. static_function_etag,
  95. static_mimetypes_function,
  96. static_test_file,
  97. static_test_file_css,
  98. static_specify_file,
  99. static_specify_file_catchall,
  100. stream_body_set_resp,
  101. te_chunked,
  102. te_chunked_delayed,
  103. te_identity
  104. ],
  105. [
  106. {http, [], Tests},
  107. {https, [], Tests},
  108. {hooks, [], [
  109. onrequest,
  110. onrequest_reply
  111. ]}
  112. ].
  113. init_per_suite(Config) ->
  114. application:start(inets),
  115. application:start(cowboy),
  116. Config.
  117. end_per_suite(_Config) ->
  118. application:stop(cowboy),
  119. application:stop(inets),
  120. ok.
  121. init_per_group(http, Config) ->
  122. Port = 33080,
  123. Transport = cowboy_tcp_transport,
  124. Config1 = init_static_dir(Config),
  125. cowboy:start_listener(http, 100,
  126. Transport, [{port, Port}],
  127. cowboy_http_protocol, [
  128. {dispatch, init_dispatch(Config1)},
  129. {max_keepalive, 50},
  130. {timeout, 500}]
  131. ),
  132. {ok, Client} = cowboy_client:init([]),
  133. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  134. {transport, Transport}, {client, Client}|Config1];
  135. init_per_group(https, Config) ->
  136. Port = 33081,
  137. Transport = cowboy_ssl_transport,
  138. Opts = [
  139. {certfile, ?config(data_dir, Config) ++ "cert.pem"},
  140. {keyfile, ?config(data_dir, Config) ++ "key.pem"},
  141. {password, "cowboy"}
  142. ],
  143. Config1 = init_static_dir(Config),
  144. application:start(crypto),
  145. application:start(public_key),
  146. application:start(ssl),
  147. {ok,_} = cowboy:start_listener(https, 100,
  148. Transport, Opts ++ [{port, Port}],
  149. cowboy_http_protocol, [
  150. {dispatch, init_dispatch(Config1)},
  151. {max_keepalive, 50},
  152. {timeout, 500}]
  153. ),
  154. {ok, Client} = cowboy_client:init(Opts),
  155. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  156. {transport, Transport}, {client, Client}|Config1];
  157. init_per_group(hooks, Config) ->
  158. Port = 33082,
  159. Transport = cowboy_tcp_transport,
  160. {ok, _} = cowboy:start_listener(hooks, 100,
  161. Transport, [{port, Port}],
  162. cowboy_http_protocol, [
  163. {dispatch, init_dispatch(Config)},
  164. {max_keepalive, 50},
  165. {onrequest, fun onrequest_hook/1},
  166. {timeout, 500}
  167. ]),
  168. {ok, Client} = cowboy_client:init([]),
  169. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  170. {transport, Transport}, {client, Client}|Config].
  171. end_per_group(https, Config) ->
  172. cowboy:stop_listener(https),
  173. application:stop(ssl),
  174. application:stop(public_key),
  175. application:stop(crypto),
  176. end_static_dir(Config),
  177. ok;
  178. end_per_group(http, Config) ->
  179. cowboy:stop_listener(http),
  180. end_static_dir(Config);
  181. end_per_group(hooks, _) ->
  182. cowboy:stop_listener(hooks),
  183. ok.
  184. %% Dispatch configuration.
  185. init_dispatch(Config) ->
  186. [
  187. {[<<"localhost">>], [
  188. {[<<"chunked_response">>], chunked_handler, []},
  189. {[<<"init_shutdown">>], http_handler_init_shutdown, []},
  190. {[<<"long_polling">>], http_handler_long_polling, []},
  191. {[<<"headers">>, <<"dupe">>], http_handler,
  192. [{headers, [{<<"Connection">>, <<"close">>}]}]},
  193. {[<<"set_resp">>, <<"header">>], http_handler_set_resp,
  194. [{headers, [{<<"Vary">>, <<"Accept">>}]}]},
  195. {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
  196. [{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
  197. {[<<"set_resp">>, <<"body">>], http_handler_set_resp,
  198. [{body, <<"A flameless dance does not equal a cycle">>}]},
  199. {[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body,
  200. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  201. {[<<"static">>, '...'], cowboy_http_static,
  202. [{directory, ?config(static_dir, Config)},
  203. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  204. {[<<"static_mimetypes_function">>, '...'], cowboy_http_static,
  205. [{directory, ?config(static_dir, Config)},
  206. {mimetypes, {fun(Path, data) when is_binary(Path) ->
  207. [<<"text/html">>] end, data}}]},
  208. {[<<"handler_errors">>], http_handler_errors, []},
  209. {[<<"static_attribute_etag">>, '...'], cowboy_http_static,
  210. [{directory, ?config(static_dir, Config)},
  211. {etag, {attributes, [filepath, filesize, inode, mtime]}}]},
  212. {[<<"static_function_etag">>, '...'], cowboy_http_static,
  213. [{directory, ?config(static_dir, Config)},
  214. {etag, {fun static_function_etag/2, etag_data}}]},
  215. {[<<"static_specify_file">>, '...'], cowboy_http_static,
  216. [{directory, ?config(static_dir, Config)},
  217. {mimetypes, [{<<".css">>, [<<"text/css">>]}]},
  218. {file, <<"test_file.css">>}]},
  219. {[<<"multipart">>], http_handler_multipart, []},
  220. {[<<"echo">>, <<"body">>], http_handler_echo_body, []},
  221. {[<<"simple">>], rest_simple_resource, []},
  222. {[<<"forbidden_post">>], rest_forbidden_resource, [true]},
  223. {[<<"simple_post">>], rest_forbidden_resource, [false]},
  224. {[<<"nodelete">>], rest_nodelete_resource, []},
  225. {[<<"resetags">>], rest_resource_etags, []},
  226. {[], http_handler, []}
  227. ]}
  228. ].
  229. init_static_dir(Config) ->
  230. Dir = filename:join(?config(priv_dir, Config), "static"),
  231. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  232. ok = file:make_dir(Dir),
  233. ok = file:write_file(Level1("test_file"), "test_file\n"),
  234. ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
  235. ok = file:write_file(Level1("test_noread"), "test_noread\n"),
  236. ok = file:change_mode(Level1("test_noread"), 8#0333),
  237. ok = file:write_file(Level1("test.html"), "test.html\n"),
  238. ok = file:make_dir(Level1("test_dir")),
  239. [{static_dir, Dir}|Config].
  240. end_static_dir(Config) ->
  241. Dir = ?config(static_dir, Config),
  242. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  243. ok = file:delete(Level1("test_file")),
  244. ok = file:delete(Level1("test_file.css")),
  245. ok = file:delete(Level1("test_noread")),
  246. ok = file:delete(Level1("test.html")),
  247. ok = file:del_dir(Level1("test_dir")),
  248. ok = file:del_dir(Dir),
  249. Config.
  250. %% Convenience functions.
  251. quick_raw(Data, Config) ->
  252. Client = ?config(client, Config),
  253. Transport = ?config(transport, Config),
  254. {ok, Client2} = cowboy_client:connect(
  255. Transport, "localhost", ?config(port, Config), Client),
  256. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  257. case cowboy_client:response(Client3) of
  258. {ok, Status, _, _} -> Status;
  259. {error, _} -> closed
  260. end.
  261. build_url(Path, Config) ->
  262. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  263. {port, Port} = lists:keyfind(port, 1, Config),
  264. PortBin = list_to_binary(integer_to_list(Port)),
  265. PathBin = list_to_binary(Path),
  266. << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
  267. quick_get(URL, Config) ->
  268. Client = ?config(client, Config),
  269. {ok, Client2} = cowboy_client:request(<<"GET">>,
  270. build_url(URL, Config), Client),
  271. {ok, Status, _, _} = cowboy_client:response(Client2),
  272. Status.
  273. body_to_chunks(_, <<>>, Acc) ->
  274. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  275. body_to_chunks(ChunkSize, Body, Acc) ->
  276. BodySize = byte_size(Body),
  277. ChunkSize2 = case BodySize < ChunkSize of
  278. true -> BodySize;
  279. false -> ChunkSize
  280. end,
  281. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  282. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  283. body_to_chunks(ChunkSize, Rest,
  284. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  285. %% Tests.
  286. check_raw_status(Config) ->
  287. Huge = [$0 || _ <- lists:seq(1, 5000)],
  288. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  289. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  290. ResponsePacket =
  291. "HTTP/1.0 302 Found
  292. Location: http://www.google.co.il/
  293. Cache-Control: private
  294. Content-Type: text/html; charset=UTF-8
  295. 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
  296. Date: Sun, 04 Dec 2011 15:55:01 GMT
  297. Server: gws
  298. Content-Length: 221
  299. X-XSS-Protection: 1; mode=block
  300. X-Frame-Options: SAMEORIGIN
  301. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  302. <TITLE>302 Moved</TITLE></HEAD><BODY>
  303. <H1>302 Moved</H1>
  304. The document has moved
  305. <A HREF=\"http://www.google.co.il/\">here</A>.
  306. </BODY></HTML>",
  307. Tests = [
  308. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  309. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  310. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  311. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  312. {400, "\n"},
  313. {400, "Garbage\r\n\r\n"},
  314. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  315. {400, "GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n"},
  316. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  317. {400, ResponsePacket},
  318. {408, "GET / HTTP/1.1\r\n"},
  319. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  320. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  321. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  322. {413, Huge},
  323. {413, "GET / HTTP/1.1\r\n" ++ Huge},
  324. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  325. {closed, ""},
  326. {closed, "\r\n"},
  327. {closed, "\r\n\r\n"},
  328. {closed, "GET / HTTP/1.1"}
  329. ],
  330. _ = [{Status, Packet} = begin
  331. Ret = quick_raw(Packet, Config),
  332. {Ret, Packet}
  333. end || {Status, Packet} <- Tests].
  334. check_status(Config) ->
  335. Tests = [
  336. {102, "/long_polling"},
  337. {200, "/"},
  338. {200, "/simple"},
  339. {400, "/static/%2f"},
  340. {400, "/static/%2e"},
  341. {400, "/static/%2e%2e"},
  342. {403, "/static/test_dir"},
  343. {403, "/static/test_dir/"},
  344. {403, "/static/test_noread"},
  345. {404, "/not/found"},
  346. {404, "/static/not_found"},
  347. {500, "/handler_errors?case=handler_before_reply"},
  348. {500, "/handler_errors?case=init_before_reply"},
  349. {666, "/init_shutdown"}
  350. ],
  351. _ = [{Status, URL} = begin
  352. Ret = quick_get(URL, Config),
  353. {Ret, URL}
  354. end || {Status, URL} <- Tests].
  355. %% @todo Convert to cowboy_client.
  356. chunked_response(Config) ->
  357. {ok, {{"HTTP/1.1", 200, "OK"}, _, "chunked_handler\r\nworks fine!"}}
  358. = httpc:request(binary_to_list(build_url("/chunked_response", Config))).
  359. error_chain_handle_after_reply(Config) ->
  360. Client = ?config(client, Config),
  361. {ok, Client2} = cowboy_client:request(<<"GET">>,
  362. build_url("/", Config), Client),
  363. {ok, Client3} = cowboy_client:request(<<"GET">>,
  364. build_url("/handler_errors?case=handle_after_reply", Config), Client2),
  365. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  366. {ok, 200, _, Client5} = cowboy_client:response(Client4),
  367. {error, closed} = cowboy_client:response(Client5).
  368. error_chain_handle_before_reply(Config) ->
  369. Client = ?config(client, Config),
  370. {ok, Client2} = cowboy_client:request(<<"GET">>,
  371. build_url("/", Config), Client),
  372. {ok, Client3} = cowboy_client:request(<<"GET">>,
  373. build_url("/handler_errors?case=handle_before_reply", Config), Client2),
  374. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  375. {ok, 500, _, Client5} = cowboy_client:response(Client4),
  376. {error, closed} = cowboy_client:response(Client5).
  377. error_handle_after_reply(Config) ->
  378. Client = ?config(client, Config),
  379. {ok, Client2} = cowboy_client:request(<<"GET">>,
  380. build_url("/handler_errors?case=handle_after_reply", Config), Client),
  381. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  382. {error, closed} = cowboy_client:response(Client3).
  383. error_init_after_reply(Config) ->
  384. Client = ?config(client, Config),
  385. {ok, Client2} = cowboy_client:request(<<"GET">>,
  386. build_url("/handler_errors?case=init_after_reply", Config), Client),
  387. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  388. {error, closed} = cowboy_client:response(Client3).
  389. error_init_reply_handle_error(Config) ->
  390. Client = ?config(client, Config),
  391. {ok, Client2} = cowboy_client:request(<<"GET">>,
  392. build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
  393. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  394. {error, closed} = cowboy_client:response(Client3).
  395. headers_dupe(Config) ->
  396. Client = ?config(client, Config),
  397. {ok, Client2} = cowboy_client:request(<<"GET">>,
  398. build_url("/headers/dupe", Config), Client),
  399. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  400. {<<"connection">>, <<"close">>}
  401. = lists:keyfind(<<"connection">>, 1, Headers),
  402. Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
  403. 1 = length(Connections),
  404. {error, closed} = cowboy_client:response(Client3).
  405. http10_chunkless(Config) ->
  406. Client = ?config(client, Config),
  407. Transport = ?config(transport, Config),
  408. {ok, Client2} = cowboy_client:connect(
  409. Transport, "localhost", ?config(port, Config), Client),
  410. Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
  411. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  412. {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
  413. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  414. %% Hack: we just try to get 28 bytes and compare.
  415. {ok, Transport, Socket} = cowboy_client:transport(Client4),
  416. Buffer = element(7, Client4),
  417. Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
  418. {ok, Recv} -> << Buffer/binary, Recv/binary >>;
  419. _ -> Buffer
  420. end,
  421. <<"chunked_handler\r\nworks fine!">> = Buffer2.
  422. http10_hostless(Config) ->
  423. Port10 = ?config(port, Config) + 10,
  424. Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
  425. cowboy:start_listener(Name, 5,
  426. ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}],
  427. cowboy_http_protocol, [
  428. {dispatch, [{'_', [
  429. {[<<"http1.0">>, <<"hostless">>], http_handler, []}]}]},
  430. {max_keepalive, 50},
  431. {timeout, 500}]
  432. ),
  433. 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  434. [{port, Port10}|Config]),
  435. cowboy:stop_listener(http10).
  436. keepalive_max(Config) ->
  437. Client = ?config(client, Config),
  438. URL = build_url("/", Config),
  439. ok = keepalive_max_loop(Client, URL, 50).
  440. keepalive_max_loop(_, _, 0) ->
  441. ok;
  442. keepalive_max_loop(Client, URL, N) ->
  443. Headers = [{<<"connection">>, <<"keep-alive">>}],
  444. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  445. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  446. Expected = case N of
  447. 1 -> <<"close">>;
  448. N -> <<"keep-alive">>
  449. end,
  450. {<<"connection">>, Expected}
  451. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  452. keepalive_max_loop(Client3, URL, N - 1).
  453. keepalive_nl(Config) ->
  454. Client = ?config(client, Config),
  455. URL = build_url("/", Config),
  456. ok = keepalive_nl_loop(Client, URL, 10).
  457. keepalive_nl_loop(_, _, 0) ->
  458. ok;
  459. keepalive_nl_loop(Client, URL, N) ->
  460. Headers = [{<<"connection">>, <<"keep-alive">>}],
  461. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  462. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  463. {<<"connection">>, <<"keep-alive">>}
  464. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  465. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  466. ok = Transport:send(Socket, <<"\r\n">>), %% empty line
  467. keepalive_nl_loop(Client3, URL, N - 1).
  468. multipart(Config) ->
  469. Client = ?config(client, Config),
  470. Body = <<
  471. "This is a preamble."
  472. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  473. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  474. "\r\n--OHai--"
  475. "This is an epiloque."
  476. >>,
  477. {ok, Client2} = cowboy_client:request(<<"POST">>,
  478. build_url("/multipart", Config),
  479. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  480. Body, Client),
  481. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  482. {ok, RespBody, _} = cowboy_client:response_body(Client3),
  483. Parts = binary_to_term(RespBody),
  484. Parts = [
  485. {[{<<"X-Name">>, <<"answer">>}], <<"42">>},
  486. {[{'Server', <<"Cowboy">>}], <<"It rocks!\r\n">>}
  487. ].
  488. nc_reqs(Config, Input) ->
  489. Cat = os:find_executable("cat"),
  490. Nc = os:find_executable("nc"),
  491. case {Cat, Nc} of
  492. {false, _} ->
  493. {skip, {notfound, cat}};
  494. {_, false} ->
  495. {skip, {notfound, nc}};
  496. _Good ->
  497. %% Throw garbage at the server then check if it's still up.
  498. {port, Port} = lists:keyfind(port, 1, Config),
  499. StrPort = integer_to_list(Port),
  500. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  501. || _ <- lists:seq(1, 100)],
  502. 200 = quick_get("/", Config)
  503. end.
  504. nc_rand(Config) ->
  505. nc_reqs(Config, "/dev/urandom").
  506. nc_zero(Config) ->
  507. nc_reqs(Config, "/dev/zero").
  508. onrequest(Config) ->
  509. Client = ?config(client, Config),
  510. {ok, Client2} = cowboy_client:request(<<"GET">>,
  511. build_url("/", Config), Client),
  512. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  513. {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
  514. {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
  515. onrequest_reply(Config) ->
  516. Client = ?config(client, Config),
  517. {ok, Client2} = cowboy_client:request(<<"GET">>,
  518. build_url("/?reply=1", Config), Client),
  519. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  520. {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
  521. {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
  522. %% Hook for the above onrequest tests.
  523. onrequest_hook(Req) ->
  524. case cowboy_http_req:qs_val(<<"reply">>, Req) of
  525. {undefined, Req2} ->
  526. {ok, Req3} = cowboy_http_req:set_resp_header(
  527. 'Server', <<"Serenity">>, Req2),
  528. Req3;
  529. {_, Req2} ->
  530. {ok, Req3} = cowboy_http_req:reply(
  531. 200, [], <<"replied!">>, Req2),
  532. Req3
  533. end.
  534. pipeline(Config) ->
  535. Client = ?config(client, Config),
  536. {ok, Client2} = cowboy_client:request(<<"GET">>,
  537. build_url("/", Config), Client),
  538. {ok, Client3} = cowboy_client:request(<<"GET">>,
  539. build_url("/", Config), Client2),
  540. {ok, Client4} = cowboy_client:request(<<"GET">>,
  541. build_url("/", Config), Client3),
  542. {ok, Client5} = cowboy_client:request(<<"GET">>,
  543. build_url("/", Config), Client4),
  544. {ok, Client6} = cowboy_client:request(<<"GET">>,
  545. build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
  546. {ok, 200, _, Client7} = cowboy_client:response(Client6),
  547. {ok, 200, _, Client8} = cowboy_client:response(Client7),
  548. {ok, 200, _, Client9} = cowboy_client:response(Client8),
  549. {ok, 200, _, Client10} = cowboy_client:response(Client9),
  550. {ok, 200, _, Client11} = cowboy_client:response(Client10),
  551. {error, closed} = cowboy_client:response(Client11).
  552. rest_keepalive(Config) ->
  553. Client = ?config(client, Config),
  554. URL = build_url("/simple", Config),
  555. ok = rest_keepalive_loop(Client, URL, 10).
  556. rest_keepalive_loop(_, _, 0) ->
  557. ok;
  558. rest_keepalive_loop(Client, URL, N) ->
  559. Headers = [{<<"connection">>, <<"keep-alive">>}],
  560. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  561. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  562. {<<"connection">>, <<"keep-alive">>}
  563. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  564. rest_keepalive_loop(Client3, URL, N - 1).
  565. rest_keepalive_post(Config) ->
  566. Client = ?config(client, Config),
  567. ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
  568. rest_keepalive_post_loop(_, _, _, 0) ->
  569. ok;
  570. rest_keepalive_post_loop(Config, Client, simple_post, N) ->
  571. Headers = [
  572. {<<"connection">>, <<"keep-alive">>},
  573. {<<"content-type">>, <<"text/plain">>}
  574. ],
  575. {ok, Client2} = cowboy_client:request(<<"POST">>,
  576. build_url("/simple_post", Config), Headers, "12345", Client),
  577. {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
  578. {<<"connection">>, <<"keep-alive">>}
  579. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  580. rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
  581. rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
  582. Headers = [
  583. {<<"connection">>, <<"keep-alive">>},
  584. {<<"content-type">>, <<"text/plain">>}
  585. ],
  586. {ok, Client2} = cowboy_client:request(<<"POST">>,
  587. build_url("/forbidden_post", Config), Headers, "12345", Client),
  588. {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
  589. {<<"connection">>, <<"keep-alive">>}
  590. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  591. rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
  592. rest_nodelete(Config) ->
  593. Client = ?config(client, Config),
  594. {ok, Client2} = cowboy_client:request(<<"DELETE">>,
  595. build_url("/nodelete", Config), Client),
  596. {ok, 500, _, _} = cowboy_client:response(Client2).
  597. rest_resource_get_etag(Config, Type) ->
  598. rest_resource_get_etag(Config, Type, []).
  599. rest_resource_get_etag(Config, Type, Headers) ->
  600. Client = ?config(client, Config),
  601. {ok, Client2} = cowboy_client:request(<<"GET">>,
  602. build_url("/resetags?type=" ++ Type, Config), Headers, Client),
  603. {ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
  604. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  605. false -> {Status, false};
  606. {<<"etag">>, ETag} -> {Status, ETag}
  607. end.
  608. rest_resource_etags(Config) ->
  609. Tests = [
  610. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  611. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  612. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  613. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  614. {500, false, "binary-strong-unquoted"},
  615. {500, false, "binary-weak-unquoted"}
  616. ],
  617. _ = [{Status, ETag, Type} = begin
  618. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  619. {Ret, RespETag, Type}
  620. end || {Status, ETag, Type} <- Tests].
  621. rest_resource_etags_if_none_match(Config) ->
  622. Tests = [
  623. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  624. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  625. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  626. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  627. ],
  628. _ = [{Status, Type} = begin
  629. {Ret, _} = rest_resource_get_etag(Config, Type,
  630. [{<<"if-none-match">>, ETag}]),
  631. {Ret, Type}
  632. end || {Status, ETag, Type} <- Tests].
  633. set_resp_body(Config) ->
  634. Client = ?config(client, Config),
  635. {ok, Client2} = cowboy_client:request(<<"GET">>,
  636. build_url("/set_resp/body", Config), Client),
  637. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  638. {ok, <<"A flameless dance does not equal a cycle">>, _}
  639. = cowboy_client:response_body(Client3).
  640. set_resp_header(Config) ->
  641. Client = ?config(client, Config),
  642. {ok, Client2} = cowboy_client:request(<<"GET">>,
  643. build_url("/set_resp/header", Config), Client),
  644. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  645. {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  646. {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
  647. set_resp_overwrite(Config) ->
  648. Client = ?config(client, Config),
  649. {ok, Client2} = cowboy_client:request(<<"GET">>,
  650. build_url("/set_resp/overwrite", Config), Client),
  651. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  652. {<<"server">>, <<"DesireDrive/1.0">>}
  653. = lists:keyfind(<<"server">>, 1, Headers).
  654. static_attribute_etag(Config) ->
  655. Client = ?config(client, Config),
  656. {ok, Client2} = cowboy_client:request(<<"GET">>,
  657. build_url("/static_attribute_etag/test.html", Config), Client),
  658. {ok, Client3} = cowboy_client:request(<<"GET">>,
  659. build_url("/static_attribute_etag/test.html", Config), Client2),
  660. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  661. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  662. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  663. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  664. false = ETag1 =:= undefined,
  665. ETag1 = ETag2.
  666. static_function_etag(Config) ->
  667. Client = ?config(client, Config),
  668. {ok, Client2} = cowboy_client:request(<<"GET">>,
  669. build_url("/static_function_etag/test.html", Config), Client),
  670. {ok, Client3} = cowboy_client:request(<<"GET">>,
  671. build_url("/static_function_etag/test.html", Config), Client2),
  672. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  673. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  674. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  675. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  676. false = ETag1 =:= undefined,
  677. ETag1 = ETag2.
  678. %% Callback function for generating the ETag for the above test.
  679. static_function_etag(Arguments, etag_data) ->
  680. {_, Filepath} = lists:keyfind(filepath, 1, Arguments),
  681. {_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
  682. {_, _INode} = lists:keyfind(inode, 1, Arguments),
  683. {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
  684. ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
  685. [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
  686. {strong, iolist_to_binary(Checksum)}.
  687. static_mimetypes_function(Config) ->
  688. Client = ?config(client, Config),
  689. {ok, Client2} = cowboy_client:request(<<"GET">>,
  690. build_url("/static_mimetypes_function/test.html", Config), Client),
  691. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  692. {<<"content-type">>, <<"text/html">>}
  693. = lists:keyfind(<<"content-type">>, 1, Headers).
  694. static_test_file(Config) ->
  695. Client = ?config(client, Config),
  696. {ok, Client2} = cowboy_client:request(<<"GET">>,
  697. build_url("/static/test_file", Config), Client),
  698. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  699. {<<"content-type">>, <<"application/octet-stream">>}
  700. = lists:keyfind(<<"content-type">>, 1, Headers).
  701. static_test_file_css(Config) ->
  702. Client = ?config(client, Config),
  703. {ok, Client2} = cowboy_client:request(<<"GET">>,
  704. build_url("/static/test_file.css", Config), Client),
  705. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  706. {<<"content-type">>, <<"text/css">>}
  707. = lists:keyfind(<<"content-type">>, 1, Headers).
  708. static_specify_file(Config) ->
  709. Client = ?config(client, Config),
  710. {ok, Client2} = cowboy_client:request(<<"GET">>,
  711. build_url("/static_specify_file", Config), Client),
  712. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  713. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  714. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  715. static_specify_file_catchall(Config) ->
  716. Client = ?config(client, Config),
  717. {ok, Client2} = cowboy_client:request(<<"GET">>,
  718. build_url("/static_specify_file/none", Config), Client),
  719. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  720. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  721. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  722. stream_body_set_resp(Config) ->
  723. Client = ?config(client, Config),
  724. {ok, Client2} = cowboy_client:request(<<"GET">>,
  725. build_url("/stream_body/set_resp", Config), Client),
  726. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  727. {ok, <<"stream_body_set_resp">>, _}
  728. = cowboy_client:response_body(Client3).
  729. te_chunked(Config) ->
  730. Client = ?config(client, Config),
  731. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  732. Chunks = body_to_chunks(50, Body, []),
  733. {ok, Client2} = cowboy_client:request(<<"GET">>,
  734. build_url("/echo/body", Config),
  735. [{<<"transfer-encoding">>, <<"chunked">>}],
  736. Chunks, Client),
  737. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  738. {ok, Body, _} = cowboy_client:response_body(Client3).
  739. te_chunked_delayed(Config) ->
  740. Client = ?config(client, Config),
  741. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  742. Chunks = body_to_chunks(50, Body, []),
  743. {ok, Client2} = cowboy_client:request(<<"GET">>,
  744. build_url("/echo/body", Config),
  745. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  746. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  747. _ = [begin
  748. ok = Transport:send(Socket, Chunk),
  749. ok = timer:sleep(10)
  750. end || Chunk <- Chunks],
  751. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  752. {ok, Body, _} = cowboy_client:response_body(Client3).
  753. te_identity(Config) ->
  754. Client = ?config(client, Config),
  755. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  756. {ok, Client2} = cowboy_client:request(<<"GET">>,
  757. build_url("/echo/body", Config), [], Body, Client),
  758. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  759. {ok, Body, _} = cowboy_client:response_body(Client3).