spdy_SUITE.erl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. %% Copyright (c) 2013-2014, 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. gun_monitor_open(Config) ->
  89. {_, Port} = lists:keyfind(port, 1, Config),
  90. {ok, ConnPid} = gun:open("localhost", Port, [{retry, 0}]),
  91. {ConnPid, monitor(process, ConnPid)}.
  92. quick_get(ConnPid, MRef, Host, Path) ->
  93. StreamRef = gun:get(ConnPid, Path, [{":host", Host}]),
  94. {response, IsFin, Status, _} = gun:await(ConnPid, StreamRef, MRef),
  95. {IsFin, Status}.
  96. %% Tests.
  97. check_status(Config) ->
  98. Tests = [
  99. {200, nofin, "localhost", "/"},
  100. {200, nofin, "localhost", "/chunked"},
  101. {200, nofin, "localhost", "/static/style.css"},
  102. {400, fin, "bad-host", "/"},
  103. {400, fin, "localhost", "bad-path"},
  104. {404, fin, "localhost", "/this/path/does/not/exist"}
  105. ],
  106. {ConnPid, MRef} = gun_monitor_open(Config),
  107. _ = [{Status, Fin, Host, Path} = begin
  108. {IsFin, Ret} = quick_get(ConnPid, MRef, Host, Path),
  109. {Ret, IsFin, Host, Path}
  110. end || {Status, Fin, Host, Path} <- Tests],
  111. gun:close(ConnPid).
  112. echo_body(Config) ->
  113. {ConnPid, MRef} = gun_monitor_open(Config),
  114. Body = << 0:800000 >>,
  115. StreamRef = gun:post(ConnPid, "/echo/body", [
  116. {<<"content-type">>, "application/octet-stream"}
  117. ], Body),
  118. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
  119. {ok, Body} = gun:await_body(ConnPid, StreamRef, MRef),
  120. gun:close(ConnPid).
  121. echo_body_multi(Config) ->
  122. {ConnPid, MRef} = gun_monitor_open(Config),
  123. BodyChunk = << 0:80000 >>,
  124. StreamRef = gun:post(ConnPid, "/echo/body", [
  125. %% @todo I'm still unhappy with this. It shouldn't be required...
  126. {<<"content-length">>, integer_to_list(byte_size(BodyChunk) * 10)},
  127. {<<"content-type">>, "application/octet-stream"}
  128. ]),
  129. _ = [gun:data(ConnPid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)],
  130. gun:data(ConnPid, StreamRef, fin, BodyChunk),
  131. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
  132. {ok, << 0:800000 >>} = gun:await_body(ConnPid, StreamRef, MRef),
  133. gun:close(ConnPid).