spdy_SUITE.erl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. %% Copyright (c) 2013, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(spdy_SUITE).
  15. -include_lib("common_test/include/ct.hrl").
  16. %% ct.
  17. -export([all/0]).
  18. -export([groups/0]).
  19. -export([init_per_suite/1]).
  20. -export([end_per_suite/1]).
  21. -export([init_per_group/2]).
  22. -export([end_per_group/2]).
  23. %% Tests.
  24. -export([check_status/1]).
  25. -export([echo_body/1]).
  26. -export([echo_body_multi/1]).
  27. %% ct.
  28. all() ->
  29. [{group, spdy}].
  30. groups() ->
  31. [{spdy, [], [
  32. check_status,
  33. echo_body,
  34. echo_body_multi
  35. ]}].
  36. init_per_suite(Config) ->
  37. case proplists:get_value(ssl_app, ssl:versions()) of
  38. Version when Version < "5.2.1" ->
  39. {skip, "No NPN support in SSL application."};
  40. _ ->
  41. application:start(crypto),
  42. application:start(cowlib),
  43. application:start(ranch),
  44. application:start(cowboy),
  45. application:start(asn1),
  46. application:start(public_key),
  47. application:start(ssl),
  48. application:start(gun),
  49. Dir = ?config(priv_dir, Config) ++ "/static",
  50. ct_helper:create_static_dir(Dir),
  51. [{static_dir, Dir}|Config]
  52. end.
  53. end_per_suite(Config) ->
  54. Dir = ?config(static_dir, Config),
  55. ct_helper:delete_static_dir(Dir),
  56. application:stop(gun),
  57. application:stop(ssl),
  58. application:stop(public_key),
  59. application:stop(asn1),
  60. application:stop(cowboy),
  61. application:stop(ranch),
  62. application:stop(cowlib),
  63. application:stop(crypto),
  64. ok.
  65. init_per_group(Name, Config) ->
  66. {_, Cert, Key} = ct_helper:make_certs(),
  67. Opts = [{cert, Cert}, {key, Key}],
  68. {ok, _} = cowboy:start_spdy(Name, 100, Opts ++ [{port, 0}], [
  69. {env, [{dispatch, init_dispatch(Config)}]}
  70. ]),
  71. Port = ranch:get_port(Name),
  72. [{port, Port}|Config].
  73. end_per_group(Name, _) ->
  74. cowboy:stop_listener(Name),
  75. ok.
  76. %% Dispatch configuration.
  77. init_dispatch(Config) ->
  78. cowboy_router:compile([
  79. {"localhost", [
  80. {"/static/[...]", cowboy_static,
  81. {dir, ?config(static_dir, Config)}},
  82. {"/echo/body", http_echo_body, []},
  83. {"/chunked", http_chunked, []},
  84. {"/", http_handler, []}
  85. ]}
  86. ]).
  87. %% Convenience functions.
  88. quick_get(Pid, Host, Path) ->
  89. MRef = monitor(process, Pid),
  90. StreamRef = gun:get(Pid, Path, [{":host", Host}]),
  91. receive
  92. {'DOWN', MRef, _, _, Reason} ->
  93. error(Reason);
  94. {gun_response, Pid, StreamRef, IsFin,
  95. << Status:3/binary, _/bits >>, Headers} ->
  96. {IsFin, list_to_integer(binary_to_list(Status)), Headers}
  97. after 1000 ->
  98. error(timeout)
  99. end.
  100. %% Tests.
  101. check_status(Config) ->
  102. {_, Port} = lists:keyfind(port, 1, Config),
  103. {ok, Pid} = gun:open("localhost", Port),
  104. Tests = [
  105. {200, nofin, "localhost", "/"},
  106. {200, nofin, "localhost", "/chunked"},
  107. {200, nofin, "localhost", "/static/style.css"},
  108. {400, fin, "bad-host", "/"},
  109. {400, fin, "localhost", "bad-path"},
  110. {404, fin, "localhost", "/this/path/does/not/exist"}
  111. ],
  112. _ = [{Status, Fin, Host, Path} = begin
  113. {IsFin, Ret, _} = quick_get(Pid, Host, Path),
  114. {Ret, IsFin, Host, Path}
  115. end || {Status, Fin, Host, Path} <- Tests],
  116. gun:close(Pid).
  117. echo_body(Config) ->
  118. {_, Port} = lists:keyfind(port, 1, Config),
  119. {ok, Pid} = gun:open("localhost", Port),
  120. MRef = monitor(process, Pid),
  121. Body = << 0:800000 >>,
  122. StreamRef = gun:post(Pid, "/echo/body", [
  123. {<<"content-length">>, integer_to_list(byte_size(Body))},
  124. {<<"content-type">>, "application/octet-stream"}
  125. ], Body),
  126. receive
  127. {'DOWN', MRef, _, _, Reason} ->
  128. error(Reason);
  129. {gun_response, Pid, StreamRef, nofin, << "200", _/bits >>, _} ->
  130. ok
  131. after 1000 ->
  132. error(response_timeout)
  133. end,
  134. receive
  135. {'DOWN', MRef, _, _, Reason2} ->
  136. error({gun_data, Reason2});
  137. {gun_data, Pid, StreamRef, fin, Body} ->
  138. ok
  139. after 1000 ->
  140. error(data_timeout)
  141. end,
  142. gun:close(Pid).
  143. echo_body_multi(Config) ->
  144. {_, Port} = lists:keyfind(port, 1, Config),
  145. {ok, Pid} = gun:open("localhost", Port),
  146. MRef = monitor(process, Pid),
  147. BodyChunk = << 0:80000 >>,
  148. StreamRef = gun:post(Pid, "/echo/body", [
  149. {<<"content-length">>, integer_to_list(byte_size(BodyChunk) * 10)},
  150. {<<"content-type">>, "application/octet-stream"}
  151. ]),
  152. _ = [gun:data(Pid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)],
  153. gun:data(Pid, StreamRef, fin, BodyChunk),
  154. receive
  155. {'DOWN', MRef, _, _, Reason} ->
  156. error(Reason);
  157. {gun_response, Pid, StreamRef, nofin, << "200", _/bits >>, _} ->
  158. ok
  159. after 1000 ->
  160. error(response_timeout)
  161. end,
  162. receive
  163. {'DOWN', MRef, _, _, Reason2} ->
  164. error({gun_data, Reason2});
  165. {gun_data, Pid, StreamRef, fin, << 0:800000 >>} ->
  166. ok
  167. after 1000 ->
  168. error(data_timeout)
  169. end,
  170. gun:close(Pid).