http_SUITE.erl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. chunked_response(Config) ->
  199. ConnPid = gun_open(Config),
  200. Ref = gun:get(ConnPid, "/chunked_response"),
  201. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  202. true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  203. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
  204. ok.
  205. %% Check if sending requests whose size is around the MTU breaks something.
  206. echo_body(Config) ->
  207. MTU = ct_helper:get_loopback_mtu(),
  208. _ = [begin
  209. Body = list_to_binary(lists:duplicate(Size, $a)),
  210. ConnPid = gun_open(Config),
  211. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  212. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  213. {ok, Body} = gun:await_body(ConnPid, Ref)
  214. end || Size <- lists:seq(MTU - 500, MTU)],
  215. ok.
  216. %% Check if sending request whose size is bigger than 1000000 bytes causes 413
  217. echo_body_max_length(Config) ->
  218. ConnPid = gun_open(Config),
  219. Ref = gun:post(ConnPid, "/echo/body", [], << 0:10000000/unit:8 >>),
  220. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  221. ok.
  222. error_init_after_reply(Config) ->
  223. ConnPid = gun_open(Config),
  224. Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
  225. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  226. ok.
  227. headers_dupe(Config) ->
  228. ConnPid = gun_open(Config),
  229. Ref = gun:get(ConnPid, "/headers/dupe"),
  230. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  231. %% Ensure that only one connection header was received.
  232. [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
  233. gun_down(ConnPid).
  234. http10_chunkless(Config) ->
  235. ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
  236. Ref = gun:get(ConnPid, "/chunked_response"),
  237. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  238. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  239. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
  240. gun_down(ConnPid).
  241. http10_hostless(Config) ->
  242. Name = http10_hostless,
  243. Port10 = config(port, Config) + 10,
  244. {Transport, Protocol} = case config(type, Config) of
  245. tcp -> {ranch_tcp, cowboy_clear};
  246. ssl -> {ranch_ssl, cowboy_tls}
  247. end,
  248. ranch:start_listener(Name, 5, Transport,
  249. config(opts, Config) ++ [{port, Port10}],
  250. Protocol, #{
  251. env =>#{dispatch => cowboy_router:compile([
  252. {'_', [{"/http1.0/hostless", http_handler, []}]}])},
  253. max_keepalive => 50,
  254. timeout => 500
  255. }),
  256. 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  257. [{port, Port10}|Config]),
  258. cowboy:stop_listener(http10_hostless).
  259. http10_keepalive_default(Config) ->
  260. Normal = "GET / HTTP/1.0\r\nhost: localhost\r\n\r\n",
  261. Client = raw_open(Config),
  262. ok = raw_send(Client, Normal),
  263. case catch raw_recv_head(Client) of
  264. {'EXIT', _} -> error(closed);
  265. Data ->
  266. %% Cowboy always advertises itself as HTTP/1.1.
  267. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  268. {Headers, _} = cow_http:parse_headers(Rest),
  269. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers)
  270. end,
  271. ok = raw_send(Client, Normal),
  272. case catch raw_recv_head(Client) of
  273. {'EXIT', _} -> closed;
  274. _ -> error(not_closed)
  275. end.
  276. http10_keepalive_forced(Config) ->
  277. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  278. Client = raw_open(Config),
  279. ok = raw_send(Client, Keepalive),
  280. case catch raw_recv_head(Client) of
  281. {'EXIT', _} -> error(closed);
  282. Data ->
  283. %% Cowboy always advertises itself as HTTP/1.1.
  284. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  285. {Headers, _} = cow_http:parse_headers(Rest),
  286. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  287. end,
  288. ok = raw_send(Client, Keepalive),
  289. case catch raw_recv_head(Client) of
  290. {'EXIT', Err} -> error({closed, Err});
  291. _ -> ok
  292. end.
  293. keepalive_max(Config) ->
  294. ConnPid = gun_open(Config),
  295. Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
  296. || _ <- lists:seq(1, 99)],
  297. CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  298. _ = [begin
  299. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  300. false = lists:keymember(<<"connection">>, 1, Headers)
  301. end || Ref <- Refs],
  302. {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef),
  303. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  304. gun_down(ConnPid).
  305. keepalive_nl(Config) ->
  306. ConnPid = gun_open(Config),
  307. Refs = [begin
  308. Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  309. gun:dbg_send_raw(ConnPid, <<"\r\n">>),
  310. Ref
  311. end || _ <- lists:seq(1, 10)],
  312. _ = [begin
  313. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  314. false = lists:keymember(<<"connection">>, 1, Headers)
  315. end || Ref <- Refs],
  316. ok.
  317. keepalive_stream_loop(Config) ->
  318. ConnPid = gun_open(Config),
  319. Refs = [begin
  320. Ref = gun:post(ConnPid, "/loop_stream_recv",
  321. [{<<"content-type">>, <<"application/octet-stream">>}]),
  322. _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
  323. || ID <- lists:seq(1, 250)],
  324. gun:data(ConnPid, Ref, fin, <<>>),
  325. Ref
  326. end || _ <- lists:seq(1, 10)],
  327. _ = [begin
  328. {response, fin, 200, _} = gun:await(ConnPid, Ref)
  329. end || Ref <- Refs],
  330. ok.
  331. do_nc(Config, Input) ->
  332. Cat = os:find_executable("cat"),
  333. Nc = os:find_executable("nc"),
  334. case {Cat, Nc} of
  335. {false, _} ->
  336. {skip, {notfound, cat}};
  337. {_, false} ->
  338. {skip, {notfound, nc}};
  339. _Good ->
  340. %% Throw garbage at the server then check if it's still up.
  341. StrPort = integer_to_list(config(port, Config)),
  342. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  343. || _ <- lists:seq(1, 100)],
  344. 200 = do_get("/", Config)
  345. end.
  346. nc_rand(Config) ->
  347. do_nc(Config, "/dev/urandom").
  348. nc_zero(Config) ->
  349. do_nc(Config, "/dev/zero").
  350. parse_host(Config) ->
  351. ConnPid = gun_open(Config),
  352. Tests = [
  353. {<<"example.org:8080">>, <<"example.org\n8080">>},
  354. {<<"example.org">>, <<"example.org\n80">>},
  355. {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
  356. {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
  357. {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
  358. {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
  359. {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
  360. {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
  361. ],
  362. [begin
  363. Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
  364. [{<<"host">>, Host}]),
  365. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  366. {ok, Body} = gun:await_body(ConnPid, Ref)
  367. end || {Host, Body} <- Tests],
  368. ok.
  369. pipeline(Config) ->
  370. ConnPid = gun_open(Config),
  371. Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
  372. _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  373. ok.
  374. rest_param_all(Config) ->
  375. ConnPid = gun_open(Config),
  376. %% Accept without param.
  377. Ref1 = gun:get(ConnPid, "/param_all",
  378. [{<<"accept">>, <<"text/plain">>}]),
  379. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  380. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  381. %% Accept with param.
  382. Ref2 = gun:get(ConnPid, "/param_all",
  383. [{<<"accept">>, <<"text/plain;level=1">>}]),
  384. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  385. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  386. %% Accept with param and quality.
  387. Ref3 = gun:get(ConnPid, "/param_all",
  388. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  389. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  390. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  391. Ref4 = gun:get(ConnPid, "/param_all",
  392. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  393. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  394. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  395. %% Without Accept.
  396. Ref5 = gun:get(ConnPid, "/param_all"),
  397. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  398. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  399. %% Content-Type without param.
  400. Ref6 = gun:put(ConnPid, "/param_all",
  401. [{<<"content-type">>, <<"text/plain">>}]),
  402. gun:data(ConnPid, Ref6, fin, "Hello world!"),
  403. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  404. %% Content-Type with param.
  405. Ref7 = gun:put(ConnPid, "/param_all",
  406. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  407. gun:data(ConnPid, Ref7, fin, "Hello world!"),
  408. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  409. ok.
  410. rest_bad_accept(Config) ->
  411. ConnPid = gun_open(Config),
  412. Ref = gun:get(ConnPid, "/bad_accept",
  413. [{<<"accept">>, <<"1">>}]),
  414. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  415. ok.
  416. rest_bad_content_type(Config) ->
  417. ConnPid = gun_open(Config),
  418. Ref = gun:patch(ConnPid, "/bad_content_type",
  419. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  420. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  421. ok.
  422. rest_expires(Config) ->
  423. ConnPid = gun_open(Config),
  424. Ref = gun:get(ConnPid, "/rest_expires"),
  425. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  426. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  427. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  428. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  429. ok.
  430. rest_expires_binary(Config) ->
  431. ConnPid = gun_open(Config),
  432. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  433. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  434. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  435. ok.
  436. rest_last_modified_undefined(Config) ->
  437. ConnPid = gun_open(Config),
  438. Ref = gun:get(ConnPid, "/simple",
  439. [{<<"if-modified-since">>, <<"Fri, 21 Sep 2012 22:36:14 GMT">>}]),
  440. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  441. ok.
  442. rest_keepalive(Config) ->
  443. ConnPid = gun_open(Config),
  444. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  445. _ = [begin
  446. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  447. false = lists:keymember(<<"connection">>, 1, Headers)
  448. end || Ref <- Refs],
  449. ok.
  450. rest_keepalive_post(Config) ->
  451. ConnPid = gun_open(Config),
  452. Refs = [begin
  453. Ref1 = gun:post(ConnPid, "/forbidden_post", [{<<"content-type">>, <<"text/plain">>}]),
  454. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  455. Ref2 = gun:post(ConnPid, "/simple_post", [{<<"content-type">>, <<"text/plain">>}]),
  456. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  457. {Ref1, Ref2}
  458. end || _ <- lists:seq(1, 5)],
  459. _ = [begin
  460. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  461. false = lists:keymember(<<"connection">>, 1, Headers1),
  462. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  463. false = lists:keymember(<<"connection">>, 1, Headers2)
  464. end || {Ref1, Ref2} <- Refs],
  465. ok.
  466. rest_missing_get_callbacks(Config) ->
  467. ConnPid = gun_open(Config),
  468. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  469. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  470. ok.
  471. rest_missing_put_callbacks(Config) ->
  472. ConnPid = gun_open(Config),
  473. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  474. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  475. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  476. ok.
  477. rest_nodelete(Config) ->
  478. ConnPid = gun_open(Config),
  479. Ref = gun:delete(ConnPid, "/nodelete"),
  480. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  481. ok.
  482. rest_options_default(Config) ->
  483. ConnPid = gun_open(Config),
  484. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  485. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  486. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  487. ok.
  488. rest_patch(Config) ->
  489. Tests = [
  490. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  491. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  492. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  493. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  494. ],
  495. ConnPid = gun_open(Config),
  496. _ = [begin
  497. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  498. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  499. end || {Status, Headers, Body} <- Tests],
  500. ok.
  501. rest_post_charset(Config) ->
  502. ConnPid = gun_open(Config),
  503. Ref = gun:post(ConnPid, "/post_charset",
  504. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  505. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  506. ok.
  507. rest_postonly(Config) ->
  508. ConnPid = gun_open(Config),
  509. Ref = gun:post(ConnPid, "/postonly",
  510. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  511. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  512. ok.
  513. rest_resource_get_etag(Config, Type) ->
  514. rest_resource_get_etag(Config, Type, []).
  515. rest_resource_get_etag(Config, Type, Headers) ->
  516. ConnPid = gun_open(Config),
  517. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  518. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  519. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  520. false -> {Status, false};
  521. {<<"etag">>, ETag} -> {Status, ETag}
  522. end.
  523. rest_resource_etags(Config) ->
  524. Tests = [
  525. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  526. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  527. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  528. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  529. {400, false, "binary-strong-unquoted"},
  530. {400, false, "binary-weak-unquoted"}
  531. ],
  532. _ = [{Status, ETag, Type} = begin
  533. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  534. {Ret, RespETag, Type}
  535. end || {Status, ETag, Type} <- Tests].
  536. rest_resource_etags_if_none_match(Config) ->
  537. Tests = [
  538. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  539. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  540. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  541. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  542. ],
  543. _ = [{Status, Type} = begin
  544. {Ret, _} = rest_resource_get_etag(Config, Type,
  545. [{<<"if-none-match">>, ETag}]),
  546. {Ret, Type}
  547. end || {Status, ETag, Type} <- Tests].
  548. set_env_dispatch(Config) ->
  549. ConnPid1 = gun_open(Config),
  550. Ref1 = gun:get(ConnPid1, "/"),
  551. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  552. ok = cowboy:set_env(set_env, dispatch,
  553. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  554. ConnPid2 = gun_open(Config),
  555. Ref2 = gun:get(ConnPid2, "/"),
  556. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  557. ok.
  558. set_resp_body(Config) ->
  559. ConnPid = gun_open(Config),
  560. Ref = gun:get(ConnPid, "/set_resp/body"),
  561. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  562. {ok, <<"A flameless dance does not equal a cycle">>}
  563. = gun:await_body(ConnPid, Ref),
  564. ok.
  565. set_resp_header(Config) ->
  566. ConnPid = gun_open(Config),
  567. Ref = gun:get(ConnPid, "/set_resp/header"),
  568. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  569. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  570. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  571. ok.
  572. set_resp_overwrite(Config) ->
  573. ConnPid = gun_open(Config),
  574. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  575. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  576. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  577. ok.
  578. slowloris(Config) ->
  579. Client = raw_open(Config),
  580. try
  581. [begin
  582. ok = raw_send(Client, [C]),
  583. receive after 250 -> ok end
  584. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  585. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  586. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  587. error(failure)
  588. catch error:{badmatch, _} ->
  589. ok
  590. end.
  591. slowloris2(Config) ->
  592. Client = raw_open(Config),
  593. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  594. receive after 300 -> ok end,
  595. ok = raw_send(Client, "Host: localhost\r\n"),
  596. receive after 300 -> ok end,
  597. Data = raw_recv_head(Client),
  598. {_, 408, _, _} = cow_http:parse_status_line(Data),
  599. ok.
  600. te_chunked(Config) ->
  601. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  602. ConnPid = gun_open(Config),
  603. Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]),
  604. gun:data(ConnPid, Ref, fin, Body),
  605. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  606. {ok, Body} = gun:await_body(ConnPid, Ref),
  607. ok.
  608. do_body_to_chunks(_, <<>>, Acc) ->
  609. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  610. do_body_to_chunks(ChunkSize, Body, Acc) ->
  611. BodySize = byte_size(Body),
  612. ChunkSize2 = case BodySize < ChunkSize of
  613. true -> BodySize;
  614. false -> ChunkSize
  615. end,
  616. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  617. ChunkSizeBin = integer_to_binary(ChunkSize2, 16),
  618. do_body_to_chunks(ChunkSize, Rest,
  619. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  620. te_chunked_chopped(Config) ->
  621. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  622. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  623. ConnPid = gun_open(Config),
  624. Ref = gun:post(ConnPid, "/echo/body",
  625. [{<<"content-type">>, <<"text/plain">>}]),
  626. _ = [begin
  627. ok = gun:dbg_send_raw(ConnPid, << C >>),
  628. receive after 10 -> ok end
  629. end || << C >> <= Body2],
  630. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  631. {ok, Body} = gun:await_body(ConnPid, Ref),
  632. ok.
  633. te_chunked_delayed(Config) ->
  634. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  635. Chunks = do_body_to_chunks(50, Body, []),
  636. ConnPid = gun_open(Config),
  637. Ref = gun:post(ConnPid, "/echo/body",
  638. [{<<"content-type">>, <<"text/plain">>}]),
  639. _ = [begin
  640. ok = gun:dbg_send_raw(ConnPid, Chunk),
  641. receive after 10 -> ok end
  642. end || Chunk <- Chunks],
  643. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  644. {ok, Body} = gun:await_body(ConnPid, Ref),
  645. ok.
  646. te_chunked_split_body(Config) ->
  647. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  648. Chunks = do_body_to_chunks(50, Body, []),
  649. ConnPid = gun_open(Config),
  650. Ref = gun:post(ConnPid, "/echo/body",
  651. [{<<"content-type">>, <<"text/plain">>}]),
  652. _ = [begin
  653. case Chunk of
  654. <<"0\r\n\r\n">> ->
  655. ok = gun:dbg_send_raw(ConnPid, Chunk);
  656. _ ->
  657. [Size, ChunkBody, <<>>] =
  658. binary:split(Chunk, [<<"\r\n">>], [global]),
  659. PartASize = random:uniform(byte_size(ChunkBody)),
  660. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  661. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  662. receive after 10 -> ok end,
  663. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  664. end
  665. end || Chunk <- Chunks],
  666. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  667. {ok, Body} = gun:await_body(ConnPid, Ref),
  668. ok.
  669. te_chunked_split_crlf(Config) ->
  670. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  671. Chunks = do_body_to_chunks(50, Body, []),
  672. ConnPid = gun_open(Config),
  673. Ref = gun:post(ConnPid, "/echo/body",
  674. [{<<"content-type">>, <<"text/plain">>}]),
  675. _ = [begin
  676. %% Split in the newline just before the end of the chunk.
  677. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  678. << Chunk2:Len/binary, End/binary >> = Chunk,
  679. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  680. receive after 10 -> ok end,
  681. ok = gun:dbg_send_raw(ConnPid, End)
  682. end || Chunk <- Chunks],
  683. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  684. {ok, Body} = gun:await_body(ConnPid, Ref),
  685. ok.
  686. te_identity(Config) ->
  687. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  688. ConnPid = gun_open(Config),
  689. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  690. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  691. {ok, Body} = gun:await_body(ConnPid, Ref),
  692. ok.