http_SUITE.erl 16 KB

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