h2spec_SUITE.erl 2.1 KB

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