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