h2spec_SUITE.erl 2.3 KB

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