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