old_http_SUITE.erl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_get(Path, Config) ->
  95. ConnPid = gun_open(Config),
  96. Ref = gun:get(ConnPid, Path),
  97. {response, _, Status, _} = gun:await(ConnPid, Ref),
  98. gun:close(ConnPid),
  99. Status.
  100. %% Tests.
  101. check_status(Config) ->
  102. Tests = [
  103. {200, "/simple"},
  104. {500, "/handler_errors?case=init_before_reply"}
  105. ],
  106. _ = [{Status, URL} = begin
  107. Ret = do_get(URL, Config),
  108. {Ret, URL}
  109. end || {Status, URL} <- Tests].
  110. error_init_after_reply(Config) ->
  111. ConnPid = gun_open(Config),
  112. Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
  113. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  114. ok.
  115. rest_param_all(Config) ->
  116. ConnPid = gun_open(Config),
  117. %% Accept without param.
  118. Ref1 = gun:get(ConnPid, "/param_all",
  119. [{<<"accept">>, <<"text/plain">>}]),
  120. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  121. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  122. %% Accept with param.
  123. Ref2 = gun:get(ConnPid, "/param_all",
  124. [{<<"accept">>, <<"text/plain;level=1">>}]),
  125. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  126. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  127. %% Accept with param and quality.
  128. Ref3 = gun:get(ConnPid, "/param_all",
  129. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  130. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  131. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  132. Ref4 = gun:get(ConnPid, "/param_all",
  133. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  134. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  135. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  136. %% Without Accept.
  137. Ref5 = gun:get(ConnPid, "/param_all"),
  138. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  139. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  140. %% Content-Type without param.
  141. Ref6 = gun:put(ConnPid, "/param_all",
  142. [{<<"content-type">>, <<"text/plain">>}]),
  143. gun:data(ConnPid, Ref6, fin, "Hello world!"),
  144. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  145. %% Content-Type with param.
  146. Ref7 = gun:put(ConnPid, "/param_all",
  147. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  148. gun:data(ConnPid, Ref7, fin, "Hello world!"),
  149. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  150. ok.
  151. rest_bad_accept(Config) ->
  152. ConnPid = gun_open(Config),
  153. Ref = gun:get(ConnPid, "/bad_accept",
  154. [{<<"accept">>, <<"1">>}]),
  155. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  156. ok.
  157. rest_bad_content_type(Config) ->
  158. ConnPid = gun_open(Config),
  159. Ref = gun:patch(ConnPid, "/bad_content_type",
  160. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  161. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  162. ok.
  163. rest_expires(Config) ->
  164. ConnPid = gun_open(Config),
  165. Ref = gun:get(ConnPid, "/rest_expires"),
  166. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  167. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  168. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  169. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  170. ok.
  171. rest_expires_binary(Config) ->
  172. ConnPid = gun_open(Config),
  173. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  174. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  175. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  176. ok.
  177. rest_last_modified_undefined(Config) ->
  178. ConnPid = gun_open(Config),
  179. Ref = gun:get(ConnPid, "/simple",
  180. [{<<"if-modified-since">>, <<"Fri, 21 Sep 2012 22:36:14 GMT">>}]),
  181. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  182. ok.
  183. rest_keepalive(Config) ->
  184. ConnPid = gun_open(Config),
  185. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  186. _ = [begin
  187. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  188. false = lists:keymember(<<"connection">>, 1, Headers)
  189. end || Ref <- Refs],
  190. ok.
  191. rest_keepalive_post(Config) ->
  192. ConnPid = gun_open(Config),
  193. Refs = [begin
  194. Ref1 = gun:post(ConnPid, "/forbidden_post", [
  195. {<<"content-type">>, <<"text/plain">>},
  196. {<<"content-length">>, <<"12">>}
  197. ]),
  198. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  199. Ref2 = gun:post(ConnPid, "/simple_post", [
  200. {<<"content-type">>, <<"text/plain">>},
  201. {<<"content-length">>, <<"12">>}
  202. ]),
  203. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  204. {Ref1, Ref2}
  205. end || _ <- lists:seq(1, 5)],
  206. _ = [begin
  207. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  208. false = lists:keymember(<<"connection">>, 1, Headers1),
  209. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  210. false = lists:keymember(<<"connection">>, 1, Headers2)
  211. end || {Ref1, Ref2} <- Refs],
  212. ok.
  213. rest_missing_get_callbacks(Config) ->
  214. ConnPid = gun_open(Config),
  215. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  216. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  217. ok.
  218. rest_missing_put_callbacks(Config) ->
  219. ConnPid = gun_open(Config),
  220. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  221. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  222. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  223. ok.
  224. rest_nodelete(Config) ->
  225. ConnPid = gun_open(Config),
  226. Ref = gun:delete(ConnPid, "/nodelete"),
  227. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  228. ok.
  229. rest_options_default(Config) ->
  230. ConnPid = gun_open(Config),
  231. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  232. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  233. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  234. ok.
  235. rest_patch(Config) ->
  236. Tests = [
  237. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  238. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  239. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  240. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  241. ],
  242. ConnPid = gun_open(Config),
  243. _ = [begin
  244. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  245. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  246. end || {Status, Headers, Body} <- Tests],
  247. ok.
  248. rest_post_charset(Config) ->
  249. ConnPid = gun_open(Config),
  250. Ref = gun:post(ConnPid, "/post_charset",
  251. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  252. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  253. ok.
  254. rest_postonly(Config) ->
  255. ConnPid = gun_open(Config),
  256. Ref = gun:post(ConnPid, "/postonly",
  257. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  258. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  259. ok.
  260. rest_resource_get_etag(Config, Type) ->
  261. rest_resource_get_etag(Config, Type, []).
  262. rest_resource_get_etag(Config, Type, Headers) ->
  263. ConnPid = gun_open(Config),
  264. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  265. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  266. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  267. false -> {Status, false};
  268. {<<"etag">>, ETag} -> {Status, ETag}
  269. end.
  270. rest_resource_etags(Config) ->
  271. Tests = [
  272. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  273. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  274. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  275. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  276. {500, false, "binary-strong-unquoted"},
  277. {500, false, "binary-weak-unquoted"}
  278. ],
  279. _ = [{Status, ETag, Type} = begin
  280. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  281. {Ret, RespETag, Type}
  282. end || {Status, ETag, Type} <- Tests].
  283. rest_resource_etags_if_none_match(Config) ->
  284. Tests = [
  285. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  286. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  287. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  288. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  289. ],
  290. _ = [{Status, Type} = begin
  291. {Ret, _} = rest_resource_get_etag(Config, Type,
  292. [{<<"if-none-match">>, ETag}]),
  293. {Ret, Type}
  294. end || {Status, ETag, Type} <- Tests].