old_http_SUITE.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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(old_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. ].
  34. groups() ->
  35. Tests = ct_helper:all(?MODULE),
  36. [
  37. {http, [], Tests}, %% @todo parallel
  38. {https, [parallel], Tests},
  39. {http_compress, [parallel], Tests},
  40. {https_compress, [parallel], Tests}
  41. ].
  42. init_per_group(Name = http, Config) ->
  43. cowboy_test:init_http(Name, #{env => #{dispatch => init_dispatch(Config)}}, Config);
  44. init_per_group(Name = https, Config) ->
  45. cowboy_test:init_https(Name, #{env => #{dispatch => init_dispatch(Config)}}, Config);
  46. init_per_group(Name = http_compress, Config) ->
  47. cowboy_test:init_http(Name, #{
  48. env => #{dispatch => init_dispatch(Config)},
  49. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  50. }, Config);
  51. init_per_group(Name = https_compress, Config) ->
  52. cowboy_test:init_https(Name, #{
  53. env => #{dispatch => init_dispatch(Config)},
  54. stream_handlers => [cowboy_compress_h, cowboy_stream_h]
  55. }, Config).
  56. end_per_group(Name, _) ->
  57. ok = cowboy:stop_listener(Name).
  58. %% Dispatch configuration.
  59. init_dispatch(_) ->
  60. cowboy_router:compile([
  61. {"localhost", [
  62. {"/chunked_response", http_chunked, []},
  63. {"/headers/dupe", http_handler,
  64. [{headers, #{<<"connection">> => <<"close">>}}]},
  65. {"/set_resp/header", http_set_resp,
  66. [{headers, #{<<"vary">> => <<"Accept">>}}]},
  67. {"/set_resp/overwrite", http_set_resp,
  68. [{headers, #{<<"server">> => <<"DesireDrive/1.0">>}}]},
  69. {"/set_resp/body", http_set_resp,
  70. [{body, <<"A flameless dance does not equal a cycle">>}]},
  71. {"/handler_errors", http_errors, []},
  72. {"/echo/body", http_echo_body, []},
  73. {"/param_all", rest_param_all, []},
  74. {"/bad_accept", rest_simple_resource, []},
  75. {"/bad_content_type", rest_patch_resource, []},
  76. {"/simple", rest_simple_resource, []},
  77. {"/forbidden_post", rest_forbidden_resource, [true]},
  78. {"/simple_post", rest_forbidden_resource, [false]},
  79. {"/missing_get_callbacks", rest_missing_callbacks, []},
  80. {"/missing_put_callbacks", rest_missing_callbacks, []},
  81. {"/nodelete", rest_nodelete_resource, []},
  82. {"/post_charset", rest_post_charset_resource, []},
  83. {"/postonly", rest_postonly_resource, []},
  84. {"/patch", rest_patch_resource, []},
  85. {"/resetags", rest_resource_etags, []},
  86. {"/rest_expires", rest_expires, []},
  87. {"/rest_expires_binary", rest_expires_binary, []},
  88. {"/rest_empty_resource", rest_empty_resource, []},
  89. {"/loop_stream_recv", http_loop_stream_recv, []},
  90. {"/", http_handler, []}
  91. ]}
  92. ]).
  93. %% Convenience functions.
  94. do_raw(Data, Config) ->
  95. Client = raw_open(Config),
  96. ok = raw_send(Client, Data),
  97. case catch raw_recv_head(Client) of
  98. {'EXIT', _} -> closed;
  99. Resp -> element(2, cow_http:parse_status_line(Resp))
  100. end.
  101. do_get(Path, Config) ->
  102. ConnPid = gun_open(Config),
  103. Ref = gun:get(ConnPid, Path),
  104. {response, _, Status, _} = gun:await(ConnPid, Ref),
  105. gun:close(ConnPid),
  106. Status.
  107. %% Tests.
  108. check_status(Config) ->
  109. Tests = [
  110. {200, "/simple"},
  111. {500, "/handler_errors?case=init_before_reply"}
  112. ],
  113. _ = [{Status, URL} = begin
  114. Ret = do_get(URL, Config),
  115. {Ret, URL}
  116. end || {Status, URL} <- Tests].
  117. error_init_after_reply(Config) ->
  118. ConnPid = gun_open(Config),
  119. Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
  120. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  121. ok.
  122. keepalive_nl(Config) ->
  123. ConnPid = gun_open(Config),
  124. Refs = [begin
  125. Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  126. dbg_send_raw(ConnPid, <<"\r\n">>),
  127. Ref
  128. end || _ <- lists:seq(1, 10)],
  129. _ = [begin
  130. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  131. false = lists:keymember(<<"connection">>, 1, Headers)
  132. end || Ref <- Refs],
  133. ok.
  134. keepalive_stream_loop(Config) ->
  135. ConnPid = gun_open(Config),
  136. Refs = [begin
  137. Ref = gun:post(ConnPid, "/loop_stream_recv",
  138. [{<<"content-type">>, <<"application/octet-stream">>}]),
  139. _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
  140. || ID <- lists:seq(1, 250)],
  141. gun:data(ConnPid, Ref, fin, <<>>),
  142. Ref
  143. end || _ <- lists:seq(1, 10)],
  144. _ = [begin
  145. {response, fin, 200, _} = gun:await(ConnPid, Ref)
  146. end || Ref <- Refs],
  147. ok.
  148. rest_param_all(Config) ->
  149. ConnPid = gun_open(Config),
  150. %% Accept without param.
  151. Ref1 = gun:get(ConnPid, "/param_all",
  152. [{<<"accept">>, <<"text/plain">>}]),
  153. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  154. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  155. %% Accept with param.
  156. Ref2 = gun:get(ConnPid, "/param_all",
  157. [{<<"accept">>, <<"text/plain;level=1">>}]),
  158. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  159. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  160. %% Accept with param and quality.
  161. Ref3 = gun:get(ConnPid, "/param_all",
  162. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  163. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  164. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  165. Ref4 = gun:get(ConnPid, "/param_all",
  166. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  167. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  168. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  169. %% Without Accept.
  170. Ref5 = gun:get(ConnPid, "/param_all"),
  171. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  172. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  173. %% Content-Type without param.
  174. Ref6 = gun:put(ConnPid, "/param_all",
  175. [{<<"content-type">>, <<"text/plain">>}]),
  176. gun:data(ConnPid, Ref6, fin, "Hello world!"),
  177. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  178. %% Content-Type with param.
  179. Ref7 = gun:put(ConnPid, "/param_all",
  180. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  181. gun:data(ConnPid, Ref7, fin, "Hello world!"),
  182. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  183. ok.
  184. rest_bad_accept(Config) ->
  185. ConnPid = gun_open(Config),
  186. Ref = gun:get(ConnPid, "/bad_accept",
  187. [{<<"accept">>, <<"1">>}]),
  188. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  189. ok.
  190. rest_bad_content_type(Config) ->
  191. ConnPid = gun_open(Config),
  192. Ref = gun:patch(ConnPid, "/bad_content_type",
  193. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  194. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  195. ok.
  196. rest_expires(Config) ->
  197. ConnPid = gun_open(Config),
  198. Ref = gun:get(ConnPid, "/rest_expires"),
  199. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  200. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  201. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  202. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  203. ok.
  204. rest_expires_binary(Config) ->
  205. ConnPid = gun_open(Config),
  206. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  207. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  208. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  209. ok.
  210. rest_last_modified_undefined(Config) ->
  211. ConnPid = gun_open(Config),
  212. Ref = gun:get(ConnPid, "/simple",
  213. [{<<"if-modified-since">>, <<"Fri, 21 Sep 2012 22:36:14 GMT">>}]),
  214. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  215. ok.
  216. rest_keepalive(Config) ->
  217. ConnPid = gun_open(Config),
  218. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  219. _ = [begin
  220. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  221. false = lists:keymember(<<"connection">>, 1, Headers)
  222. end || Ref <- Refs],
  223. ok.
  224. rest_keepalive_post(Config) ->
  225. ConnPid = gun_open(Config),
  226. Refs = [begin
  227. Ref1 = gun:post(ConnPid, "/forbidden_post", [
  228. {<<"content-type">>, <<"text/plain">>},
  229. {<<"content-length">>, <<"12">>}
  230. ]),
  231. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  232. Ref2 = gun:post(ConnPid, "/simple_post", [
  233. {<<"content-type">>, <<"text/plain">>},
  234. {<<"content-length">>, <<"12">>}
  235. ]),
  236. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  237. {Ref1, Ref2}
  238. end || _ <- lists:seq(1, 5)],
  239. _ = [begin
  240. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  241. false = lists:keymember(<<"connection">>, 1, Headers1),
  242. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  243. false = lists:keymember(<<"connection">>, 1, Headers2)
  244. end || {Ref1, Ref2} <- Refs],
  245. ok.
  246. rest_missing_get_callbacks(Config) ->
  247. ConnPid = gun_open(Config),
  248. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  249. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  250. ok.
  251. rest_missing_put_callbacks(Config) ->
  252. ConnPid = gun_open(Config),
  253. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  254. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  255. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  256. ok.
  257. rest_nodelete(Config) ->
  258. ConnPid = gun_open(Config),
  259. Ref = gun:delete(ConnPid, "/nodelete"),
  260. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  261. ok.
  262. rest_options_default(Config) ->
  263. ConnPid = gun_open(Config),
  264. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  265. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  266. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  267. ok.
  268. rest_patch(Config) ->
  269. Tests = [
  270. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  271. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  272. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  273. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  274. ],
  275. ConnPid = gun_open(Config),
  276. _ = [begin
  277. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  278. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  279. end || {Status, Headers, Body} <- Tests],
  280. ok.
  281. rest_post_charset(Config) ->
  282. ConnPid = gun_open(Config),
  283. Ref = gun:post(ConnPid, "/post_charset",
  284. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  285. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  286. ok.
  287. rest_postonly(Config) ->
  288. ConnPid = gun_open(Config),
  289. Ref = gun:post(ConnPid, "/postonly",
  290. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  291. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  292. ok.
  293. rest_resource_get_etag(Config, Type) ->
  294. rest_resource_get_etag(Config, Type, []).
  295. rest_resource_get_etag(Config, Type, Headers) ->
  296. ConnPid = gun_open(Config),
  297. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  298. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  299. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  300. false -> {Status, false};
  301. {<<"etag">>, ETag} -> {Status, ETag}
  302. end.
  303. rest_resource_etags(Config) ->
  304. Tests = [
  305. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  306. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  307. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  308. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  309. {500, false, "binary-strong-unquoted"},
  310. {500, false, "binary-weak-unquoted"}
  311. ],
  312. _ = [{Status, ETag, Type} = begin
  313. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  314. {Ret, RespETag, Type}
  315. end || {Status, ETag, Type} <- Tests].
  316. rest_resource_etags_if_none_match(Config) ->
  317. Tests = [
  318. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  319. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  320. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  321. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  322. ],
  323. _ = [{Status, Type} = begin
  324. {Ret, _} = rest_resource_get_etag(Config, Type,
  325. [{<<"if-none-match">>, ETag}]),
  326. {Ret, Type}
  327. end || {Status, ETag, Type} <- Tests].
  328. dbg_send_raw(ConnPid, Data) ->
  329. #{
  330. socket := Socket,
  331. transport := Transport
  332. } = gun:info(ConnPid),
  333. _ = case Transport of
  334. tcp -> gen_tcp:send(Socket, Data);
  335. tls -> ssl:send(Socket, Data)
  336. end,
  337. ok.