h2spec_SUITE.erl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. %% Copyright (c) 2017, 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(h2spec_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. %% ct.
  20. all() ->
  21. [h2spec].
  22. init_per_suite(Config) ->
  23. case os:getenv("H2SPEC") of
  24. false ->
  25. skip;
  26. _ ->
  27. cowboy_test:init_http(h2spec, #{
  28. env => #{dispatch => init_dispatch()},
  29. max_concurrent_streams => 100
  30. }, Config)
  31. end.
  32. end_per_suite(_Config) ->
  33. cowboy:stop_listener(h2spec).
  34. %% Dispatch configuration.
  35. init_dispatch() ->
  36. cowboy_router:compile([
  37. {'_', [
  38. {"/", delay_hello_h, 50}
  39. ]}
  40. ]).
  41. %% Tests.
  42. h2spec(Config) ->
  43. doc("h2spec test suite for the HTTP/2 protocol."),
  44. Self = self(),
  45. spawn_link(fun() -> start_port(Config, Self) end),
  46. receive
  47. {h2spec_exit, 0, Log} ->
  48. ct:log("~ts", [Log]),
  49. ok;
  50. {h2spec_exit, Status, Log} ->
  51. ct:log("~ts", [Log]),
  52. error({exit_status, Status})
  53. end.
  54. start_port(Config, Pid) ->
  55. H2spec = os:getenv("H2SPEC"),
  56. ListenPort = config(port, Config),
  57. Port = open_port(
  58. {spawn, H2spec ++ " -S -p "
  59. ++ integer_to_list(ListenPort)},
  60. [{line, 10000}, {cd, config(priv_dir, Config)}, binary, exit_status]),
  61. receive_infinity(Port, Pid, []).
  62. receive_infinity(Port, Pid, Acc) ->
  63. receive
  64. {Port, {data, {eol, Line}}} ->
  65. io:format(user, "~s~n", [Line]),
  66. receive_infinity(Port, Pid, [Line|Acc]);
  67. {Port, {exit_status, Status}} ->
  68. Pid ! {h2spec_exit, Status, [[L, $\n] || L <- lists:reverse(Acc)]}
  69. end.