http_SUITE.erl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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, max_keepalive/1, nc_rand/1, nc_zero/1,
  20. pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1,
  21. set_resp_body/1, stream_body_set_resp/1, response_as_req/1,
  22. static_mimetypes_function/1]). %% http.
  23. -export([http_200/1, http_404/1, file_200/1, file_403/1,
  24. dir_403/1, file_404/1, file_400/1]). %% http and https.
  25. -export([http_10_hostless/1]). %% misc.
  26. -export([rest_simple/1, rest_keepalive/1]). %% rest.
  27. %% ct.
  28. all() ->
  29. [{group, http}, {group, https}, {group, misc}, {group, rest}].
  30. groups() ->
  31. BaseTests = [http_200, http_404, file_200, file_403, dir_403, file_404,
  32. file_400],
  33. [{http, [], [chunked_response, headers_dupe, headers_huge,
  34. keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw,
  35. set_resp_header, set_resp_overwrite,
  36. set_resp_body, response_as_req, stream_body_set_resp,
  37. static_mimetypes_function] ++ BaseTests},
  38. {https, [], BaseTests},
  39. {misc, [], [http_10_hostless]},
  40. {rest, [], [rest_simple, rest_keepalive]}].
  41. init_per_suite(Config) ->
  42. application:start(inets),
  43. application:start(cowboy),
  44. Config.
  45. end_per_suite(_Config) ->
  46. application:stop(cowboy),
  47. application:stop(inets),
  48. ok.
  49. init_per_group(http, Config) ->
  50. Port = 33080,
  51. Config1 = init_static_dir(Config),
  52. cowboy:start_listener(http, 100,
  53. cowboy_tcp_transport, [{port, Port}],
  54. cowboy_http_protocol, [{max_keepalive, 50},
  55. {dispatch, init_http_dispatch(Config1)}]
  56. ),
  57. [{scheme, "http"}, {port, Port}|Config1];
  58. init_per_group(https, Config) ->
  59. Port = 33081,
  60. Config1 = init_static_dir(Config),
  61. application:start(crypto),
  62. application:start(public_key),
  63. application:start(ssl),
  64. DataDir = ?config(data_dir, Config),
  65. cowboy:start_listener(https, 100,
  66. cowboy_ssl_transport, [
  67. {port, Port}, {certfile, DataDir ++ "cert.pem"},
  68. {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
  69. cowboy_http_protocol, [{dispatch, init_https_dispatch(Config1)}]
  70. ),
  71. [{scheme, "https"}, {port, Port}|Config1];
  72. init_per_group(misc, Config) ->
  73. Port = 33082,
  74. cowboy:start_listener(misc, 100,
  75. cowboy_tcp_transport, [{port, Port}],
  76. cowboy_http_protocol, [{dispatch, [{'_', [
  77. {[], http_handler, []}
  78. ]}]}]),
  79. [{port, Port}|Config];
  80. init_per_group(rest, Config) ->
  81. Port = 33083,
  82. cowboy:start_listener(reset, 100,
  83. cowboy_tcp_transport, [{port, Port}],
  84. cowboy_http_protocol, [{dispatch, [{'_', [
  85. {[<<"simple">>], rest_simple_resource, []}
  86. ]}]}]),
  87. [{port, Port}|Config].
  88. end_per_group(https, Config) ->
  89. cowboy:stop_listener(https),
  90. application:stop(ssl),
  91. application:stop(public_key),
  92. application:stop(crypto),
  93. end_static_dir(Config),
  94. ok;
  95. end_per_group(http, Config) ->
  96. cowboy:stop_listener(http),
  97. end_static_dir(Config);
  98. end_per_group(Listener, _Config) ->
  99. cowboy:stop_listener(Listener),
  100. ok.
  101. %% Dispatch configuration.
  102. init_http_dispatch(Config) ->
  103. [
  104. {[<<"localhost">>], [
  105. {[<<"chunked_response">>], chunked_handler, []},
  106. {[<<"init_shutdown">>], http_handler_init_shutdown, []},
  107. {[<<"long_polling">>], http_handler_long_polling, []},
  108. {[<<"headers">>, <<"dupe">>], http_handler,
  109. [{headers, [{<<"Connection">>, <<"close">>}]}]},
  110. {[<<"set_resp">>, <<"header">>], http_handler_set_resp,
  111. [{headers, [{<<"Vary">>, <<"Accept">>}]}]},
  112. {[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
  113. [{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
  114. {[<<"set_resp">>, <<"body">>], http_handler_set_resp,
  115. [{body, <<"A flameless dance does not equal a cycle">>}]},
  116. {[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body,
  117. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  118. {[<<"static">>, '...'], cowboy_http_static,
  119. [{directory, ?config(static_dir, Config)},
  120. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  121. {[<<"static_mimetypes_function">>, '...'], cowboy_http_static,
  122. [{directory, ?config(static_dir, Config)},
  123. {mimetypes, {fun(Path, data) when is_binary(Path) ->
  124. [<<"text/html">>] end, data}}]},
  125. {[], http_handler, []}
  126. ]}
  127. ].
  128. init_https_dispatch(Config) ->
  129. init_http_dispatch(Config).
  130. init_static_dir(Config) ->
  131. Dir = filename:join(?config(priv_dir, Config), "static"),
  132. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  133. ok = file:make_dir(Dir),
  134. ok = file:write_file(Level1("test_file"), "test_file\n"),
  135. ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
  136. ok = file:write_file(Level1("test_noread"), "test_noread\n"),
  137. ok = file:change_mode(Level1("test_noread"), 8#0333),
  138. ok = file:write_file(Level1("test.html"), "test.html\n"),
  139. ok = file:make_dir(Level1("test_dir")),
  140. [{static_dir, Dir}|Config].
  141. end_static_dir(Config) ->
  142. Dir = ?config(static_dir, Config),
  143. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  144. ok = file:delete(Level1("test_file")),
  145. ok = file:delete(Level1("test_file.css")),
  146. ok = file:delete(Level1("test_noread")),
  147. ok = file:delete(Level1("test.html")),
  148. ok = file:del_dir(Level1("test_dir")),
  149. ok = file:del_dir(Dir),
  150. Config.
  151. %% http.
  152. chunked_response(Config) ->
  153. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
  154. httpc:request(build_url("/chunked_response", Config)).
  155. headers_dupe(Config) ->
  156. {port, Port} = lists:keyfind(port, 1, Config),
  157. {ok, Socket} = gen_tcp:connect("localhost", Port,
  158. [binary, {active, false}, {packet, raw}]),
  159. ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
  160. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  161. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  162. {_Start, _Length} = binary:match(Data, <<"Connection: close">>),
  163. nomatch = binary:match(Data, <<"Connection: keep-alive">>),
  164. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  165. headers_huge(Config) ->
  166. Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  167. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]),
  168. {_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n"
  169. "Set-Cookie: ", Cookie, "\r\n\r\n"], Config).
  170. keepalive_nl(Config) ->
  171. {port, Port} = lists:keyfind(port, 1, Config),
  172. {ok, Socket} = gen_tcp:connect("localhost", Port,
  173. [binary, {active, false}, {packet, raw}]),
  174. ok = keepalive_nl_loop(Socket, 10),
  175. ok = gen_tcp:close(Socket).
  176. keepalive_nl_loop(_Socket, 0) ->
  177. ok;
  178. keepalive_nl_loop(Socket, N) ->
  179. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  180. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  181. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  182. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  183. nomatch = binary:match(Data, <<"Connection: close">>),
  184. ok = gen_tcp:send(Socket, "\r\n"), %% extra nl
  185. keepalive_nl_loop(Socket, N - 1).
  186. max_keepalive(Config) ->
  187. {port, Port} = lists:keyfind(port, 1, Config),
  188. {ok, Socket} = gen_tcp:connect("localhost", Port,
  189. [binary, {active, false}, {packet, raw}]),
  190. ok = max_keepalive_loop(Socket, 50),
  191. {error, closed} = gen_tcp:recv(Socket, 0, 1000).
  192. max_keepalive_loop(_Socket, 0) ->
  193. ok;
  194. max_keepalive_loop(Socket, N) ->
  195. ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
  196. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  197. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  198. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  199. case N of
  200. 1 -> {_, _} = binary:match(Data, <<"Connection: close">>);
  201. N -> nomatch = binary:match(Data, <<"Connection: close">>)
  202. end,
  203. keepalive_nl_loop(Socket, N - 1).
  204. nc_rand(Config) ->
  205. nc_reqs(Config, "/dev/urandom").
  206. nc_zero(Config) ->
  207. nc_reqs(Config, "/dev/zero").
  208. nc_reqs(Config, Input) ->
  209. Cat = os:find_executable("cat"),
  210. Nc = os:find_executable("nc"),
  211. case {Cat, Nc} of
  212. {false, _} ->
  213. {skip, {notfound, cat}};
  214. {_, false} ->
  215. {skip, {notfound, nc}};
  216. _Good ->
  217. %% Throw garbage at the server then check if it's still up.
  218. {port, Port} = lists:keyfind(port, 1, Config),
  219. [nc_run_req(Port, Input) || _N <- lists:seq(1, 100)],
  220. Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
  221. {Packet, 200} = raw_req(Packet, Config)
  222. end.
  223. nc_run_req(Port, Input) ->
  224. os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)).
  225. pipeline(Config) ->
  226. {port, Port} = lists:keyfind(port, 1, Config),
  227. {ok, Socket} = gen_tcp:connect("localhost", Port,
  228. [binary, {active, false}, {packet, raw}]),
  229. ok = gen_tcp:send(Socket,
  230. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  231. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  232. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  233. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
  234. "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
  235. Data = pipeline_recv(Socket, <<>>),
  236. Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
  237. 5 = length(Reqs),
  238. pipeline_check(Reqs).
  239. pipeline_check([]) ->
  240. ok;
  241. pipeline_check([Req|Tail]) ->
  242. << "HTTP/1.1 200", _Rest/bits >> = Req,
  243. pipeline_check(Tail).
  244. pipeline_recv(Socket, SoFar) ->
  245. case gen_tcp:recv(Socket, 0, 6000) of
  246. {ok, Data} ->
  247. pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
  248. {error, closed} ->
  249. ok = gen_tcp:close(Socket),
  250. SoFar
  251. end.
  252. raw_req(Packet, Config) ->
  253. {port, Port} = lists:keyfind(port, 1, Config),
  254. {ok, Socket} = gen_tcp:connect("localhost", Port,
  255. [binary, {active, false}, {packet, raw}]),
  256. ok = gen_tcp:send(Socket, Packet),
  257. Res = case gen_tcp:recv(Socket, 0, 6000) of
  258. {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} ->
  259. list_to_integer(binary_to_list(Str));
  260. {error, Reason} ->
  261. Reason
  262. end,
  263. gen_tcp:close(Socket),
  264. {Packet, Res}.
  265. raw(Config) ->
  266. Huge = [$0 || _N <- lists:seq(1, 5000)],
  267. Tests = [
  268. {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
  269. {"\n", 400},
  270. {"Garbage\r\n\r\n", 400},
  271. {"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
  272. {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
  273. {"", closed},
  274. {"\r\n", closed},
  275. {"\r\n\r\n", closed},
  276. {"GET / HTTP/1.1", closed},
  277. {"GET / HTTP/1.1\r\n", 408},
  278. {"GET / HTTP/1.1\r\nHost: localhost", 408},
  279. {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
  280. {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
  281. {"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
  282. {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
  283. {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
  284. {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
  285. {Huge, 413},
  286. {"GET / HTTP/1.1\r\n" ++ Huge, 413}
  287. ],
  288. [{Packet, StatusCode} = raw_req(Packet, Config)
  289. || {Packet, StatusCode} <- Tests].
  290. set_resp_header(Config) ->
  291. {port, Port} = lists:keyfind(port, 1, Config),
  292. {ok, Socket} = gen_tcp:connect("localhost", Port,
  293. [binary, {active, false}, {packet, raw}]),
  294. ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n"
  295. "Host: localhost\r\nConnection: close\r\n\r\n"),
  296. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  297. {_, _} = binary:match(Data, <<"Vary: Accept">>),
  298. {_, _} = binary:match(Data, <<"Set-Cookie: ">>).
  299. set_resp_overwrite(Config) ->
  300. {port, Port} = lists:keyfind(port, 1, Config),
  301. {ok, Socket} = gen_tcp:connect("localhost", Port,
  302. [binary, {active, false}, {packet, raw}]),
  303. ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n"
  304. "Host: localhost\r\nConnection: close\r\n\r\n"),
  305. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  306. {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>).
  307. set_resp_body(Config) ->
  308. {port, Port} = lists:keyfind(port, 1, Config),
  309. {ok, Socket} = gen_tcp:connect("localhost", Port,
  310. [binary, {active, false}, {packet, raw}]),
  311. ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n"
  312. "Host: localhost\r\nConnection: close\r\n\r\n"),
  313. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  314. {_Start, _Length} = binary:match(Data, <<"\r\n\r\n"
  315. "A flameless dance does not equal a cycle">>).
  316. response_as_req(Config) ->
  317. Packet =
  318. "HTTP/1.0 302 Found
  319. Location: http://www.google.co.il/
  320. Cache-Control: private
  321. Content-Type: text/html; charset=UTF-8
  322. 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
  323. Date: Sun, 04 Dec 2011 15:55:01 GMT
  324. Server: gws
  325. Content-Length: 221
  326. X-XSS-Protection: 1; mode=block
  327. X-Frame-Options: SAMEORIGIN
  328. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  329. <TITLE>302 Moved</TITLE></HEAD><BODY>
  330. <H1>302 Moved</H1>
  331. The document has moved
  332. <A HREF=\"http://www.google.co.il/\">here</A>.
  333. </BODY></HTML>",
  334. {Packet, 400} = raw_req(Packet, Config).
  335. stream_body_set_resp(Config) ->
  336. {port, Port} = lists:keyfind(port, 1, Config),
  337. {ok, Socket} = gen_tcp:connect("localhost", Port,
  338. [binary, {active, false}, {packet, raw}]),
  339. ok = gen_tcp:send(Socket, "GET /stream_body/set_resp HTTP/1.1\r\n"
  340. "Host: localhost\r\nConnection: close\r\n\r\n"),
  341. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  342. {_Start, _Length} = binary:match(Data, <<"stream_body_set_resp">>).
  343. static_mimetypes_function(Config) ->
  344. TestURL = build_url("/static_mimetypes_function/test.html", Config),
  345. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
  346. httpc:request(TestURL),
  347. "text/html" = ?config("content-type", Headers1).
  348. %% http and https.
  349. build_url(Path, Config) ->
  350. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  351. {port, Port} = lists:keyfind(port, 1, Config),
  352. Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
  353. http_200(Config) ->
  354. {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
  355. httpc:request(build_url("/", Config)).
  356. http_404(Config) ->
  357. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  358. httpc:request(build_url("/not/found", Config)).
  359. file_200(Config) ->
  360. {ok, {{"HTTP/1.1", 200, "OK"}, Headers, "test_file\n"}} =
  361. httpc:request(build_url("/static/test_file", Config)),
  362. "application/octet-stream" = ?config("content-type", Headers),
  363. {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test_file.css\n"}} =
  364. httpc:request(build_url("/static/test_file.css", Config)),
  365. "text/css" = ?config("content-type", Headers1).
  366. file_403(Config) ->
  367. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  368. httpc:request(build_url("/static/test_noread", Config)).
  369. dir_403(Config) ->
  370. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  371. httpc:request(build_url("/static/test_dir", Config)),
  372. {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
  373. httpc:request(build_url("/static/test_dir/", Config)).
  374. file_404(Config) ->
  375. {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
  376. httpc:request(build_url("/static/not_found", Config)).
  377. file_400(Config) ->
  378. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers, _Body}} =
  379. httpc:request(build_url("/static/%2f", Config)),
  380. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers1, _Body1}} =
  381. httpc:request(build_url("/static/%2e", Config)),
  382. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers2, _Body2}} =
  383. httpc:request(build_url("/static/%2e%2e", Config)).
  384. %% misc.
  385. http_10_hostless(Config) ->
  386. Packet = "GET / HTTP/1.0\r\n\r\n",
  387. {Packet, 200} = raw_req(Packet, Config).
  388. %% rest.
  389. rest_simple(Config) ->
  390. Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
  391. {Packet, 200} = raw_req(Packet, Config).
  392. rest_keepalive(Config) ->
  393. {port, Port} = lists:keyfind(port, 1, Config),
  394. {ok, Socket} = gen_tcp:connect("localhost", Port,
  395. [binary, {active, false}, {packet, raw}]),
  396. ok = rest_keepalive_loop(Socket, 100),
  397. ok = gen_tcp:close(Socket).
  398. rest_keepalive_loop(_Socket, 0) ->
  399. ok;
  400. rest_keepalive_loop(Socket, N) ->
  401. ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n"
  402. "Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
  403. {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
  404. {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
  405. nomatch = binary:match(Data, <<"Connection: close">>),
  406. rest_keepalive_loop(Socket, N - 1).