spdy_SUITE.erl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. application:start(crypto),
  38. application:start(cowlib),
  39. application:start(ranch),
  40. application:start(cowboy),
  41. application:start(asn1),
  42. application:start(public_key),
  43. application:start(ssl),
  44. application:start(gun),
  45. Dir = ?config(priv_dir, Config) ++ "/static",
  46. ct_helper:create_static_dir(Dir),
  47. [{static_dir, Dir}|Config].
  48. end_per_suite(Config) ->
  49. Dir = ?config(static_dir, Config),
  50. ct_helper:delete_static_dir(Dir),
  51. application:stop(gun),
  52. application:stop(ssl),
  53. application:stop(public_key),
  54. application:stop(asn1),
  55. application:stop(cowboy),
  56. application:stop(ranch),
  57. application:stop(cowlib),
  58. application:stop(crypto),
  59. ok.
  60. init_per_group(Name, Config) ->
  61. {_, Cert, Key} = ct_helper:make_certs(),
  62. Opts = [{cert, Cert}, {key, Key}],
  63. {ok, _} = cowboy:start_spdy(Name, 100, Opts ++ [{port, 0}], [
  64. {env, [{dispatch, init_dispatch(Config)}]}
  65. ]),
  66. Port = ranch:get_port(Name),
  67. [{port, Port}|Config].
  68. end_per_group(Name, _) ->
  69. cowboy:stop_listener(Name),
  70. ok.
  71. %% Dispatch configuration.
  72. init_dispatch(Config) ->
  73. cowboy_router:compile([
  74. {"localhost", [
  75. {"/static/[...]", cowboy_static,
  76. {dir, ?config(static_dir, Config)}},
  77. {"/echo/body", http_echo_body, []},
  78. {"/chunked", http_chunked, []},
  79. {"/", http_handler, []}
  80. ]}
  81. ]).
  82. %% Convenience functions.
  83. quick_get(Pid, Host, Path) ->
  84. MRef = monitor(process, Pid),
  85. StreamRef = gun:get(Pid, Path, [{":host", Host}]),
  86. receive
  87. {'DOWN', MRef, _, _, Reason} ->
  88. error(Reason);
  89. {gun_response, Pid, StreamRef, IsFin,
  90. << Status:3/binary, _/bits >>, Headers} ->
  91. {IsFin, binary_to_integer(Status), Headers}
  92. after 1000 ->
  93. error(timeout)
  94. end.
  95. %% Tests.
  96. check_status(Config) ->
  97. {_, Port} = lists:keyfind(port, 1, Config),
  98. {ok, Pid} = gun:open("localhost", Port),
  99. Tests = [
  100. {200, nofin, "localhost", "/"},
  101. {200, nofin, "localhost", "/chunked"},
  102. {200, nofin, "localhost", "/static/style.css"},
  103. {400, fin, "bad-host", "/"},
  104. {400, fin, "localhost", "bad-path"},
  105. {404, fin, "localhost", "/this/path/does/not/exist"}
  106. ],
  107. _ = [{Status, Fin, Host, Path} = begin
  108. {IsFin, Ret, _} = quick_get(Pid, Host, Path),
  109. {Ret, IsFin, Host, Path}
  110. end || {Status, Fin, Host, Path} <- Tests],
  111. gun:close(Pid).
  112. echo_body(Config) ->
  113. {_, Port} = lists:keyfind(port, 1, Config),
  114. {ok, Pid} = gun:open("localhost", Port),
  115. MRef = monitor(process, Pid),
  116. Body = << 0:800000 >>,
  117. StreamRef = gun:post(Pid, "/echo/body", [
  118. {<<"content-length">>, integer_to_list(byte_size(Body))},
  119. {<<"content-type">>, "application/octet-stream"}
  120. ], Body),
  121. receive
  122. {'DOWN', MRef, _, _, Reason} ->
  123. error(Reason);
  124. {gun_response, Pid, StreamRef, nofin, << "200", _/bits >>, _} ->
  125. ok
  126. after 1000 ->
  127. error(response_timeout)
  128. end,
  129. receive
  130. {'DOWN', MRef, _, _, Reason2} ->
  131. error({gun_data, Reason2});
  132. {gun_data, Pid, StreamRef, fin, Body} ->
  133. ok
  134. after 1000 ->
  135. error(data_timeout)
  136. end,
  137. gun:close(Pid).
  138. echo_body_multi(Config) ->
  139. {_, Port} = lists:keyfind(port, 1, Config),
  140. {ok, Pid} = gun:open("localhost", Port),
  141. MRef = monitor(process, Pid),
  142. BodyChunk = << 0:80000 >>,
  143. StreamRef = gun:post(Pid, "/echo/body", [
  144. {<<"content-length">>, integer_to_list(byte_size(BodyChunk) * 10)},
  145. {<<"content-type">>, "application/octet-stream"}
  146. ]),
  147. _ = [gun:data(Pid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)],
  148. gun:data(Pid, StreamRef, fin, BodyChunk),
  149. receive
  150. {'DOWN', MRef, _, _, Reason} ->
  151. error(Reason);
  152. {gun_response, Pid, StreamRef, nofin, << "200", _/bits >>, _} ->
  153. ok
  154. after 1000 ->
  155. error(response_timeout)
  156. end,
  157. receive
  158. {'DOWN', MRef, _, _, Reason2} ->
  159. error({gun_data, Reason2});
  160. {gun_data, Pid, StreamRef, fin, << 0:800000 >>} ->
  161. ok
  162. after 1000 ->
  163. error(data_timeout)
  164. end,
  165. gun:close(Pid).