spdy_SUITE.erl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. %% ct.
  26. all() ->
  27. [{group, spdy}].
  28. groups() ->
  29. [{spdy, [], [
  30. check_status
  31. ]}].
  32. init_per_suite(Config) ->
  33. application:start(crypto),
  34. application:start(ranch),
  35. application:start(cowboy),
  36. application:start(asn1),
  37. application:start(public_key),
  38. application:start(ssl),
  39. application:start(gun),
  40. Dir = ?config(priv_dir, Config) ++ "/static",
  41. ct_helper:create_static_dir(Dir),
  42. [{static_dir, Dir}|Config].
  43. end_per_suite(Config) ->
  44. Dir = ?config(static_dir, Config),
  45. ct_helper:delete_static_dir(Dir),
  46. application:stop(gun),
  47. application:stop(ssl),
  48. application:stop(public_key),
  49. application:stop(asn1),
  50. application:stop(cowboy),
  51. application:stop(ranch),
  52. application:stop(crypto),
  53. ok.
  54. init_per_group(Name, Config) ->
  55. {_, Cert, Key} = ct_helper:make_certs(),
  56. Opts = [{cert, Cert}, {key, Key}],
  57. {ok, _} = cowboy:start_spdy(Name, 100, Opts ++ [{port, 0}], [
  58. {env, [{dispatch, init_dispatch(Config)}]}
  59. ]),
  60. Port = ranch:get_port(Name),
  61. [{port, Port}|Config].
  62. end_per_group(Name, _) ->
  63. cowboy:stop_listener(Name),
  64. ok.
  65. %% Dispatch configuration.
  66. init_dispatch(Config) ->
  67. cowboy_router:compile([
  68. {"localhost", [
  69. {"/static/[...]", cowboy_static,
  70. [{directory, ?config(static_dir, Config)},
  71. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  72. {"/chunked", http_chunked, []},
  73. {"/", http_handler, []}
  74. ]}
  75. ]).
  76. %% Convenience functions.
  77. quick_get(Pid, Host, Path) ->
  78. MRef = monitor(process, Pid),
  79. StreamRef = gun:get(Pid, Path, [{":host", Host}]),
  80. receive
  81. {'DOWN', MRef, _, _, Reason} ->
  82. error(Reason);
  83. {gun_response, Pid, StreamRef, IsFin,
  84. << Status:3/binary, _/bits >>, Headers} ->
  85. {IsFin, binary_to_integer(Status), Headers}
  86. after 1000 ->
  87. error(timeout)
  88. end.
  89. %% Tests.
  90. check_status(Config) ->
  91. {_, Port} = lists:keyfind(port, 1, Config),
  92. {ok, Pid} = gun:open("localhost", Port),
  93. Tests = [
  94. {200, nofin, "localhost", "/"},
  95. {200, nofin, "localhost", "/chunked"},
  96. {200, nofin, "localhost", "/static/style.css"},
  97. {400, fin, "bad-host", "/"},
  98. {400, fin, "localhost", "bad-path"},
  99. {404, fin, "localhost", "/this/path/does/not/exist"}
  100. ],
  101. _ = [{Status, Fin, Host, Path} = begin
  102. {IsFin, Ret, _} = quick_get(Pid, Host, Path),
  103. {Ret, IsFin, Host, Path}
  104. end || {Status, Fin, Host, Path} <- Tests],
  105. gun:close(Pid).