http_SUITE.erl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. %% Copyright (c) 2011-2017, Loïc Hoguin <essen@ninenines.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. -compile(export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(cowboy_test, [gun_open/1]).
  19. -import(cowboy_test, [gun_open/2]).
  20. -import(cowboy_test, [gun_down/1]).
  21. -import(cowboy_test, [raw_open/1]).
  22. -import(cowboy_test, [raw_send/2]).
  23. -import(cowboy_test, [raw_recv_head/1]).
  24. -import(cowboy_test, [raw_expect_recv/2]).
  25. %% ct.
  26. all() ->
  27. [
  28. {group, http},
  29. {group, https},
  30. {group, http_compress},
  31. {group, https_compress},
  32. {group, parse_host},
  33. {group, set_env}
  34. ].
  35. groups() ->
  36. Tests = ct_helper:all(?MODULE) -- [
  37. parse_host, set_env_dispatch
  38. ],
  39. [
  40. {http, [], Tests}, %% @todo parallel
  41. {https, [parallel], Tests},
  42. {http_compress, [parallel], Tests},
  43. {https_compress, [parallel], Tests},
  44. {parse_host, [], [
  45. parse_host
  46. ]},
  47. {set_env, [], [
  48. set_env_dispatch
  49. ]}
  50. ].
  51. init_per_group(Name = http, Config) ->
  52. cowboy_test:init_http(Name, #{env => #{dispatch => init_dispatch(Config)}}, Config);
  53. init_per_group(Name = https, Config) ->
  54. cowboy_test:init_https(Name, #{env => #{dispatch => init_dispatch(Config)}}, Config);
  55. init_per_group(Name = http_compress, Config) ->
  56. cowboy_test:init_http(Name, #{
  57. env => #{dispatch => init_dispatch(Config)},
  58. compress => true
  59. }, Config);
  60. init_per_group(Name = https_compress, Config) ->
  61. cowboy_test:init_https(Name, #{
  62. env => #{dispatch => init_dispatch(Config)},
  63. compress => true
  64. }, Config);
  65. init_per_group(parse_host, Config) ->
  66. Dispatch = cowboy_router:compile([
  67. {'_', [
  68. {"/req_attr", http_req_attr, []}
  69. ]}
  70. ]),
  71. {ok, _} = cowboy:start_clear(parse_host, [{port, 0}], #{
  72. env => #{dispatch => Dispatch}
  73. }),
  74. Port = ranch:get_port(parse_host),
  75. [{type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config];
  76. init_per_group(set_env, Config) ->
  77. {ok, _} = cowboy:start_clear(set_env, [{port, 0}], #{
  78. env => #{dispatch => []}
  79. }),
  80. Port = ranch:get_port(set_env),
  81. [{type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config].
  82. end_per_group(Name, _) ->
  83. ok = cowboy:stop_listener(Name).
  84. %% Dispatch configuration.
  85. init_dispatch(Config) ->
  86. cowboy_router:compile([
  87. {"localhost", [
  88. {"/chunked_response", http_chunked, []},
  89. {"/headers/dupe", http_handler,
  90. [{headers, #{<<"connection">> => <<"close">>}}]},
  91. {"/set_resp/header", http_set_resp,
  92. [{headers, #{<<"vary">> => <<"Accept">>}}]},
  93. {"/set_resp/overwrite", http_set_resp,
  94. [{headers, #{<<"server">> => <<"DesireDrive/1.0">>}}]},
  95. {"/set_resp/body", http_set_resp,
  96. [{body, <<"A flameless dance does not equal a cycle">>}]},
  97. {"/handler_errors", http_errors, []},
  98. {"/echo/body", http_echo_body, []},
  99. {"/param_all", rest_param_all, []},
  100. {"/bad_accept", rest_simple_resource, []},
  101. {"/bad_content_type", rest_patch_resource, []},
  102. {"/simple", rest_simple_resource, []},
  103. {"/forbidden_post", rest_forbidden_resource, [true]},
  104. {"/simple_post", rest_forbidden_resource, [false]},
  105. {"/missing_get_callbacks", rest_missing_callbacks, []},
  106. {"/missing_put_callbacks", rest_missing_callbacks, []},
  107. {"/nodelete", rest_nodelete_resource, []},
  108. {"/post_charset", rest_post_charset_resource, []},
  109. {"/postonly", rest_postonly_resource, []},
  110. {"/patch", rest_patch_resource, []},
  111. {"/resetags", rest_resource_etags, []},
  112. {"/rest_expires", rest_expires, []},
  113. {"/rest_expires_binary", rest_expires_binary, []},
  114. {"/rest_empty_resource", rest_empty_resource, []},
  115. {"/loop_stream_recv", http_loop_stream_recv, []},
  116. {"/", http_handler, []}
  117. ]}
  118. ]).
  119. %% Convenience functions.
  120. do_raw(Data, Config) ->
  121. Client = raw_open(Config),
  122. ok = raw_send(Client, Data),
  123. case catch raw_recv_head(Client) of
  124. {'EXIT', _} -> closed;
  125. Resp -> element(2, cow_http:parse_status_line(Resp))
  126. end.
  127. do_get(Path, Config) ->
  128. ConnPid = gun_open(Config),
  129. Ref = gun:get(ConnPid, Path),
  130. {response, _, Status, _} = gun:await(ConnPid, Ref),
  131. gun:close(ConnPid),
  132. Status.
  133. %% Tests.
  134. check_raw_status(Config) ->
  135. Huge = [$0 || _ <- lists:seq(1, 5000)],
  136. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  137. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  138. ResponsePacket =
  139. "HTTP/1.0 302 Found\r
  140. Location: http://www.google.co.il/\r
  141. Cache-Control: private\r
  142. Content-Type: text/html; charset=UTF-8\r
  143. 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\r
  144. Date: Sun, 04 Dec 2011 15:55:01 GMT\r
  145. Server: gws\r
  146. Content-Length: 221\r
  147. X-XSS-Protection: 1; mode=block\r
  148. X-Frame-Options: SAMEORIGIN\r
  149. \r
  150. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  151. <TITLE>302 Moved</TITLE></HEAD><BODY>
  152. <H1>302 Moved</H1>
  153. The document has moved
  154. <A HREF=\"http://www.google.co.il/\">here</A>.
  155. </BODY></HTML>",
  156. Tests = [
  157. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  158. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  159. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  160. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  161. {400, "\n"},
  162. {400, "Garbage\r\n\r\n"},
  163. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  164. {400, " / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  165. {400, "GET HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  166. {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
  167. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  168. {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
  169. {400, ResponsePacket},
  170. {408, "GET / HTTP/1.1\r\n"},
  171. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  172. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  173. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  174. {closed, Huge},
  175. {431, "GET / HTTP/1.1\r\n" ++ Huge},
  176. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  177. {closed, ""},
  178. {closed, "\r\n"},
  179. {closed, "\r\n\r\n"},
  180. {closed, "GET / HTTP/1.1"}
  181. ],
  182. _ = [{Status, Packet} = begin
  183. Ret = do_raw(Packet, Config),
  184. {Ret, Packet}
  185. end || {Status, Packet} <- Tests],
  186. ok.
  187. check_status(Config) ->
  188. Tests = [
  189. {200, "/"},
  190. {200, "/simple"},
  191. {404, "/not/found"},
  192. {500, "/handler_errors?case=init_before_reply"}
  193. ],
  194. _ = [{Status, URL} = begin
  195. Ret = do_get(URL, Config),
  196. {Ret, URL}
  197. end || {Status, URL} <- Tests].
  198. %% Check if sending requests whose size is around the MTU breaks something.
  199. echo_body(Config) ->
  200. MTU = ct_helper:get_loopback_mtu(),
  201. _ = [begin
  202. Body = list_to_binary(lists:duplicate(Size, $a)),
  203. ConnPid = gun_open(Config),
  204. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  205. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  206. {ok, Body} = gun:await_body(ConnPid, Ref)
  207. end || Size <- lists:seq(MTU - 500, MTU)],
  208. ok.
  209. %% Check if sending request whose size is bigger than 1000000 bytes causes 413
  210. echo_body_max_length(Config) ->
  211. ConnPid = gun_open(Config),
  212. Ref = gun:post(ConnPid, "/echo/body", [], << 0:10000000/unit:8 >>),
  213. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  214. ok.
  215. error_init_after_reply(Config) ->
  216. ConnPid = gun_open(Config),
  217. Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
  218. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  219. ok.
  220. headers_dupe(Config) ->
  221. ConnPid = gun_open(Config),
  222. Ref = gun:get(ConnPid, "/headers/dupe"),
  223. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  224. %% Ensure that only one connection header was received.
  225. [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
  226. gun_down(ConnPid).
  227. http10_chunkless(Config) ->
  228. ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
  229. Ref = gun:get(ConnPid, "/chunked_response"),
  230. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  231. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  232. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
  233. gun_down(ConnPid).
  234. http10_hostless(Config) ->
  235. Name = http10_hostless,
  236. Port10 = config(port, Config) + 10,
  237. {Transport, Protocol} = case config(type, Config) of
  238. tcp -> {ranch_tcp, cowboy_clear};
  239. ssl -> {ranch_ssl, cowboy_tls}
  240. end,
  241. ranch:start_listener(Name, 5, Transport,
  242. config(opts, Config) ++ [{port, Port10}],
  243. Protocol, #{
  244. env =>#{dispatch => cowboy_router:compile([
  245. {'_', [{"/http1.0/hostless", http_handler, []}]}])},
  246. max_keepalive => 50,
  247. timeout => 500
  248. }),
  249. 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  250. [{port, Port10}|Config]),
  251. cowboy:stop_listener(http10_hostless).
  252. http10_keepalive_default(Config) ->
  253. Normal = "GET / HTTP/1.0\r\nhost: localhost\r\n\r\n",
  254. Client = raw_open(Config),
  255. ok = raw_send(Client, Normal),
  256. case catch raw_recv_head(Client) of
  257. {'EXIT', _} -> error(closed);
  258. Data ->
  259. %% Cowboy always advertises itself as HTTP/1.1.
  260. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  261. {Headers, _} = cow_http:parse_headers(Rest),
  262. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers)
  263. end,
  264. ok = raw_send(Client, Normal),
  265. case catch raw_recv_head(Client) of
  266. {'EXIT', _} -> closed;
  267. _ -> error(not_closed)
  268. end.
  269. http10_keepalive_forced(Config) ->
  270. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  271. Client = raw_open(Config),
  272. ok = raw_send(Client, Keepalive),
  273. case catch raw_recv_head(Client) of
  274. {'EXIT', _} -> error(closed);
  275. Data ->
  276. %% Cowboy always advertises itself as HTTP/1.1.
  277. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  278. {Headers, _} = cow_http:parse_headers(Rest),
  279. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  280. end,
  281. ok = raw_send(Client, Keepalive),
  282. case catch raw_recv_head(Client) of
  283. {'EXIT', Err} -> error({closed, Err});
  284. _ -> ok
  285. end.
  286. keepalive_max(Config) ->
  287. ConnPid = gun_open(Config),
  288. Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
  289. || _ <- lists:seq(1, 99)],
  290. CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  291. _ = [begin
  292. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  293. false = lists:keymember(<<"connection">>, 1, Headers)
  294. end || Ref <- Refs],
  295. {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef),
  296. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  297. gun_down(ConnPid).
  298. keepalive_nl(Config) ->
  299. ConnPid = gun_open(Config),
  300. Refs = [begin
  301. Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  302. gun:dbg_send_raw(ConnPid, <<"\r\n">>),
  303. Ref
  304. end || _ <- lists:seq(1, 10)],
  305. _ = [begin
  306. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  307. false = lists:keymember(<<"connection">>, 1, Headers)
  308. end || Ref <- Refs],
  309. ok.
  310. keepalive_stream_loop(Config) ->
  311. ConnPid = gun_open(Config),
  312. Refs = [begin
  313. Ref = gun:post(ConnPid, "/loop_stream_recv",
  314. [{<<"content-type">>, <<"application/octet-stream">>}]),
  315. _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
  316. || ID <- lists:seq(1, 250)],
  317. gun:data(ConnPid, Ref, fin, <<>>),
  318. Ref
  319. end || _ <- lists:seq(1, 10)],
  320. _ = [begin
  321. {response, fin, 200, _} = gun:await(ConnPid, Ref)
  322. end || Ref <- Refs],
  323. ok.
  324. do_nc(Config, Input) ->
  325. Cat = os:find_executable("cat"),
  326. Nc = os:find_executable("nc"),
  327. case {Cat, Nc} of
  328. {false, _} ->
  329. {skip, {notfound, cat}};
  330. {_, false} ->
  331. {skip, {notfound, nc}};
  332. _Good ->
  333. %% Throw garbage at the server then check if it's still up.
  334. StrPort = integer_to_list(config(port, Config)),
  335. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  336. || _ <- lists:seq(1, 100)],
  337. 200 = do_get("/", Config)
  338. end.
  339. nc_rand(Config) ->
  340. do_nc(Config, "/dev/urandom").
  341. nc_zero(Config) ->
  342. do_nc(Config, "/dev/zero").
  343. parse_host(Config) ->
  344. ConnPid = gun_open(Config),
  345. Tests = [
  346. {<<"example.org:8080">>, <<"example.org\n8080">>},
  347. {<<"example.org">>, <<"example.org\n80">>},
  348. {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
  349. {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
  350. {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
  351. {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
  352. {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
  353. {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
  354. ],
  355. [begin
  356. Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
  357. [{<<"host">>, Host}]),
  358. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  359. {ok, Body} = gun:await_body(ConnPid, Ref)
  360. end || {Host, Body} <- Tests],
  361. ok.
  362. pipeline(Config) ->
  363. ConnPid = gun_open(Config),
  364. Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
  365. _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  366. ok.
  367. rest_param_all(Config) ->
  368. ConnPid = gun_open(Config),
  369. %% Accept without param.
  370. Ref1 = gun:get(ConnPid, "/param_all",
  371. [{<<"accept">>, <<"text/plain">>}]),
  372. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  373. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  374. %% Accept with param.
  375. Ref2 = gun:get(ConnPid, "/param_all",
  376. [{<<"accept">>, <<"text/plain;level=1">>}]),
  377. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  378. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  379. %% Accept with param and quality.
  380. Ref3 = gun:get(ConnPid, "/param_all",
  381. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  382. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  383. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  384. Ref4 = gun:get(ConnPid, "/param_all",
  385. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  386. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  387. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  388. %% Without Accept.
  389. Ref5 = gun:get(ConnPid, "/param_all"),
  390. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  391. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  392. %% Content-Type without param.
  393. Ref6 = gun:put(ConnPid, "/param_all",
  394. [{<<"content-type">>, <<"text/plain">>}]),
  395. gun:data(ConnPid, Ref6, fin, "Hello world!"),
  396. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  397. %% Content-Type with param.
  398. Ref7 = gun:put(ConnPid, "/param_all",
  399. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  400. gun:data(ConnPid, Ref7, fin, "Hello world!"),
  401. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  402. ok.
  403. rest_bad_accept(Config) ->
  404. ConnPid = gun_open(Config),
  405. Ref = gun:get(ConnPid, "/bad_accept",
  406. [{<<"accept">>, <<"1">>}]),
  407. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  408. ok.
  409. rest_bad_content_type(Config) ->
  410. ConnPid = gun_open(Config),
  411. Ref = gun:patch(ConnPid, "/bad_content_type",
  412. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  413. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  414. ok.
  415. rest_expires(Config) ->
  416. ConnPid = gun_open(Config),
  417. Ref = gun:get(ConnPid, "/rest_expires"),
  418. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  419. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  420. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  421. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  422. ok.
  423. rest_expires_binary(Config) ->
  424. ConnPid = gun_open(Config),
  425. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  426. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  427. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  428. ok.
  429. rest_last_modified_undefined(Config) ->
  430. ConnPid = gun_open(Config),
  431. Ref = gun:get(ConnPid, "/simple",
  432. [{<<"if-modified-since">>, <<"Fri, 21 Sep 2012 22:36:14 GMT">>}]),
  433. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  434. ok.
  435. rest_keepalive(Config) ->
  436. ConnPid = gun_open(Config),
  437. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  438. _ = [begin
  439. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  440. false = lists:keymember(<<"connection">>, 1, Headers)
  441. end || Ref <- Refs],
  442. ok.
  443. rest_keepalive_post(Config) ->
  444. ConnPid = gun_open(Config),
  445. Refs = [begin
  446. Ref1 = gun:post(ConnPid, "/forbidden_post", [{<<"content-type">>, <<"text/plain">>}]),
  447. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  448. Ref2 = gun:post(ConnPid, "/simple_post", [{<<"content-type">>, <<"text/plain">>}]),
  449. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  450. {Ref1, Ref2}
  451. end || _ <- lists:seq(1, 5)],
  452. _ = [begin
  453. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  454. false = lists:keymember(<<"connection">>, 1, Headers1),
  455. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  456. false = lists:keymember(<<"connection">>, 1, Headers2)
  457. end || {Ref1, Ref2} <- Refs],
  458. ok.
  459. rest_missing_get_callbacks(Config) ->
  460. ConnPid = gun_open(Config),
  461. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  462. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  463. ok.
  464. rest_missing_put_callbacks(Config) ->
  465. ConnPid = gun_open(Config),
  466. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  467. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  468. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  469. ok.
  470. rest_nodelete(Config) ->
  471. ConnPid = gun_open(Config),
  472. Ref = gun:delete(ConnPid, "/nodelete"),
  473. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  474. ok.
  475. rest_options_default(Config) ->
  476. ConnPid = gun_open(Config),
  477. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  478. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  479. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  480. ok.
  481. rest_patch(Config) ->
  482. Tests = [
  483. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  484. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  485. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  486. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  487. ],
  488. ConnPid = gun_open(Config),
  489. _ = [begin
  490. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  491. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  492. end || {Status, Headers, Body} <- Tests],
  493. ok.
  494. rest_post_charset(Config) ->
  495. ConnPid = gun_open(Config),
  496. Ref = gun:post(ConnPid, "/post_charset",
  497. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  498. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  499. ok.
  500. rest_postonly(Config) ->
  501. ConnPid = gun_open(Config),
  502. Ref = gun:post(ConnPid, "/postonly",
  503. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  504. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  505. ok.
  506. rest_resource_get_etag(Config, Type) ->
  507. rest_resource_get_etag(Config, Type, []).
  508. rest_resource_get_etag(Config, Type, Headers) ->
  509. ConnPid = gun_open(Config),
  510. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  511. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  512. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  513. false -> {Status, false};
  514. {<<"etag">>, ETag} -> {Status, ETag}
  515. end.
  516. rest_resource_etags(Config) ->
  517. Tests = [
  518. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  519. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  520. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  521. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  522. {400, false, "binary-strong-unquoted"},
  523. {400, false, "binary-weak-unquoted"}
  524. ],
  525. _ = [{Status, ETag, Type} = begin
  526. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  527. {Ret, RespETag, Type}
  528. end || {Status, ETag, Type} <- Tests].
  529. rest_resource_etags_if_none_match(Config) ->
  530. Tests = [
  531. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  532. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  533. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  534. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  535. ],
  536. _ = [{Status, Type} = begin
  537. {Ret, _} = rest_resource_get_etag(Config, Type,
  538. [{<<"if-none-match">>, ETag}]),
  539. {Ret, Type}
  540. end || {Status, ETag, Type} <- Tests].
  541. set_env_dispatch(Config) ->
  542. ConnPid1 = gun_open(Config),
  543. Ref1 = gun:get(ConnPid1, "/"),
  544. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  545. ok = cowboy:set_env(set_env, dispatch,
  546. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  547. ConnPid2 = gun_open(Config),
  548. Ref2 = gun:get(ConnPid2, "/"),
  549. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  550. ok.
  551. set_resp_body(Config) ->
  552. ConnPid = gun_open(Config),
  553. Ref = gun:get(ConnPid, "/set_resp/body"),
  554. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  555. {ok, <<"A flameless dance does not equal a cycle">>}
  556. = gun:await_body(ConnPid, Ref),
  557. ok.
  558. set_resp_header(Config) ->
  559. ConnPid = gun_open(Config),
  560. Ref = gun:get(ConnPid, "/set_resp/header"),
  561. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  562. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  563. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  564. ok.
  565. set_resp_overwrite(Config) ->
  566. ConnPid = gun_open(Config),
  567. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  568. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  569. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  570. ok.
  571. slowloris(Config) ->
  572. Client = raw_open(Config),
  573. try
  574. [begin
  575. ok = raw_send(Client, [C]),
  576. receive after 250 -> ok end
  577. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  578. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  579. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  580. error(failure)
  581. catch error:{badmatch, _} ->
  582. ok
  583. end.
  584. slowloris2(Config) ->
  585. Client = raw_open(Config),
  586. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  587. receive after 300 -> ok end,
  588. ok = raw_send(Client, "Host: localhost\r\n"),
  589. receive after 300 -> ok end,
  590. Data = raw_recv_head(Client),
  591. {_, 408, _, _} = cow_http:parse_status_line(Data),
  592. ok.
  593. te_chunked(Config) ->
  594. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  595. ConnPid = gun_open(Config),
  596. Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]),
  597. gun:data(ConnPid, Ref, fin, Body),
  598. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  599. {ok, Body} = gun:await_body(ConnPid, Ref),
  600. ok.
  601. do_body_to_chunks(_, <<>>, Acc) ->
  602. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  603. do_body_to_chunks(ChunkSize, Body, Acc) ->
  604. BodySize = byte_size(Body),
  605. ChunkSize2 = case BodySize < ChunkSize of
  606. true -> BodySize;
  607. false -> ChunkSize
  608. end,
  609. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  610. ChunkSizeBin = integer_to_binary(ChunkSize2, 16),
  611. do_body_to_chunks(ChunkSize, Rest,
  612. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  613. te_chunked_chopped(Config) ->
  614. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  615. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  616. ConnPid = gun_open(Config),
  617. Ref = gun:post(ConnPid, "/echo/body",
  618. [{<<"content-type">>, <<"text/plain">>}]),
  619. _ = [begin
  620. ok = gun:dbg_send_raw(ConnPid, << C >>),
  621. receive after 10 -> ok end
  622. end || << C >> <= Body2],
  623. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  624. {ok, Body} = gun:await_body(ConnPid, Ref),
  625. ok.
  626. te_chunked_delayed(Config) ->
  627. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  628. Chunks = do_body_to_chunks(50, Body, []),
  629. ConnPid = gun_open(Config),
  630. Ref = gun:post(ConnPid, "/echo/body",
  631. [{<<"content-type">>, <<"text/plain">>}]),
  632. _ = [begin
  633. ok = gun:dbg_send_raw(ConnPid, Chunk),
  634. receive after 10 -> ok end
  635. end || Chunk <- Chunks],
  636. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  637. {ok, Body} = gun:await_body(ConnPid, Ref),
  638. ok.
  639. te_chunked_split_body(Config) ->
  640. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  641. Chunks = do_body_to_chunks(50, Body, []),
  642. ConnPid = gun_open(Config),
  643. Ref = gun:post(ConnPid, "/echo/body",
  644. [{<<"content-type">>, <<"text/plain">>}]),
  645. _ = [begin
  646. case Chunk of
  647. <<"0\r\n\r\n">> ->
  648. ok = gun:dbg_send_raw(ConnPid, Chunk);
  649. _ ->
  650. [Size, ChunkBody, <<>>] =
  651. binary:split(Chunk, [<<"\r\n">>], [global]),
  652. PartASize = random:uniform(byte_size(ChunkBody)),
  653. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  654. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  655. receive after 10 -> ok end,
  656. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  657. end
  658. end || Chunk <- Chunks],
  659. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  660. {ok, Body} = gun:await_body(ConnPid, Ref),
  661. ok.
  662. te_chunked_split_crlf(Config) ->
  663. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  664. Chunks = do_body_to_chunks(50, Body, []),
  665. ConnPid = gun_open(Config),
  666. Ref = gun:post(ConnPid, "/echo/body",
  667. [{<<"content-type">>, <<"text/plain">>}]),
  668. _ = [begin
  669. %% Split in the newline just before the end of the chunk.
  670. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  671. << Chunk2:Len/binary, End/binary >> = Chunk,
  672. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  673. receive after 10 -> ok end,
  674. ok = gun:dbg_send_raw(ConnPid, End)
  675. end || Chunk <- Chunks],
  676. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  677. {ok, Body} = gun:await_body(ConnPid, Ref),
  678. ok.
  679. te_identity(Config) ->
  680. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  681. ConnPid = gun_open(Config),
  682. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  683. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  684. {ok, Body} = gun:await_body(ConnPid, Ref),
  685. ok.