http_SUITE.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(http_SUITE).
  15. -include_lib("common_test/include/ct.hrl").
  16. -export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
  17. init_per_group/2, end_per_group/2]). %% ct.
  18. -export([chunked_response/1, headers_dupe/1, headers_huge/1,
  19. keepalive_nl/1, nc_rand/1, nc_zero/1, pipeline/1, raw/1,
  20. set_resp_header/1, set_resp_overwrite/1, set_resp_body/1,
  21. response_as_req/1]). %% http.
  22. -export([http_200/1, http_404/1]). %% http and https.
  23. -export([http_10_hostless/1]). %% misc.
  24. -export([rest_simple/1, rest_keepalive/1]). %% rest.
  25. %% ct.
  26. all() ->
  27. [{group, http}, {group, https}, {group, misc}, {group, rest}].
  28. groups() ->
  29. BaseTests = [http_200, http_404],
  30. [{http, [], [chunked_response, headers_dupe, headers_huge,
  31. keepalive_nl, nc_rand, nc_zero, pipeline, raw,
  32. set_resp_header, set_resp_overwrite,
  33. set_resp_body, response_as_req] ++ BaseTests},
  34. {https, [], BaseTests},
  35. {misc, [], [http_10_hostless]},
  36. {rest, [], [rest_simple, rest_keepalive]}].
  37. init_per_suite(Config) ->
  38. application:start(inets),
  39. application:start(cowboy),
  40. Config.
  41. end_per_suite(_Config) ->
  42. application:stop(cowboy),
  43. application:stop(inets),
  44. ok.
  45. init_per_group(http, Config) ->
  46. Port = 33080,
  47. cowboy:start_listener(http, 100,
  48. cowboy_tcp_transport, [{port, Port}],
  49. cowboy_http_protocol, [{dispatch, init_http_dispatch()}]
  50. ),
  51. [{scheme, "http"}, {port, Port}|Config];
  52. init_per_group(https, Config) ->
  53. Port = 33081,
  54. application:start(crypto),
  55. application:start(public_key),
  56. application:start(ssl),
  57. DataDir = ?config(data_dir, Config),
  58. cowboy:start_listener(https, 100,
  59. cowboy_ssl_transport, [
  60. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  61. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  62. cowboy_http_protocol, [{dispatch, init_https_dispatch()}]
  63. ),
  64. [{scheme, "https"}, {port, Port}|Config];
  65. init_per_group(misc, Config) ->
  66. Port = 33082,
  67. cowboy:start_listener(misc, 100,
  68. cowboy_tcp_transport, [{port, Port}],
  69. cowboy_http_protocol, [{dispatch, [{'_', [
  70. {[], http_handler, []}
  71. ]}]}]),
  72. [{port, Port}|Config];
  73. init_per_group(rest, Config) ->
  74. Port = 33083,
  75. cowboy:start_listener(reset, 100,
  76. cowboy_tcp_transport, [{port, Port}],
  77. cowboy_http_protocol, [{dispatch, [{'_', [
  78. {[<<"simple">>], rest_simple_resource, []}
  79. ]}]}]),
  80. [{port, Port}|Config].
  81. end_per_group(https, _Config) ->
  82. cowboy:stop_listener(https),
  83. application:stop(ssl),
  84. application:stop(public_key),
  85. application:stop(crypto),
  86. ok;
  87. end_per_group(Listener, _Config) ->
  88. cowboy:stop_listener(Listener),
  89. ok.
  90. %% Dispatch configuration.
  91. init_http_dispatch() ->
  92. [
  93. {[<<"localhost">>], [
  94. {[<<"chunked_response">>], chunked_handler, []},
  95. {[<<"init_shutdown">>], http_handler_init_shutdown, []},
  96. {[<<"long_polling">>], http_handler_long_polling, []},
  97. {[<<"headers">>, <<"dupe">>], http_handler,
  98. [{headers, [{<<"Connection">>, <<"close">>}]}]},
  99. {[<<"set_resp">>, <<"header">>], http_handler_set_resp,
  100. [{headers, [{<<"Vary">>, <<"Accept">>}]}]},
  101. {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
  102. [{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
  103. {[<<"set_resp">>, <<"body">>], http_handler_set_resp,
  104. [{body, <<"A flameless dance does not equal a cycle">>}]},
  105. {[], http_handler, []}
  106. ]}
  107. ].
  108. init_https_dispatch() ->
  109. init_http_dispatch().
  110. %% http.
  111. chunked_response(Config) ->
  112. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
  113. httpc:request(build_url("/chunked_response", Config)).
  114. headers_dupe(Config) ->
  115. {port, Port} = lists:keyfind(port, 1, Config),
  116. {ok, Socket} = gen_tcp:connect("localhost", Port,
  117. [binary, {active, false}, {packet, raw}]),
  118. ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
  119. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  120. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  121. {_Start, _Length} = binary:match(Data, <<"Connection: close">>),
  122. nomatch = binary:match(Data, <<"Connection: keep-alive">>),
  123. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  124. headers_huge(Config) ->
  125. Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  126. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]),
  127. {_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n"
  128. "Set-Cookie: ", Cookie, "\r\n\r\n"], Config).
  129. keepalive_nl(Config) ->
  130. {port, Port} = lists:keyfind(port, 1, Config),
  131. {ok, Socket} = gen_tcp:connect("localhost", Port,
  132. [binary, {active, false}, {packet, raw}]),
  133. ok = keepalive_nl_loop(Socket, 100),
  134. ok = gen_tcp:close(Socket).
  135. keepalive_nl_loop(_Socket, 0) ->
  136. ok;
  137. keepalive_nl_loop(Socket, N) ->
  138. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  139. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  140. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  141. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  142. nomatch = binary:match(Data, <<"Connection: close">>),
  143. ok = gen_tcp:send(Socket, "\r\n"), %% extra nl
  144. keepalive_nl_loop(Socket, N - 1).
  145. nc_rand(Config) ->
  146. nc_reqs(Config, "/dev/urandom").
  147. nc_zero(Config) ->
  148. nc_reqs(Config, "/dev/zero").
  149. nc_reqs(Config, Input) ->
  150. Cat = os:find_executable("cat"),
  151. Nc = os:find_executable("nc"),
  152. case {Cat, Nc} of
  153. {false, _} ->
  154. {skip, {notfound, cat}};
  155. {_, false} ->
  156. {skip, {notfound, nc}};
  157. _Good ->
  158. %% Throw garbage at the server then check if it's still up.
  159. {port, Port} = lists:keyfind(port, 1, Config),
  160. [nc_run_req(Port, Input) || _N <- lists:seq(1, 100)],
  161. Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
  162. {Packet, 200} = raw_req(Packet, Config)
  163. end.
  164. nc_run_req(Port, Input) ->
  165. os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)).
  166. pipeline(Config) ->
  167. {port, Port} = lists:keyfind(port, 1, Config),
  168. {ok, Socket} = gen_tcp:connect("localhost", Port,
  169. [binary, {active, false}, {packet, raw}]),
  170. ok = gen_tcp:send(Socket,
  171. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  172. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  173. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  174. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  175. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  176. Data = pipeline_recv(Socket, <<>>),
  177. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  178. 5 = length(Reqs),
  179. pipeline_check(Reqs).
  180. pipeline_check([]) ->
  181. ok;
  182. pipeline_check([Req|Tail]) ->
  183. << "HTTP/1.1 200", _Rest/bits >> = Req,
  184. pipeline_check(Tail).
  185. pipeline_recv(Socket, SoFar) ->
  186. case gen_tcp:recv(Socket, 0, 6000) of
  187. {ok, Data} ->
  188. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  189. {error, closed} ->
  190. ok = gen_tcp:close(Socket),
  191. SoFar
  192. end.
  193. raw_req(Packet, Config) ->
  194. {port, Port} = lists:keyfind(port, 1, Config),
  195. {ok, Socket} = gen_tcp:connect("localhost", Port,
  196. [binary, {active, false}, {packet, raw}]),
  197. ok = gen_tcp:send(Socket, Packet),
  198. Res = case gen_tcp:recv(Socket, 0, 6000) of
  199. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} ->
  200. list_to_integer(binary_to_list(Str));
  201. {error, Reason} ->
  202. Reason
  203. end,
  204. gen_tcp:close(Socket),
  205. {Packet, Res}.
  206. raw(Config) ->
  207. Huge = [$0 || _N <- lists:seq(1, 5000)],
  208. Tests = [
  209. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  210. {"\n", 400},
  211. {"Garbage\r\n\r\n", 400},
  212. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  213. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  214. {"", closed},
  215. {"\r\n", closed},
  216. {"\r\n\r\n", closed},
  217. {"GET / HTTP/1.1", closed},
  218. {"GET / HTTP/1.1\r\n", 408},
  219. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  220. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  221. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  222. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  223. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
  224. {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
  225. {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
  226. {Huge, 413},
  227. {"GET / HTTP/1.1\r\n" ++ Huge, 413}
  228. ],
  229. [{Packet, StatusCode} = raw_req(Packet, Config)
  230. || {Packet, StatusCode} <- Tests].
  231. set_resp_header(Config) ->
  232. {port, Port} = lists:keyfind(port, 1, Config),
  233. {ok, Socket} = gen_tcp:connect("localhost", Port,
  234. [binary, {active, false}, {packet, raw}]),
  235. ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n"
  236. "Host: localhost\r\nConnection: close\r\n\r\n"),
  237. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  238. {_, _} = binary:match(Data, <<"Vary: Accept">>),
  239. {_, _} = binary:match(Data, <<"Set-Cookie: ">>).
  240. set_resp_overwrite(Config) ->
  241. {port, Port} = lists:keyfind(port, 1, Config),
  242. {ok, Socket} = gen_tcp:connect("localhost", Port,
  243. [binary, {active, false}, {packet, raw}]),
  244. ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n"
  245. "Host: localhost\r\nConnection: close\r\n\r\n"),
  246. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  247. {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>).
  248. set_resp_body(Config) ->
  249. {port, Port} = lists:keyfind(port, 1, Config),
  250. {ok, Socket} = gen_tcp:connect("localhost", Port,
  251. [binary, {active, false}, {packet, raw}]),
  252. ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n"
  253. "Host: localhost\r\nConnection: close\r\n\r\n"),
  254. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  255. {_Start, _Length} = binary:match(Data, <<"\r\n\r\n"
  256. "A flameless dance does not equal a cycle">>).
  257. response_as_req(Config) ->
  258. Packet =
  259. "HTTP/1.0 302 Found
  260. Location: http://www.google.co.il/
  261. Cache-Control: private
  262. Content-Type: text/html; charset=UTF-8
  263. 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
  264. Date: Sun, 04 Dec 2011 15:55:01 GMT
  265. Server: gws
  266. Content-Length: 221
  267. X-XSS-Protection: 1; mode=block
  268. X-Frame-Options: SAMEORIGIN
  269. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  270. <TITLE>302 Moved</TITLE></HEAD><BODY>
  271. <H1>302 Moved</H1>
  272. The document has moved
  273. <A HREF=\"http://www.google.co.il/\">here</A>.
  274. </BODY></HTML>",
  275. {Packet, 400} = raw_req(Packet, Config).
  276. %% http and https.
  277. build_url(Path, Config) ->
  278. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  279. {port, Port} = lists:keyfind(port, 1, Config),
  280. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  281. http_200(Config) ->
  282. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  283. httpc:request(build_url("/", Config)).
  284. http_404(Config) ->
  285. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  286. httpc:request(build_url("/not/found", Config)).
  287. %% misc.
  288. http_10_hostless(Config) ->
  289. Packet = "GET / HTTP/1.0\r\n\r\n",
  290. {Packet, 200} = raw_req(Packet, Config).
  291. %% rest.
  292. rest_simple(Config) ->
  293. Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
  294. {Packet, 200} = raw_req(Packet, Config).
  295. rest_keepalive(Config) ->
  296. {port, Port} = lists:keyfind(port, 1, Config),
  297. {ok, Socket} = gen_tcp:connect("localhost", Port,
  298. [binary, {active, false}, {packet, raw}]),
  299. ok = rest_keepalive_loop(Socket, 100),
  300. ok = gen_tcp:close(Socket).
  301. rest_keepalive_loop(_Socket, 0) ->
  302. ok;
  303. rest_keepalive_loop(Socket, N) ->
  304. ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n"
  305. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  306. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  307. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  308. nomatch = binary:match(Data, <<"Connection: close">>),
  309. rest_keepalive_loop(Socket, N - 1).