spdy_SUITE.erl 3.5 KB

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