http_SUITE.erl 29 KB

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