spdy_SUITE.erl 3.6 KB

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