0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Move listener initialization to cowboy_test

This commit is contained in:
Loïc Hoguin 2014-04-22 22:50:45 +02:00
parent b377eb9805
commit 25a17a2590
4 changed files with 65 additions and 59 deletions

View file

@ -49,6 +49,34 @@ all(Suite) ->
string:substr(atom_to_list(F), 1, 3) =/= "do_"
]).
%% Listeners initialization.
init_http(Ref, ProtoOpts, Config) ->
{ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
{max_keepalive, 50},
{timeout, 500}
|ProtoOpts]),
Port = ranch:get_port(Ref),
[{type, tcp}, {port, Port}, {opts, []}|Config].
init_https(Ref, ProtoOpts, Config) ->
{_, Cert, Key} = ct_helper:make_certs(),
Opts = [{cert, Cert}, {key, Key}],
{ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
{max_keepalive, 50},
{timeout, 500}
|ProtoOpts]),
Port = ranch:get_port(Ref),
[{type, ssl}, {port, Port}, {opts, Opts}|Config].
init_spdy(Ref, ProtoOpts, Config) ->
{_, Cert, Key} = ct_helper:make_certs(),
Opts = [{cert, Cert}, {key, Key}],
{ok, _} = cowboy:start_spdy(Ref, 100, Opts ++ [{port, 0}],
ProtoOpts),
Port = ranch:get_port(Ref),
[{type, ssl}, {port, Port}, {opts, Opts}|Config].
%% Support functions for testing using Gun.
gun_open(Config) ->

View file

