http_SUITE.erl 28 KB

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