h2spec_SUITE.erl 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. %% Disable the DATA threshold for this test suite.
  35. stream_window_data_threshold => 0
  36. }, Config)
  37. end
  38. end.
  39. end_per_suite(_Config) ->
  40. cowboy:stop_listener(h2spec).
  41. %% Dispatch configuration.
  42. init_dispatch() ->
  43. cowboy_router:compile([
  44. {'_', [
  45. {"/", delay_hello_h, 50}
  46. ]}
  47. ]).
  48. %% Tests.
  49. h2spec(Config) ->
  50. doc("h2spec test suite for the HTTP/2 protocol."),
  51. Self = self(),
  52. spawn_link(fun() -> start_port(Config, Self) end),
  53. receive
  54. {h2spec_exit, 0, Log} ->
  55. ct:log("~ts", [Log]),
  56. ok;
  57. {h2spec_exit, Status, Log} ->
  58. ct:log("~ts", [Log]),
  59. error({exit_status, Status})
  60. end.
  61. start_port(Config, Pid) ->
  62. H2spec = os:getenv("H2SPEC"),
  63. ListenPort = config(port, Config),
  64. Port = open_port(
  65. {spawn, H2spec ++ " -S -p "
  66. ++ integer_to_list(ListenPort)},
  67. [{line, 10000}, {cd, config(priv_dir, Config)}, binary, exit_status]),
  68. receive_infinity(Port, Pid, []).
  69. receive_infinity(Port, Pid, Acc) ->
  70. receive
  71. {Port, {data, {eol, Line}}} ->
  72. io:format(user, "~s~n", [Line]),
  73. receive_infinity(Port, Pid, [Line|Acc]);
  74. {Port, {exit_status, Status}} ->
  75. Pid ! {h2spec_exit, Status, [[L, $\n] || L <- lists:reverse(Acc)]}
  76. end.