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

Cleanup the configuration handling in http_SUITE

This commit is contained in:
Loïc Hoguin 2014-04-17 19:15:39 +02:00
parent 23eb3f8e9a
commit 806cde9ace

View file

@ -16,7 +16,9 @@
-module(http_SUITE). -module(http_SUITE).
-compile(export_all). -compile(export_all).
-include_lib("common_test/include/ct.hrl"). config(Key, Config) ->
{_, Value} = lists:keyfind(Key, 1, Config),
Value.
%% ct. %% ct.
@ -131,12 +133,12 @@ init_per_suite(Config) ->
application:start(cowlib), application:start(cowlib),
application:start(gun), application:start(gun),
application:start(cowboy), application:start(cowboy),
Dir = ?config(priv_dir, Config) ++ "/static", Dir = config(priv_dir, Config) ++ "/static",
ct_helper:create_static_dir(Dir), ct_helper:create_static_dir(Dir),
[{static_dir, Dir}|Config]. [{static_dir, Dir}|Config].
end_per_suite(Config) -> end_per_suite(Config) ->
Dir = ?config(static_dir, Config), Dir = config(static_dir, Config),
ct_helper:delete_static_dir(Dir), ct_helper:delete_static_dir(Dir),
application:stop(cowboy), application:stop(cowboy),
application:stop(gun), application:stop(gun),
@ -188,8 +190,7 @@ init_per_group(onrequest, Config) ->
{timeout, 500} {timeout, 500}
]), ]),
Port = ranch:get_port(onrequest), Port = ranch:get_port(onrequest),
[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []}, [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
{transport, Transport}|Config];
init_per_group(onresponse, Config) -> init_per_group(onresponse, Config) ->
Transport = ranch_tcp, Transport = ranch_tcp,
{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [ {ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
@ -199,8 +200,7 @@ init_per_group(onresponse, Config) ->
{timeout, 500} {timeout, 500}
]), ]),
Port = ranch:get_port(onresponse), Port = ranch:get_port(onresponse),
[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []}, [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
{transport, Transport}|Config];
init_per_group(onresponse_capitalize, Config) -> init_per_group(onresponse_capitalize, Config) ->
Transport = ranch_tcp, Transport = ranch_tcp,
{ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [ {ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
@ -210,8 +210,7 @@ init_per_group(onresponse_capitalize, Config) ->
{timeout, 500} {timeout, 500}
]), ]),
Port = ranch:get_port(onresponse_capitalize), Port = ranch:get_port(onresponse_capitalize),
[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []}, [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
{transport, Transport}|Config];
init_per_group(parse_host, Config) -> init_per_group(parse_host, Config) ->
Transport = ranch_tcp, Transport = ranch_tcp,
Dispatch = cowboy_router:compile([ Dispatch = cowboy_router:compile([
@ -225,8 +224,7 @@ init_per_group(parse_host, Config) ->
{timeout, 500} {timeout, 500}
]), ]),
Port = ranch:get_port(http), Port = ranch:get_port(http),
[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []}, [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
{transport, Transport}|Config];
init_per_group(set_env, Config) -> init_per_group(set_env, Config) ->
Transport = ranch_tcp, Transport = ranch_tcp,
{ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [ {ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
@ -235,8 +233,7 @@ init_per_group(set_env, Config) ->
{timeout, 500} {timeout, 500}
]), ]),
Port = ranch:get_port(set_env), Port = ranch:get_port(set_env),
[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []}, [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config].
{transport, Transport}|Config].
end_per_group(Name, _) -> end_per_group(Name, _) ->
cowboy:stop_listener(Name), cowboy:stop_listener(Name),
@ -270,18 +267,18 @@ init_dispatch(Config) ->
{reply, set_resp_chunked}, {reply, set_resp_chunked},
{body, [<<"stream_body">>, <<"_set_resp_chunked">>]}]}, {body, [<<"stream_body">>, <<"_set_resp_chunked">>]}]},
{"/static/[...]", cowboy_static, {"/static/[...]", cowboy_static,
{dir, ?config(static_dir, Config)}}, {dir, config(static_dir, Config)}},
{"/static_mimetypes_function/[...]", cowboy_static, {"/static_mimetypes_function/[...]", cowboy_static,
{dir, ?config(static_dir, Config), {dir, config(static_dir, Config),
[{mimetypes, ?MODULE, mimetypes_text_html}]}}, [{mimetypes, ?MODULE, mimetypes_text_html}]}},
{"/handler_errors", http_errors, []}, {"/handler_errors", http_errors, []},
{"/static_attribute_etag/[...]", cowboy_static, {"/static_attribute_etag/[...]", cowboy_static,
{dir, ?config(static_dir, Config)}}, {dir, config(static_dir, Config)}},
{"/static_function_etag/[...]", cowboy_static, {"/static_function_etag/[...]", cowboy_static,
{dir, ?config(static_dir, Config), {dir, config(static_dir, Config),
[{etag, ?MODULE, etag_gen}]}}, [{etag, ?MODULE, etag_gen}]}},
{"/static_specify_file/[...]", cowboy_static, {"/static_specify_file/[...]", cowboy_static,
{file, ?config(static_dir, Config) ++ "/style.css"}}, {file, config(static_dir, Config) ++ "/style.css"}},
{"/multipart", http_multipart, []}, {"/multipart", http_multipart, []},
{"/multipart/large", http_multipart_stream, []}, {"/multipart/large", http_multipart_stream, []},
{"/echo/body", http_echo_body, []}, {"/echo/body", http_echo_body, []},
@ -320,9 +317,8 @@ gun_open(Config) ->
gun_open(Config, []). gun_open(Config, []).
gun_open(Config, Opts) -> gun_open(Config, Opts) ->
{_, Port} = lists:keyfind(port, 1, Config), {ok, ConnPid} = gun:open("localhost", config(port, Config),
{_, Type} = lists:keyfind(type, 1, Config), [{retry, 0}, {type, config(type, Config)}|Opts]),
{ok, ConnPid} = gun:open("localhost", Port, [{retry, 0}, {type, Type}|Opts]),
ConnPid. ConnPid.
gun_monitor_open(Config) -> gun_monitor_open(Config) ->
@ -342,14 +338,12 @@ gun_is_gone(ConnPid, MRef) ->
%% Support functions for testing using a raw socket. %% Support functions for testing using a raw socket.
raw_open(Config) -> raw_open(Config) ->
{_, Port} = lists:keyfind(port, 1, Config), Transport = case config(type, Config) of
{_, Type} = lists:keyfind(type, 1, Config),
Transport = case Type of
tcp -> gen_tcp; tcp -> gen_tcp;
ssl -> ssl ssl -> ssl
end, end,
{_, Opts} = lists:keyfind(opts, 1, Config), {_, Opts} = lists:keyfind(opts, 1, Config),
{ok, Socket} = Transport:connect("localhost", Port, {ok, Socket} = Transport:connect("localhost", config(port, Config),
[binary, {active, false}, {packet, raw}, [binary, {active, false}, {packet, raw},
{reuseaddr, true}, {nodelay, true}|Opts]), {reuseaddr, true}, {nodelay, true}|Opts]),
{raw_client, Socket, Transport}. {raw_client, Socket, Transport}.
@ -566,10 +560,10 @@ http10_chunkless(Config) ->
gun_is_gone(ConnPid, MRef). gun_is_gone(ConnPid, MRef).
http10_hostless(Config) -> http10_hostless(Config) ->
Port10 = ?config(port, Config) + 10, Port10 = config(port, Config) + 10,
Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)), Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
ranch:start_listener(Name, 5, ranch:start_listener(Name, 5,
?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}], config(transport, Config), config(opts, Config) ++ [{port, Port10}],
cowboy_protocol, [ cowboy_protocol, [
{env, [{dispatch, cowboy_router:compile([ {env, [{dispatch, cowboy_router:compile([
{'_', [{"/http1.0/hostless", http_handler, []}]}])}]}, {'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
@ -666,8 +660,7 @@ nc_reqs(Config, Input) ->
{skip, {notfound, nc}}; {skip, {notfound, nc}};
_Good -> _Good ->
%% Throw garbage at the server then check if it's still up. %% Throw garbage at the server then check if it's still up.
{port, Port} = lists:keyfind(port, 1, Config), StrPort = integer_to_list(config(port, Config)),
StrPort = integer_to_list(Port),
[os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort) [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
|| _ <- lists:seq(1, 100)], || _ <- lists:seq(1, 100)],
200 = quick_get("/", Config) 200 = quick_get("/", Config)