mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Add rfc7230 test suite and update others to recent Gun
This is a large commit. The rfc7230 test suite adds many tests from the RFC7230 document. Gun has been updated quite a bit recently, which broke the Cowboy suites. This is now fixed with this commit. A new hook onfirstrequest has been added. It was very useful during debugging of the test suites. The initial process code has changed a little; more changes are expected with the switch to maps for options.
This commit is contained in:
parent
90ae31998e
commit
228cebaf04
21 changed files with 2137 additions and 440 deletions
|
@ -17,7 +17,7 @@
|
|||
-export([init/2]).
|
||||
|
||||
init(_, _) ->
|
||||
cowboy_test:start([cowboy, gun]),
|
||||
cowboy_test:make_certs(),
|
||||
error_logger:add_report_handler(cowboy_error_h),
|
||||
ct_helper:start([cowboy, gun]),
|
||||
ct_helper:make_certs_in_ets(),
|
||||
error_logger:add_report_handler(ct_helper_error_h),
|
||||
{ok, undefined}.
|
||||
|
|
|
@ -1,145 +0,0 @@
|
|||
%% Copyright (c) 2014, Loïc Hoguin <essen@ninenines.eu>
|
||||
%%
|
||||
%% Permission to use, copy, modify, and/or distribute this software for any
|
||||
%% purpose with or without fee is hereby granted, provided that the above
|
||||
%% copyright notice and this permission notice appear in all copies.
|
||||
%%
|
||||
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
-module(cowboy_error_h).
|
||||
-behaviour(gen_event).
|
||||
|
||||
%% Public interface.
|
||||
-export([ignore/3]).
|
||||
|
||||
%% gen_event.
|
||||
-export([init/1]).
|
||||
-export([handle_event/2]).
|
||||
-export([handle_call/2]).
|
||||
-export([handle_info/2]).
|
||||
-export([terminate/2]).
|
||||
-export([code_change/3]).
|
||||
|
||||
%% Public interface.
|
||||
|
||||
%% Ignore crashes from Pid occuring in M:F/A.
|
||||
ignore(M, F, A) ->
|
||||
gen_event:call(error_logger, ?MODULE, {expect, {self(), M, F, A}}).
|
||||
|
||||
%% gen_event.
|
||||
|
||||
init(_) ->
|
||||
spawn(fun() -> error_logger:tty(false) end),
|
||||
{ok, []}.
|
||||
|
||||
%% Ignore supervisor and progress reports.
|
||||
handle_event({info_report, _, {_, progress, _}}, State) ->
|
||||
{ok, State};
|
||||
handle_event({info_report, _, {_, std_info, _}}, State) ->
|
||||
{ok, State};
|
||||
handle_event({error_report, _, {_, supervisor_report, _}}, State) ->
|
||||
{ok, State};
|
||||
%% Ignore gun retry failures.
|
||||
handle_event({error_report, _, {_, crash_report,
|
||||
[[{initial_call, {gun, init, _}}, _, _,
|
||||
{error_info, {error, gone, _}}|_]|_]}},
|
||||
State) ->
|
||||
{ok, State};
|
||||
%% Ignore emulator reports that are a duplicate of what Ranch gives us.
|
||||
%%
|
||||
%% The emulator always sends strings for errors, which makes it very
|
||||
%% difficult to extract the information we need, hence the regexps.
|
||||
handle_event(Event = {error, GL, {emulator, _, Msg}}, State)
|
||||
when node(GL) =:= node() ->
|
||||
Result = re:run(Msg,
|
||||
"Error in process ([^\s]+).+? with exit value: "
|
||||
".+?{stacktrace,\\[{([^,]+),([^,]+),(.+)",
|
||||
[{capture, all_but_first, list}]),
|
||||
case Result of
|
||||
nomatch ->
|
||||
write_event(Event),
|
||||
{ok, State};
|
||||
{match, [PidStr, MStr, FStr, Rest]} ->
|
||||
A = case Rest of
|
||||
"[]" ++ _ ->
|
||||
0;
|
||||
"[" ++ Rest2 ->
|
||||
count_args(Rest2, 1, 0);
|
||||
_ ->
|
||||
{match, [AStr]} = re:run(Rest, "([^,]+).+",
|
||||
[{capture, all_but_first, list}]),
|
||||
list_to_integer(AStr)
|
||||
end,
|
||||
Crash = {list_to_pid(PidStr), list_to_existing_atom(MStr),
|
||||
list_to_existing_atom(FStr), A},
|
||||
case lists:member(Crash, State) of
|
||||
true ->
|
||||
{ok, lists:delete(Crash, State)};
|
||||
false ->
|
||||
write_event(Event),
|
||||
{ok, State}
|
||||
end
|
||||
end;
|
||||
handle_event(Event = {error, GL,
|
||||
{_, "Ranch listener" ++ _, [_, _, Pid, {[_, _,
|
||||
{stacktrace, [{M, F, A, _}|_]}|_], _}]}},
|
||||
State) when node(GL) =:= node() ->
|
||||
A2 = if is_list(A) -> length(A); true -> A end,
|
||||
Crash = {Pid, M, F, A2},
|
||||
case lists:member(Crash, State) of
|
||||
true ->
|
||||
{ok, lists:delete(Crash, State)};
|
||||
false ->
|
||||
write_event(Event),
|
||||
{ok, State}
|
||||
end;
|
||||
handle_event(Event = {_, GL, _}, State) when node(GL) =:= node() ->
|
||||
write_event(Event),
|
||||
{ok, State};
|
||||
handle_event(_, State) ->
|
||||
{ok, State}.
|
||||
|
||||
handle_call({expect, Crash}, State) ->
|
||||
{ok, ok, [Crash, Crash|State]};
|
||||
handle_call(_, State) ->
|
||||
{ok, {error, bad_query}, State}.
|
||||
|
||||
handle_info(_, State) ->
|
||||
{ok, State}.
|
||||
|
||||
terminate(_, _) ->
|
||||
spawn(fun() -> error_logger:tty(true) end),
|
||||
ok.
|
||||
|
||||
code_change(_, State, _) ->
|
||||
{ok, State}.
|
||||
|
||||
%% Internal.
|
||||
|
||||
write_event(Event) ->
|
||||
error_logger_tty_h:write_event(
|
||||
{erlang:universaltime(), Event},
|
||||
io).
|
||||
|
||||
count_args("]" ++ _, N, 0) ->
|
||||
N;
|
||||
count_args("]" ++ Tail, N, Levels) ->
|
||||
count_args(Tail, N, Levels - 1);
|
||||
count_args("[" ++ Tail, N, Levels) ->
|
||||
count_args(Tail, N, Levels + 1);
|
||||
count_args("}" ++ Tail, N, Levels) ->
|
||||
count_args(Tail, N, Levels - 1);
|
||||
count_args("{" ++ Tail, N, Levels) ->
|
||||
count_args(Tail, N, Levels + 1);
|
||||
count_args("," ++ Tail, N, Levels = 0) ->
|
||||
count_args(Tail, N + 1, Levels);
|
||||
count_args("," ++ Tail, N, Levels) ->
|
||||
count_args(Tail, N, Levels);
|
||||
count_args([_|Tail], N, Levels) ->
|
||||
count_args(Tail, N, Levels).
|
|
@ -15,83 +15,24 @@
|
|||
-module(cowboy_test).
|
||||
-compile(export_all).
|
||||
|
||||
%% Start and stop applications and their dependencies.
|
||||
|
||||
start(Apps) ->
|
||||
_ = [do_start(App) || App <- Apps],
|
||||
ok.
|
||||
|
||||
do_start(App) ->
|
||||
case application:start(App) of
|
||||
ok ->
|
||||
ok;
|
||||
{error, {not_started, Dep}} ->
|
||||
do_start(Dep),
|
||||
do_start(App)
|
||||
end.
|
||||
|
||||
%% SSL certificate creation and safekeeping.
|
||||
|
||||
make_certs() ->
|
||||
{_, Cert, Key} = ct_helper:make_certs(),
|
||||
CertOpts = [{cert, Cert}, {key, Key}],
|
||||
Pid = spawn(fun() -> receive after infinity -> ok end end),
|
||||
?MODULE = ets:new(?MODULE, [ordered_set, public, named_table,
|
||||
{heir, Pid, undefined}]),
|
||||
ets:insert(?MODULE, {cert_opts, CertOpts}),
|
||||
ok.
|
||||
|
||||
get_certs() ->
|
||||
ets:lookup_element(?MODULE, cert_opts, 2).
|
||||
|
||||
%% Quick configuration value retrieval.
|
||||
|
||||
config(Key, Config) ->
|
||||
{_, Value} = lists:keyfind(Key, 1, Config),
|
||||
Value.
|
||||
|
||||
%% Test case description.
|
||||
|
||||
doc(String) ->
|
||||
ct:comment(String),
|
||||
ct:log(String).
|
||||
|
||||
%% List of all test cases in the suite.
|
||||
|
||||
all(Suite) ->
|
||||
lists:usort([F || {F, 1} <- Suite:module_info(exports),
|
||||
F =/= module_info,
|
||||
F =/= test, %% This is leftover from the eunit parse_transform...
|
||||
F =/= all,
|
||||
F =/= groups,
|
||||
string:substr(atom_to_list(F), 1, 5) =/= "init_",
|
||||
string:substr(atom_to_list(F), 1, 4) =/= "end_",
|
||||
string:substr(atom_to_list(F), 1, 3) =/= "do_"
|
||||
]).
|
||||
-import(ct_helper, [config/2]).
|
||||
|
||||
%% Listeners initialization.
|
||||
|
||||
init_http(Ref, ProtoOpts, Config) ->
|
||||
{ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
|
||||
{max_keepalive, 50},
|
||||
{timeout, 500}
|
||||
|ProtoOpts]),
|
||||
{ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], ProtoOpts),
|
||||
Port = ranch:get_port(Ref),
|
||||
[{type, tcp}, {port, Port}, {opts, []}|Config].
|
||||
|
||||
init_https(Ref, ProtoOpts, Config) ->
|
||||
Opts = get_certs(),
|
||||
{ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
|
||||
{max_keepalive, 50},
|
||||
{timeout, 500}
|
||||
|ProtoOpts]),
|
||||
Opts = ct_helper:get_certs_from_ets(),
|
||||
{ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], ProtoOpts),
|
||||
Port = ranch:get_port(Ref),
|
||||
[{type, ssl}, {port, Port}, {opts, Opts}|Config].
|
||||
|
||||
init_spdy(Ref, ProtoOpts, Config) ->
|
||||
Opts = get_certs(),
|
||||
{ok, _} = cowboy:start_spdy(Ref, 100, Opts ++ [{port, 0}],
|
||||
ProtoOpts),
|
||||
Opts = ct_helper:get_certs_from_ets(),
|
||||
{ok, _} = cowboy:start_spdy(Ref, 100, Opts ++ [{port, 0}], ProtoOpts),
|
||||
Port = ranch:get_port(Ref),
|
||||
[{type, ssl}, {port, Port}, {opts, Opts}|Config].
|
||||
|
||||
|
@ -148,22 +89,17 @@ init_common_groups(Name = spdy_compress, Config, Mod) ->
|
|||
%% Support functions for testing using Gun.
|
||||
|
||||
gun_open(Config) ->
|
||||
gun_open(Config, []).
|
||||
gun_open(Config, #{}).
|
||||
|
||||
gun_open(Config, Opts) ->
|
||||
{ok, ConnPid} = gun:open("localhost", config(port, Config),
|
||||
[{retry, 0}, {type, config(type, Config)}|Opts]),
|
||||
{ok, ConnPid} = gun:open("localhost", config(port, Config), Opts#{
|
||||
retry => 0,
|
||||
transport => config(type, Config)
|
||||
}),
|
||||
ConnPid.
|
||||
|
||||
gun_monitor_open(Config) ->
|
||||
gun_monitor_open(Config, []).
|
||||
|
||||
gun_monitor_open(Config, Opts) ->
|
||||
ConnPid = gun_open(Config, Opts),
|
||||
{ConnPid, monitor(process, ConnPid)}.
|
||||
|
||||
gun_is_gone(ConnPid, MRef) ->
|
||||
receive {'DOWN', MRef, process, ConnPid, gone} -> ok
|
||||
gun_down(ConnPid) ->
|
||||
receive {gun_down, ConnPid, _, _, _, _} -> ok
|
||||
after 500 -> error(timeout) end.
|
||||
|
||||
%% Support functions for testing using a raw socket.
|
||||
|
@ -183,18 +119,21 @@ raw_send({raw_client, Socket, Transport}, Data) ->
|
|||
Transport:send(Socket, Data).
|
||||
|
||||
raw_recv_head({raw_client, Socket, Transport}) ->
|
||||
{ok, Data} = Transport:recv(Socket, 0, 5000),
|
||||
{ok, Data} = Transport:recv(Socket, 0, 10000),
|
||||
raw_recv_head(Socket, Transport, Data).
|
||||
|
||||
raw_recv_head(Socket, Transport, Buffer) ->
|
||||
case binary:match(Buffer, <<"\r\n\r\n">>) of
|
||||
nomatch ->
|
||||
{ok, Data} = Transport:recv(Socket, 0, 5000),
|
||||
{ok, Data} = Transport:recv(Socket, 0, 10000),
|
||||
raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
|
||||
{_, _} ->
|
||||
Buffer
|
||||
end.
|
||||
|
||||
raw_recv({raw_client, Socket, Transport}, Length, Timeout) ->
|
||||
Transport:recv(Socket, Length, Timeout).
|
||||
|
||||
raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
|
||||
{ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 5000),
|
||||
{ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 10000),
|
||||
ok.
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
%% Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
|
||||
%%
|
||||
%% Permission to use, copy, modify, and/or distribute this software for any
|
||||
%% purpose with or without fee is hereby granted, provided that the above
|
||||
%% copyright notice and this permission notice appear in all copies.
|
||||
%%
|
||||
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
-module(eunit_SUITE).
|
||||
-compile(export_all).
|
||||
|
||||
all() ->
|
||||
[eunit].
|
||||
|
||||
eunit(_) ->
|
||||
ok = eunit:test({application, cowboy}).
|
16
test/handlers/asterisk_h.erl
Normal file
16
test/handlers/asterisk_h.erl
Normal file
|
@ -0,0 +1,16 @@
|
|||
%% This module echoes back the value the test is interested in.
|
||||
|
||||
-module(asterisk_h).
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
echo(cowboy_req:header(<<"x-echo">>, Req), Req, Opts).
|
||||
|
||||
echo(What, Req, Opts) ->
|
||||
F = binary_to_atom(What, latin1),
|
||||
Value = case cowboy_req:F(Req) of
|
||||
V when is_integer(V) -> integer_to_binary(V);
|
||||
V -> V
|
||||
end,
|
||||
{ok, cowboy_req:reply(200, [], Value, Req), Opts}.
|
16
test/handlers/echo_h.erl
Normal file
16
test/handlers/echo_h.erl
Normal file
|
@ -0,0 +1,16 @@
|
|||
%% This module echoes back the value the test is interested in.
|
||||
|
||||
-module(echo_h).
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
echo(cowboy_req:binding(key, Req), Req, Opts).
|
||||
|
||||
echo(What, Req, Opts) ->
|
||||
F = binary_to_atom(What, latin1),
|
||||
Value = case cowboy_req:F(Req) of
|
||||
V when is_integer(V) -> integer_to_binary(V);
|
||||
V -> V
|
||||
end,
|
||||
{ok, cowboy_req:reply(200, [], Value, Req), Opts}.
|
8
test/handlers/hello_h.erl
Normal file
8
test/handlers/hello_h.erl
Normal file
|
@ -0,0 +1,8 @@
|
|||
%% This module sends a hello world response.
|
||||
|
||||
-module(hello_h).
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
{ok, cowboy_req:reply(200, [], <<"Hello world!">>, Req), Opts}.
|
|
@ -6,5 +6,5 @@
|
|||
-export([init/2]).
|
||||
|
||||
init(Req, content_length) ->
|
||||
cowboy_error_h:ignore(erlang, binary_to_integer, 1),
|
||||
ct_helper_error_h:ignore(erlang, binary_to_integer, 1),
|
||||
cowboy_req:parse_header(<<"content-length">>, Req).
|
||||
|
|
|
@ -16,11 +16,10 @@
|
|||
-module(http_SUITE).
|
||||
-compile(export_all).
|
||||
|
||||
-import(cowboy_test, [config/2]).
|
||||
-import(ct_helper, [config/2]).
|
||||
-import(cowboy_test, [gun_open/1]).
|
||||
-import(cowboy_test, [gun_monitor_open/1]).
|
||||
-import(cowboy_test, [gun_monitor_open/2]).
|
||||
-import(cowboy_test, [gun_is_gone/2]).
|
||||
-import(cowboy_test, [gun_open/2]).
|
||||
-import(cowboy_test, [gun_down/1]).
|
||||
-import(cowboy_test, [raw_open/1]).
|
||||
-import(cowboy_test, [raw_send/2]).
|
||||
-import(cowboy_test, [raw_recv_head/1]).
|
||||
|
@ -41,7 +40,7 @@ all() ->
|
|||
].
|
||||
|
||||
groups() ->
|
||||
Tests = cowboy_test:all(?MODULE) -- [
|
||||
Tests = ct_helper:all(?MODULE) -- [
|
||||
onresponse_crash, onresponse_reply, onresponse_capitalize,
|
||||
parse_host, set_env_dispatch
|
||||
],
|
||||
|
@ -95,18 +94,14 @@ init_per_group(Name = https_compress, Config) ->
|
|||
init_per_group(onresponse, Config) ->
|
||||
{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
|
||||
{env, [{dispatch, init_dispatch(Config)}]},
|
||||
{max_keepalive, 50},
|
||||
{onresponse, fun do_onresponse_hook/4},
|
||||
{timeout, 500}
|
||||
{onresponse, fun do_onresponse_hook/4}
|
||||
]),
|
||||
Port = ranch:get_port(onresponse),
|
||||
[{type, tcp}, {port, Port}, {opts, []}|Config];
|
||||
init_per_group(onresponse_capitalize, Config) ->
|
||||
{ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
|
||||
{env, [{dispatch, init_dispatch(Config)}]},
|
||||
{max_keepalive, 50},
|
||||
{onresponse, fun do_onresponse_capitalize_hook/4},
|
||||
{timeout, 500}
|
||||
{onresponse, fun do_onresponse_capitalize_hook/4}
|
||||
]),
|
||||
Port = ranch:get_port(onresponse_capitalize),
|
||||
[{type, tcp}, {port, Port}, {opts, []}|Config];
|
||||
|
@ -117,17 +112,13 @@ init_per_group(parse_host, Config) ->
|
|||
]}
|
||||
]),
|
||||
{ok, _} = cowboy:start_http(parse_host, 100, [{port, 0}], [
|
||||
{env, [{dispatch, Dispatch}]},
|
||||
{max_keepalive, 50},
|
||||
{timeout, 500}
|
||||
{env, [{dispatch, Dispatch}]}
|
||||
]),
|
||||
Port = ranch:get_port(parse_host),
|
||||
[{type, tcp}, {port, Port}, {opts, []}|Config];
|
||||
init_per_group(set_env, Config) ->
|
||||
{ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
|
||||
{env, [{dispatch, []}]},
|
||||
{max_keepalive, 50},
|
||||
{timeout, 500}
|
||||
{env, [{dispatch, []}]}
|
||||
]),
|
||||
Port = ranch:get_port(set_env),
|
||||
[{type, tcp}, {port, Port}, {opts, []}|Config].
|
||||
|
@ -342,26 +333,26 @@ echo_body_qs_max_length(Config) ->
|
|||
ok.
|
||||
|
||||
error_init_after_reply(Config) ->
|
||||
{ConnPid, MRef} = gun_monitor_open(Config),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
|
||||
{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
|
||||
gun_is_gone(ConnPid, MRef).
|
||||
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
||||
gun_down(ConnPid).
|
||||
|
||||
headers_dupe(Config) ->
|
||||
{ConnPid, MRef} = gun_monitor_open(Config),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:get(ConnPid, "/headers/dupe"),
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
||||
%% Ensure that only one connection header was received.
|
||||
[<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
|
||||
gun_is_gone(ConnPid, MRef).
|
||||
gun_down(ConnPid).
|
||||
|
||||
http10_chunkless(Config) ->
|
||||
{ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
|
||||
ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
|
||||
Ref = gun:get(ConnPid, "/chunked_response"),
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
||||
false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
|
||||
{ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref, MRef),
|
||||
gun_is_gone(ConnPid, MRef).
|
||||
{ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
|
||||
gun_down(ConnPid).
|
||||
|
||||
http10_hostless(Config) ->
|
||||
Name = http10_hostless,
|
||||
|
@ -411,17 +402,17 @@ http10_keepalive_forced(Config) ->
|
|||
end.
|
||||
|
||||
keepalive_max(Config) ->
|
||||
{ConnPid, MRef} = gun_monitor_open(Config),
|
||||
ConnPid = gun_open(Config),
|
||||
Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
|
||||
|| _ <- lists:seq(1, 49)],
|
||||
|| _ <- lists:seq(1, 99)],
|
||||
CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
|
||||
_ = [begin
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
||||
{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
|
||||
end || Ref <- Refs],
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef, MRef),
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef),
|
||||
{_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
|
||||
gun_is_gone(ConnPid, MRef).
|
||||
gun_down(ConnPid).
|
||||
|
||||
keepalive_nl(Config) ->
|
||||
ConnPid = gun_open(Config),
|
||||
|
@ -440,7 +431,7 @@ keepalive_stream_loop(Config) ->
|
|||
ConnPid = gun_open(Config),
|
||||
Refs = [begin
|
||||
Ref = gun:post(ConnPid, "/loop_stream_recv",
|
||||
[{<<"transfer-encoding">>, <<"chunked">>}]),
|
||||
[{<<"content-type">>, <<"application/octet-stream">>}]),
|
||||
_ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
|
||||
|| ID <- lists:seq(1, 250)],
|
||||
gun:data(ConnPid, Ref, fin, <<>>),
|
||||
|
@ -481,9 +472,8 @@ multipart_chunked(Config) ->
|
|||
"\r\n--OHai--\r\n"
|
||||
"This is an epilogue."
|
||||
>>,
|
||||
Ref = gun:post(ConnPid, "/multipart", [
|
||||
{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>},
|
||||
{<<"transfer-encoding">>, <<"chunked">>}]),
|
||||
Ref = gun:post(ConnPid, "/multipart",
|
||||
[{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}]),
|
||||
gun:data(ConnPid, Ref, fin, Body),
|
||||
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
||||
{ok, RespBody} = gun:await_body(ConnPid, Ref),
|
||||
|
@ -614,10 +604,12 @@ rest_param_all(Config) ->
|
|||
%% Content-Type without param.
|
||||
Ref6 = gun:put(ConnPid, "/param_all",
|
||||
[{<<"content-type">>, <<"text/plain">>}]),
|
||||
gun:data(ConnPid, Ref6, fin, "Hello world!"),
|
||||
{response, fin, 204, _} = gun:await(ConnPid, Ref6),
|
||||
%% Content-Type with param.
|
||||
Ref7 = gun:put(ConnPid, "/param_all",
|
||||
[{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
|
||||
gun:data(ConnPid, Ref7, fin, "Hello world!"),
|
||||
{response, fin, 204, _} = gun:await(ConnPid, Ref7),
|
||||
ok.
|
||||
|
||||
|
@ -662,12 +654,13 @@ rest_keepalive(Config) ->
|
|||
|
||||
rest_keepalive_post(Config) ->
|
||||
ConnPid = gun_open(Config),
|
||||
Refs = [{
|
||||
gun:post(ConnPid, "/forbidden_post",
|
||||
[{<<"content-type">>, <<"text/plain">>}]),
|
||||
gun:post(ConnPid, "/simple_post",
|
||||
[{<<"content-type">>, <<"text/plain">>}])
|
||||
} || _ <- lists:seq(1, 5)],
|
||||
Refs = [begin
|
||||
Ref1 = gun:post(ConnPid, "/forbidden_post", [{<<"content-type">>, <<"text/plain">>}]),
|
||||
gun:data(ConnPid, Ref1, fin, "Hello world!"),
|
||||
Ref2 = gun:post(ConnPid, "/simple_post", [{<<"content-type">>, <<"text/plain">>}]),
|
||||
gun:data(ConnPid, Ref2, fin, "Hello world!"),
|
||||
{Ref1, Ref2}
|
||||
end || _ <- lists:seq(1, 5)],
|
||||
_ = [begin
|
||||
{response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
|
||||
{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
|
||||
|
@ -808,7 +801,7 @@ slowloris(Config) ->
|
|||
try
|
||||
[begin
|
||||
ok = raw_send(Client, [C]),
|
||||
receive after 25 -> ok end
|
||||
receive after 250 -> ok end
|
||||
end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
|
||||
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
|
||||
"Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
|
||||
|
@ -894,11 +887,11 @@ stream_body_set_resp(Config) ->
|
|||
ok.
|
||||
|
||||
stream_body_set_resp_close(Config) ->
|
||||
{ConnPid, MRef} = gun_monitor_open(Config),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
|
||||
{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
|
||||
{ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref, MRef),
|
||||
gun_is_gone(ConnPid, MRef).
|
||||
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
||||
{ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref),
|
||||
gun_down(ConnPid).
|
||||
|
||||
stream_body_set_resp_chunked(Config) ->
|
||||
ConnPid = gun_open(Config),
|
||||
|
@ -909,12 +902,12 @@ stream_body_set_resp_chunked(Config) ->
|
|||
ok.
|
||||
|
||||
stream_body_set_resp_chunked10(Config) ->
|
||||
{ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
|
||||
ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
|
||||
Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
||||
false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
|
||||
{ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref, MRef),
|
||||
gun_is_gone(ConnPid, MRef).
|
||||
{ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
|
||||
gun_down(ConnPid).
|
||||
|
||||
%% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
|
||||
streamed_response(Config) ->
|
||||
|
@ -933,8 +926,8 @@ streamed_response(Config) ->
|
|||
te_chunked(Config) ->
|
||||
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:post(ConnPid, "/echo/body",
|
||||
[{<<"transfer-encoding">>, <<"chunked">>}], Body),
|
||||
Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]),
|
||||
gun:data(ConnPid, Ref, fin, Body),
|
||||
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
||||
{ok, Body} = gun:await_body(ConnPid, Ref),
|
||||
ok.
|
||||
|
@ -957,7 +950,7 @@ te_chunked_chopped(Config) ->
|
|||
Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:post(ConnPid, "/echo/body",
|
||||
[{<<"transfer-encoding">>, <<"chunked">>}]),
|
||||
[{<<"content-type">>, <<"text/plain">>}]),
|
||||
_ = [begin
|
||||
ok = gun:dbg_send_raw(ConnPid, << C >>),
|
||||
receive after 10 -> ok end
|
||||
|
@ -971,7 +964,7 @@ te_chunked_delayed(Config) ->
|
|||
Chunks = do_body_to_chunks(50, Body, []),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:post(ConnPid, "/echo/body",
|
||||
[{<<"transfer-encoding">>, <<"chunked">>}]),
|
||||
[{<<"content-type">>, <<"text/plain">>}]),
|
||||
_ = [begin
|
||||
ok = gun:dbg_send_raw(ConnPid, Chunk),
|
||||
receive after 10 -> ok end
|
||||
|
@ -985,7 +978,7 @@ te_chunked_split_body(Config) ->
|
|||
Chunks = do_body_to_chunks(50, Body, []),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:post(ConnPid, "/echo/body",
|
||||
[{<<"transfer-encoding">>, <<"chunked">>}]),
|
||||
[{<<"content-type">>, <<"text/plain">>}]),
|
||||
_ = [begin
|
||||
case Chunk of
|
||||
<<"0\r\n\r\n">> ->
|
||||
|
@ -1009,7 +1002,7 @@ te_chunked_split_crlf(Config) ->
|
|||
Chunks = do_body_to_chunks(50, Body, []),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:post(ConnPid, "/echo/body",
|
||||
[{<<"transfer-encoding">>, <<"chunked">>}]),
|
||||
[{<<"content-type">>, <<"text/plain">>}]),
|
||||
_ = [begin
|
||||
%% Split in the newline just before the end of the chunk.
|
||||
Len = byte_size(Chunk) - (random:uniform(2) - 1),
|
||||
|
|
|
@ -9,9 +9,9 @@ init(Req, _Opts) ->
|
|||
case_init(Case, Req).
|
||||
|
||||
case_init(<<"init_before_reply">> = Case, _Req) ->
|
||||
cowboy_error_h:ignore(?MODULE, case_init, 2),
|
||||
ct_helper_error_h:ignore(?MODULE, case_init, 2),
|
||||
error(Case);
|
||||
case_init(<<"init_after_reply">> = Case, Req) ->
|
||||
cowboy_error_h:ignore(?MODULE, case_init, 2),
|
||||
ct_helper_error_h:ignore(?MODULE, case_init, 2),
|
||||
_ = cowboy_req:reply(200, [], "http_handler_crashes", Req),
|
||||
error(Case).
|
||||
|
|
|
@ -12,13 +12,13 @@ allowed_methods(Req, State) ->
|
|||
{[<<"GET">>, <<"PUT">>], Req, State}.
|
||||
|
||||
content_types_accepted(Req, State) ->
|
||||
cowboy_error_h:ignore(cowboy_rest, process_content_type, 3),
|
||||
ct_helper_error_h:ignore(cowboy_rest, process_content_type, 3),
|
||||
{[
|
||||
{<<"application/json">>, put_application_json}
|
||||
], Req, State}.
|
||||
|
||||
content_types_provided(Req, State) ->
|
||||
cowboy_error_h:ignore(cowboy_rest, set_resp_body, 2),
|
||||
ct_helper_error_h:ignore(cowboy_rest, set_resp_body, 2),
|
||||
{[
|
||||
{<<"text/plain">>, get_text_plain}
|
||||
], Req, State}.
|
||||
|
|
|
@ -23,10 +23,10 @@ generate_etag(Req, State) ->
|
|||
{<<"\"etag-header-value\"">>, Req, State};
|
||||
%% Invalid return values from generate_etag/2.
|
||||
<<"binary-strong-unquoted">> ->
|
||||
cowboy_error_h:ignore(cow_http_hd, parse_etag, 1),
|
||||
ct_helper_error_h:ignore(cow_http_hd, parse_etag, 1),
|
||||
{<<"etag-header-value">>, Req, State};
|
||||
<<"binary-weak-unquoted">> ->
|
||||
cowboy_error_h:ignore(cow_http_hd, parse_etag, 1),
|
||||
ct_helper_error_h:ignore(cow_http_hd, parse_etag, 1),
|
||||
{<<"W/etag-header-value">>, Req, State}
|
||||
end.
|
||||
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
-module(loop_handler_SUITE).
|
||||
-compile(export_all).
|
||||
|
||||
-import(cowboy_test, [config/2]).
|
||||
-import(cowboy_test, [doc/1]).
|
||||
-import(ct_helper, [config/2]).
|
||||
-import(ct_helper, [doc/1]).
|
||||
-import(cowboy_test, [gun_open/1]).
|
||||
|
||||
%% ct.
|
||||
|
@ -25,7 +25,7 @@ all() ->
|
|||
cowboy_test:common_all().
|
||||
|
||||
groups() ->
|
||||
cowboy_test:common_groups(cowboy_test:all(?MODULE)).
|
||||
cowboy_test:common_groups(ct_helper:all(?MODULE)).
|
||||
|
||||
init_per_group(Name, Config) ->
|
||||
cowboy_test:init_common_groups(Name, Config, ?MODULE).
|
||||
|
|
1388
test/rfc7230_SUITE.erl
Normal file
1388
test/rfc7230_SUITE.erl
Normal file
File diff suppressed because it is too large
Load diff
|
@ -15,8 +15,8 @@
|
|||
-module(spdy_SUITE).
|
||||
-compile(export_all).
|
||||
|
||||
-import(cowboy_test, [config/2]).
|
||||
-import(cowboy_test, [gun_monitor_open/1]).
|
||||
-import(ct_helper, [config/2]).
|
||||
-import(cowboy_test, [gun_open/1]).
|
||||
-import(cowboy_test, [raw_open/1]).
|
||||
-import(cowboy_test, [raw_send/2]).
|
||||
|
||||
|
@ -26,7 +26,7 @@ all() ->
|
|||
[{group, spdy}].
|
||||
|
||||
groups() ->
|
||||
[{spdy, [], cowboy_test:all(?MODULE)}].
|
||||
[{spdy, [], ct_helper:all(?MODULE)}].
|
||||
|
||||
init_per_suite(Config) ->
|
||||
case proplists:get_value(ssl_app, ssl:versions()) of
|
||||
|
@ -64,9 +64,9 @@ init_dispatch(Config) ->
|
|||
|
||||
%% Convenience functions.
|
||||
|
||||
do_get(ConnPid, MRef, Host, Path) ->
|
||||
StreamRef = gun:get(ConnPid, Path, [{":host", Host}]),
|
||||
{response, IsFin, Status, _} = gun:await(ConnPid, StreamRef, MRef),
|
||||
do_get(ConnPid, Host, Path) ->
|
||||
StreamRef = gun:get(ConnPid, Path, [{<<"host">>, Host}]),
|
||||
{response, IsFin, Status, _} = gun:await(ConnPid, StreamRef),
|
||||
{IsFin, Status}.
|
||||
|
||||
%% Tests.
|
||||
|
@ -80,25 +80,25 @@ check_status(Config) ->
|
|||
{400, fin, "localhost", "bad-path"},
|
||||
{404, fin, "localhost", "/this/path/does/not/exist"}
|
||||
],
|
||||
{ConnPid, MRef} = gun_monitor_open(Config),
|
||||
ConnPid = gun_open(Config),
|
||||
_ = [{Status, Fin, Host, Path} = begin
|
||||
{IsFin, Ret} = do_get(ConnPid, MRef, Host, Path),
|
||||
{IsFin, Ret} = do_get(ConnPid, Host, Path),
|
||||
{Ret, IsFin, Host, Path}
|
||||
end || {Status, Fin, Host, Path} <- Tests],
|
||||
gun:close(ConnPid).
|
||||
|
||||
echo_body(Config) ->
|
||||
{ConnPid, MRef} = gun_monitor_open(Config),
|
||||
ConnPid = gun_open(Config),
|
||||
Body = << 0:800000 >>,
|
||||
StreamRef = gun:post(ConnPid, "/echo/body", [
|
||||
{<<"content-type">>, "application/octet-stream"}
|
||||
], Body),
|
||||
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
|
||||
{ok, Body} = gun:await_body(ConnPid, StreamRef, MRef),
|
||||
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef),
|
||||
{ok, Body} = gun:await_body(ConnPid, StreamRef),
|
||||
gun:close(ConnPid).
|
||||
|
||||
echo_body_multi(Config) ->
|
||||
{ConnPid, MRef} = gun_monitor_open(Config),
|
||||
ConnPid = gun_open(Config),
|
||||
BodyChunk = << 0:80000 >>,
|
||||
StreamRef = gun:post(ConnPid, "/echo/body", [
|
||||
%% @todo I'm still unhappy with this. It shouldn't be required...
|
||||
|
@ -107,8 +107,8 @@ echo_body_multi(Config) ->
|
|||
]),
|
||||
_ = [gun:data(ConnPid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)],
|
||||
gun:data(ConnPid, StreamRef, fin, BodyChunk),
|
||||
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
|
||||
{ok, << 0:800000 >>} = gun:await_body(ConnPid, StreamRef, MRef),
|
||||
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef),
|
||||
{ok, << 0:800000 >>} = gun:await_body(ConnPid, StreamRef),
|
||||
gun:close(ConnPid).
|
||||
|
||||
two_frames_one_packet(Config) ->
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
-module(ws_SUITE).
|
||||
-compile(export_all).
|
||||
|
||||
-import(cowboy_test, [config/2]).
|
||||
-import(ct_helper, [config/2]).
|
||||
|
||||
%% ct.
|
||||
|
||||
|
@ -23,12 +23,9 @@ all() ->
|
|||
[{group, autobahn}, {group, ws}].
|
||||
|
||||
groups() ->
|
||||
BaseTests = cowboy_test:all(?MODULE) -- [autobahn_fuzzingclient],
|
||||
BaseTests = ct_helper:all(?MODULE) -- [autobahn_fuzzingclient],
|
||||
[{autobahn, [], [autobahn_fuzzingclient]}, {ws, [parallel], BaseTests}].
|
||||
|
||||
init_per_suite(Config) ->
|
||||
Config.
|
||||
|
||||
init_per_group(Name = autobahn, Config) ->
|
||||
%% Some systems have it named pip2.
|
||||
Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue