Переделать связь нод в кластере на автоматическое обнаружение #9
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
-module(api_websocket_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
-define(WS_URL, "ws://localhost:8081/ws").
|
||||
-define(ADMIN_WS_URL, "ws://localhost:8446/admin/ws").
|
||||
-define(BASE_URL, api_test_runner:get_base_url()).
|
||||
-define(WS_URL, api_test_runner:get_base_ws_url() ++ "/ws").
|
||||
-define(ADMIN_WS_URL, api_test_runner:get_admin_ws_url() ++ "/admin/ws").
|
||||
|
||||
test() ->
|
||||
ct:pal("Testing WebSocket API..."),
|
||||
@@ -141,14 +141,24 @@ test_ws_connect_debug(Url, Token) ->
|
||||
"/ws?token=" ++ binary_to_list(Token)
|
||||
end,
|
||||
|
||||
Port = ws_port(Url),
|
||||
Host = "localhost",
|
||||
{ok, Port} = extract_port(Url),
|
||||
{ok, Host} = extract_host(Url),
|
||||
|
||||
Opts = case Port of
|
||||
443 ->
|
||||
#{
|
||||
protocols => [http],
|
||||
transport => tls,
|
||||
tls_opts => [{verify, verify_none}]
|
||||
};
|
||||
_ -> #{ protocols => [http] }
|
||||
end,
|
||||
|
||||
ct:pal(" Host: ~s", [Host]),
|
||||
ct:pal(" Port: ~p", [Port]),
|
||||
ct:pal(" Path: ~s", [Path]),
|
||||
|
||||
{ok, ConnPid} = gun:open(Host, Port, #{protocols => [http]}),
|
||||
{ok, ConnPid} = gun:open(Host, Port, Opts),
|
||||
{ok, http} = gun:await_up(ConnPid, 5000),
|
||||
|
||||
Headers = [
|
||||
@@ -234,5 +244,40 @@ test_ws_recv(ConnPid, Timeout) ->
|
||||
test_ws_close(ConnPid) ->
|
||||
gun:close(ConnPid).
|
||||
|
||||
ws_port("ws://localhost:8081" ++ _) -> 8081;
|
||||
ws_port("ws://localhost:8446" ++ _) -> 8446.
|
||||
%% ========== URL parsing helpers ==========
|
||||
|
||||
normalize_scheme(S) when is_binary(S) -> S;
|
||||
normalize_scheme(S) when is_list(S) -> list_to_binary(S);
|
||||
normalize_scheme(S) when is_atom(S) -> atom_to_binary(S, utf8);
|
||||
normalize_scheme(_) -> <<"unknown">>.
|
||||
|
||||
extract_host(Url) ->
|
||||
try
|
||||
Parsed = uri_string:parse(Url),
|
||||
#{scheme := SchemeRaw, host := Host} = Parsed,
|
||||
Scheme = normalize_scheme(SchemeRaw),
|
||||
if Scheme =:= <<"ws">>; Scheme =:= <<"wss">> -> ok;
|
||||
true -> throw({invalid_scheme, SchemeRaw})
|
||||
end,
|
||||
HostStr = if is_binary(Host) -> binary_to_list(Host); true -> Host end,
|
||||
{ok, HostStr}
|
||||
catch
|
||||
Class:Reason:Stacktrace ->
|
||||
{error, {parse_error, {Class, Reason}, Stacktrace}}
|
||||
end.
|
||||
|
||||
extract_port(Url) ->
|
||||
try
|
||||
Parsed = uri_string:parse(Url),
|
||||
#{scheme := SchemeRaw} = Parsed,
|
||||
Scheme = normalize_scheme(SchemeRaw),
|
||||
DefaultPort = if Scheme =:= <<"ws">> -> 80; Scheme =:= <<"wss">> -> 443; true -> throw({invalid_scheme, SchemeRaw}) end,
|
||||
case maps:find(port, Parsed) of
|
||||
{ok, P} when is_integer(P) -> {ok, P};
|
||||
{ok, P} -> {ok, try list_to_integer(binary_to_list(normalize_scheme(P))) catch _:_ -> DefaultPort end};
|
||||
error -> {ok, DefaultPort}
|
||||
end
|
||||
catch
|
||||
Class:Reason:Stacktrace ->
|
||||
{error, {parse_error, {Class, Reason}, Stacktrace}}
|
||||
end.
|
||||
Reference in New Issue
Block a user