chore(authz): make test http server more robust
This commit is contained in:
parent
2bada0bab8
commit
9363b6110e
|
@ -50,7 +50,7 @@ set_special_configs(_) ->
|
||||||
|
|
||||||
init_per_testcase(_Case, Config) ->
|
init_per_testcase(_Case, Config) ->
|
||||||
ok = emqx_authz_test_lib:reset_authorizers(),
|
ok = emqx_authz_test_lib:reset_authorizers(),
|
||||||
ok = emqx_authz_http_test_server:start(?HTTP_PORT, ?HTTP_PATH),
|
{ok, _} = emqx_authz_http_test_server:start_link(?HTTP_PORT, ?HTTP_PATH),
|
||||||
Config.
|
Config.
|
||||||
|
|
||||||
end_per_testcase(_Case, _Config) ->
|
end_per_testcase(_Case, _Config) ->
|
||||||
|
|
|
@ -16,20 +16,17 @@
|
||||||
|
|
||||||
-module(emqx_authz_http_test_server).
|
-module(emqx_authz_http_test_server).
|
||||||
|
|
||||||
-behaviour(gen_server).
|
-behaviour(supervisor).
|
||||||
-behaviour(cowboy_handler).
|
-behaviour(cowboy_handler).
|
||||||
|
|
||||||
% cowboy_server callbacks
|
% cowboy_server callbacks
|
||||||
-export([init/2]).
|
-export([init/2]).
|
||||||
|
|
||||||
% gen_server callbacks
|
% supervisor callbacks
|
||||||
-export([init/1,
|
-export([init/1]).
|
||||||
handle_call/3,
|
|
||||||
handle_cast/2
|
|
||||||
]).
|
|
||||||
|
|
||||||
% API
|
% API
|
||||||
-export([start/2,
|
-export([start_link/2,
|
||||||
stop/0,
|
stop/0,
|
||||||
set_handler/1
|
set_handler/1
|
||||||
]).
|
]).
|
||||||
|
@ -38,52 +35,52 @@
|
||||||
%% API
|
%% API
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
start(Port, Path) ->
|
start_link(Port, Path) ->
|
||||||
Dispatch = cowboy_router:compile([
|
supervisor:start_link({local, ?MODULE}, ?MODULE, [Port, Path]).
|
||||||
{'_', [{Path, ?MODULE, []}]}
|
|
||||||
]),
|
|
||||||
{ok, _} = cowboy:start_clear(?MODULE,
|
|
||||||
[{port, Port}],
|
|
||||||
#{env => #{dispatch => Dispatch}}
|
|
||||||
),
|
|
||||||
{ok, _} = gen_server:start_link({local, ?MODULE}, ?MODULE, [], []),
|
|
||||||
ok.
|
|
||||||
|
|
||||||
stop() ->
|
stop() ->
|
||||||
gen_server:stop(?MODULE),
|
gen_server:stop(?MODULE).
|
||||||
cowboy:stop_listener(?MODULE).
|
|
||||||
|
|
||||||
set_handler(F) when is_function(F, 2) ->
|
set_handler(F) when is_function(F, 2) ->
|
||||||
gen_server:call(?MODULE, {set_handler, F}).
|
true = ets:insert(?MODULE, {handler, F}),
|
||||||
|
ok.
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% gen_server API
|
%% supervisor API
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
init([]) ->
|
init([Port, Path]) ->
|
||||||
F = fun(Req0, State) ->
|
Dispatch = cowboy_router:compile(
|
||||||
Req = cowboy_req:reply(
|
[
|
||||||
400,
|
{'_', [{Path, ?MODULE, []}]}
|
||||||
#{<<"content-type">> => <<"text/plain">>},
|
]),
|
||||||
<<"">>,
|
TransOpts = #{socket_opts => [{port, Port}],
|
||||||
Req0),
|
connection_type => supervisor},
|
||||||
{ok, Req, State}
|
ProtoOpts = #{env => #{dispatch => Dispatch}},
|
||||||
end,
|
|
||||||
{ok, F}.
|
|
||||||
|
|
||||||
handle_cast(_, F) ->
|
Tab = ets:new(?MODULE, [set, named_table, public]),
|
||||||
{noreply, F}.
|
ets:insert(Tab, {handler, fun default_handler/2}),
|
||||||
|
|
||||||
handle_call({set_handler, F}, _From, _F) ->
|
ChildSpec = ranch:child_spec(?MODULE, ranch_tcp, TransOpts, cowboy_clear, ProtoOpts),
|
||||||
{reply, ok, F};
|
{ok, {{one_for_one, 10, 10}, [ChildSpec]}}.
|
||||||
|
|
||||||
handle_call(get_handler, _From, F) ->
|
|
||||||
{reply, F, F}.
|
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% cowboy_server API
|
%% cowboy_server API
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
init(Req, State) ->
|
init(Req, State) ->
|
||||||
Handler = gen_server:call(?MODULE, get_handler),
|
[{handler, Handler}] = ets:lookup(?MODULE, handler),
|
||||||
Handler(Req, State).
|
Handler(Req, State).
|
||||||
|
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
%% Internal functions
|
||||||
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
default_handler(Req0, State) ->
|
||||||
|
Req = cowboy_req:reply(
|
||||||
|
400,
|
||||||
|
#{<<"content-type">> => <<"text/plain">>},
|
||||||
|
<<"">>,
|
||||||
|
Req0),
|
||||||
|
{ok, Req, State}.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue