spdy_SUITE.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. -compile(export_all).
  16. -import(cowboy_test, [config/2]).
  17. -import(cowboy_test, [gun_monitor_open/1]).
  18. %% ct.
  19. all() ->
  20. [{group, spdy}].
  21. groups() ->
  22. [{spdy, [], cowboy_test:all(?MODULE)}].
  23. init_per_suite(Config) ->
  24. case proplists:get_value(ssl_app, ssl:versions()) of
  25. Version when Version < "5.2.1" ->
  26. {skip, "No NPN support in SSL application."};
  27. _ ->
  28. cowboy_test:start([cowboy, gun]),
  29. Dir = config(priv_dir, Config) ++ "/static",
  30. ct_helper:create_static_dir(Dir),
  31. [{static_dir, Dir}|Config]
  32. end.
  33. end_per_suite(Config) ->
  34. Dir = config(static_dir, Config),
  35. ct_helper:delete_static_dir(Dir),
  36. cowboy_test:stop([cowboy, gun]).
  37. init_per_group(Name, Config) ->
  38. {_, Cert, Key} = ct_helper:make_certs(),
  39. Opts = [{cert, Cert}, {key, Key}],
  40. {ok, _} = cowboy:start_spdy(Name, 100, Opts ++ [{port, 0}], [
  41. {env, [{dispatch, init_dispatch(Config)}]}
  42. ]),
  43. Port = ranch:get_port(Name),
  44. [{port, Port}, {type, ssl}|Config].
  45. end_per_group(Name, _) ->
  46. cowboy:stop_listener(Name),
  47. ok.
  48. %% Dispatch configuration.
  49. init_dispatch(Config) ->
  50. cowboy_router:compile([
  51. {"localhost", [
  52. {"/static/[...]", cowboy_static,
  53. {dir, config(static_dir, Config)}},
  54. {"/echo/body", http_echo_body, []},
  55. {"/chunked", http_chunked, []},
  56. {"/", http_handler, []}
  57. ]}
  58. ]).
  59. %% Convenience functions.
  60. do_get(ConnPid, MRef, Host, Path) ->
  61. StreamRef = gun:get(ConnPid, Path, [{":host", Host}]),
  62. {response, IsFin, Status, _} = gun:await(ConnPid, StreamRef, MRef),
  63. {IsFin, Status}.
  64. %% Tests.
  65. check_status(Config) ->
  66. Tests = [
  67. {200, nofin, "localhost", "/"},
  68. {200, nofin, "localhost", "/chunked"},
  69. {200, nofin, "localhost", "/static/style.css"},
  70. {400, fin, "bad-host", "/"},
  71. {400, fin, "localhost", "bad-path"},
  72. {404, fin, "localhost", "/this/path/does/not/exist"}
  73. ],
  74. {ConnPid, MRef} = gun_monitor_open(Config),
  75. _ = [{Status, Fin, Host, Path} = begin
  76. {IsFin, Ret} = do_get(ConnPid, MRef, Host, Path),
  77. {Ret, IsFin, Host, Path}
  78. end || {Status, Fin, Host, Path} <- Tests],
  79. gun:close(ConnPid).
  80. echo_body(Config) ->
  81. {ConnPid, MRef} = gun_monitor_open(Config),
  82. Body = << 0:800000 >>,
  83. StreamRef = gun:post(ConnPid, "/echo/body", [
  84. {<<"content-type">>, "application/octet-stream"}
  85. ], Body),
  86. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
  87. {ok, Body} = gun:await_body(ConnPid, StreamRef, MRef),
  88. gun:close(ConnPid).
  89. echo_body_multi(Config) ->
  90. {ConnPid, MRef} = gun_monitor_open(Config),
  91. BodyChunk = << 0:80000 >>,
  92. StreamRef = gun:post(ConnPid, "/echo/body", [
  93. %% @todo I'm still unhappy with this. It shouldn't be required...
  94. {<<"content-length">>, integer_to_list(byte_size(BodyChunk) * 10)},
  95. {<<"content-type">>, "application/octet-stream"}
  96. ]),
  97. _ = [gun:data(ConnPid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)],
  98. gun:data(ConnPid, StreamRef, fin, BodyChunk),
  99. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
  100. {ok, << 0:800000 >>} = gun:await_body(ConnPid, StreamRef, MRef),
  101. gun:close(ConnPid).