old_http_SUITE.erl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. %% Tests.
  94. rest_bad_content_type(Config) ->
  95. ConnPid = gun_open(Config),
  96. Ref = gun:patch(ConnPid, "/bad_content_type",
  97. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  98. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  99. ok.
  100. rest_nodelete(Config) ->
  101. ConnPid = gun_open(Config),
  102. Ref = gun:delete(ConnPid, "/nodelete"),
  103. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  104. ok.
  105. rest_patch(Config) ->
  106. Tests = [
  107. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  108. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  109. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  110. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  111. ],
  112. ConnPid = gun_open(Config),
  113. _ = [begin
  114. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  115. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  116. end || {Status, Headers, Body} <- Tests],
  117. ok.
  118. rest_post_charset(Config) ->
  119. ConnPid = gun_open(Config),
  120. Ref = gun:post(ConnPid, "/post_charset",
  121. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  122. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  123. ok.
  124. rest_postonly(Config) ->
  125. ConnPid = gun_open(Config),
  126. Ref = gun:post(ConnPid, "/postonly",
  127. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  128. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  129. ok.
  130. rest_resource_get_etag(Config, Type) ->
  131. rest_resource_get_etag(Config, Type, []).
  132. rest_resource_get_etag(Config, Type, Headers) ->
  133. ConnPid = gun_open(Config),
  134. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  135. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  136. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  137. false -> {Status, false};
  138. {<<"etag">>, ETag} -> {Status, ETag}
  139. end.
  140. rest_resource_etags(Config) ->
  141. Tests = [
  142. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  143. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  144. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  145. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  146. {500, false, "binary-strong-unquoted"},
  147. {500, false, "binary-weak-unquoted"}
  148. ],
  149. _ = [{Status, ETag, Type} = begin
  150. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  151. {Ret, RespETag, Type}
  152. end || {Status, ETag, Type} <- Tests].
  153. rest_resource_etags_if_none_match(Config) ->
  154. Tests = [
  155. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  156. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  157. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  158. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  159. ],
  160. _ = [{Status, Type} = begin
  161. {Ret, _} = rest_resource_get_etag(Config, Type,
  162. [{<<"if-none-match">>, ETag}]),
  163. {Ret, Type}
  164. end || {Status, ETag, Type} <- Tests].