h2spec_SUITE.erl 2.1 KB

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