http_SUITE.erl 29 KB

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