@ -79,39 +79,26 @@ init_per_suite(Config) ->
end_per_suite(Config) ->
ct_helper:delete_static_dir(config(static_dir, Config)).
init_tcp_group(Ref, ProtoOpts, Config) ->
Transport = ranch_tcp,
{ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
init_per_group(Name = http, Config) ->
cowboy_test:init_http(Name, [
{env, [{dispatch, init_dispatch(Config)}]}
], Config);
init_per_group(Name = https, Config) ->
cowboy_test:init_https(Name, [
{env, [{dispatch, init_dispatch(Config)}]}
], Config);
init_per_group(Name = http_compress, Config) ->
cowboy_test:init_http(Name, [
{env, [{dispatch, init_dispatch(Config)}]},
{max_keepalive, 50},
{timeout, 500}
|ProtoOpts]),
Port = ranch:get_port(Ref),
[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config].
init_ssl_group(Ref, ProtoOpts, Config) ->
Transport = ranch_ssl,
{_, Cert, Key} = ct_helper:make_certs(),
Opts = [{cert, Cert}, {key, Key}],
{ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
{compress, true}
], Config);
init_per_group(Name = https_compress, Config) ->
cowboy_test:init_https(Name, [
{env, [{dispatch, init_dispatch(Config)}]},
{max_keepalive, 50},
{timeout, 500}
|ProtoOpts]),
Port = ranch:get_port(Ref),
[{type, ssl}, {port, Port}, {opts, Opts}, {transport, Transport}|Config].
init_per_group(http, Config) ->
init_tcp_group(http, [], Config);
init_per_group(https, Config) ->
init_ssl_group(https, [], Config);
init_per_group(http_compress, Config) ->
init_tcp_group(http_compress, [{compress, true}], Config);
init_per_group(https_compress, Config) ->
init_ssl_group(https_compress, [{compress, true}], Config);
{compress, true}
], Config);
%% Most, if not all of these, should be in separate test suites.
init_per_group(onrequest, Config) ->
Transport = ranch_tcp,
{ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
{env, [{dispatch, init_dispatch(Config)}]},
{max_keepalive, 50},
@ -119,9 +106,8 @@ init_per_group(onrequest, Config) ->
{timeout, 500}
]),
Port = ranch:get_port(onrequest),
[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
[{type, tcp}, {port, Port}, {opts, []}|Config];
init_per_group(onresponse, Config) ->
Transport = ranch_tcp,
{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
{env, [{dispatch, init_dispatch(Config)}]},
{max_keepalive, 50},
@ -129,9 +115,8 @@ init_per_group(onresponse, Config) ->
{timeout, 500}
]),
Port = ranch:get_port(onresponse),
[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
[{type, tcp}, {port, Port}, {opts, []}|Config];
init_per_group(onresponse_capitalize, Config) ->
Transport = ranch_tcp,
{ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
{env, [{dispatch, init_dispatch(Config)}]},
{max_keepalive, 50},
@ -139,9 +124,8 @@ init_per_group(onresponse_capitalize, Config) ->
{timeout, 500}
]),
Port = ranch:get_port(onresponse_capitalize),
[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
[{type, tcp}, {port, Port}, {opts, []}|Config];
init_per_group(parse_host, Config) ->
Transport = ranch_tcp,
Dispatch = cowboy_router:compile([
{'_', [
{"/req_attr", http_req_attr, []}
@ -153,20 +137,18 @@ init_per_group(parse_host, Config) ->
{timeout, 500}
]),
Port = ranch:get_port(http),
[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
[{type, tcp}, {port, Port}, {opts, []}|Config];
init_per_group(set_env, Config) ->
Transport = ranch_tcp,
{ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
{env, [{dispatch, []}]},
{max_keepalive, 50},
{timeout, 500}
]),
Port = ranch:get_port(set_env),
[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config].
[{type, tcp}, {port, Port}, {opts, []}|Config].
end_per_group(Name, _) ->
cowboy:stop_listener(Name),
ok.
cowboy:stop_listener(Name).
%% Dispatch configuration.
@ -436,8 +418,12 @@ http10_chunkless(Config) ->
http10_hostless(Config) ->
Port10 = config(port, Config) + 10,
Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
ranch:start_listener(Name, 5,
config(transport, Config), config(opts, Config) ++ [{port, Port10}],
Transport = case config(type, Config) of
tcp -> ranch_tcp;
ssl -> ranch_ssl
end,
ranch:start_listener(Name, 5, Transport,
config(opts, Config) ++ [{port, Port10}],
cowboy_protocol, [
{env, [{dispatch, cowboy_router:compile([
{'_', [{"/http1.0/hostless", http_handler, []}]}])}]},

View file

@ -40,17 +40,12 @@ end_per_suite(Config) ->
ct_helper:delete_static_dir(config(static_dir, Config)).
init_per_group(Name, Config) ->
{_, Cert, Key} = ct_helper:make_certs(),
Opts = [{cert, Cert}, {key, Key}],
{ok, _} = cowboy:start_spdy(Name, 100, Opts ++ [{port, 0}], [
cowboy_test:init_spdy(Name, [
{env, [{dispatch, init_dispatch(Config)}]}
]),
Port = ranch:get_port(Name),
[{port, Port}, {type, ssl}|Config].
], Config).
end_per_group(Name, _) ->
cowboy:stop_listener(Name),
ok.
cowboy:stop_listener(Name).
%% Dispatch configuration.

View file

@ -29,7 +29,7 @@ groups() ->
init_per_suite(Config) ->
Config.
init_per_group(autobahn, Config) ->
init_per_group(Name = autobahn, Config) ->
%% Some systems have it named pip2.
Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
case string:str(Out, "autobahntestsuite") of
@ -40,21 +40,18 @@ init_per_group(autobahn, Config) ->
"http://autobahn.ws/testsuite/installation.html"),
{skip, "Autobahn Test Suite not installed."};
_ ->
{ok, _} = cowboy:start_http(autobahn, 100, [{port, 33080}], [
{ok, _} = cowboy:start_http(Name, 100, [{port, 33080}], [
{env, [{dispatch, init_dispatch()}]}]),
Config
end;
init_per_group(ws, Config) ->
cowboy:start_http(ws, 100, [{port, 0}], [
init_per_group(Name = ws, Config) ->
cowboy_test:init_http(Name, [
{env, [{dispatch, init_dispatch()}]},
{compress, true}
]),
Port = ranch:get_port(ws),
[{port, Port}|Config].
], Config).
end_per_group(Listener, _Config) ->
cowboy:stop_listener(Listener),
ok.
cowboy:stop_listener(Listener).
%% Dispatch configuration.