h2spec_SUITE.erl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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, "H2SPEC environment variable undefined."};
  26. H2spec ->
  27. case filelib:is_file(H2spec) of
  28. false ->
  29. {skip, "H2SPEC executable not found."};
  30. true ->
  31. cowboy_test:init_http(h2spec, #{
  32. env => #{dispatch => init_dispatch()},
  33. max_concurrent_streams => 100,
  34. %% This test suite expects an HTTP/2-only connection.
  35. protocols => [http2],
  36. %% Disable the DATA threshold for this test suite.
  37. stream_window_data_threshold => 0
  38. }, Config)
  39. end
  40. end.
  41. end_per_suite(_Config) ->
  42. cowboy:stop_listener(h2spec).
  43. %% Dispatch configuration.
  44. init_dispatch() ->
  45. cowboy_router:compile([
  46. {'_', [
  47. {"/", delay_hello_h, 50}
  48. ]}
  49. ]).
  50. %% Tests.
  51. h2spec(Config) ->
  52. doc("h2spec test suite for the HTTP/2 protocol."),
  53. Self = self(),
  54. spawn_link(fun() -> start_port(Config, Self) end),
  55. receive
  56. {h2spec_exit, 0, Log} ->
  57. ct:log("~ts", [Log]),
  58. ok;
  59. {h2spec_exit, Status, Log} ->
  60. ct:log("~ts", [Log]),
  61. error({exit_status, Status})
  62. end.
  63. start_port(Config, Pid) ->
  64. H2spec = os:getenv("H2SPEC"),
  65. ListenPort = config(port, Config),
  66. Port = open_port(
  67. {spawn, H2spec ++ " -S -p "
  68. ++ integer_to_list(ListenPort)},
  69. [{line, 10000}, {cd, config(priv_dir, Config)}, binary, exit_status]),
  70. receive_infinity(Port, Pid, []).
  71. receive_infinity(Port, Pid, Acc) ->
  72. receive
  73. {Port, {data, {eol, Line}}} ->
  74. io:format(user, "~s~n", [Line]),
  75. receive_infinity(Port, Pid, [Line|Acc]);
  76. {Port, {exit_status, Status}} ->
  77. Pid ! {h2spec_exit, Status, [[L, $\n] || L <- lists:reverse(Acc)]}
  78. end.