http_SUITE.erl 28 KB

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