Add router

This commit is contained in:
Maciej Szlosarczyk 2019-05-22 17:15:41 +03:00
parent e87ac6cf2e
commit cd21f70da5
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
9 changed files with 180 additions and 5 deletions

View file

@ -0,0 +1 @@
-module(certs).

View file

@ -1,9 +1,17 @@
-module(epp_xml). -module(epp_xml).
-export([find_cltrid/1, parse/1]). -export([find_cltrid/1, get_command/1, parse/1]).
-include_lib("xmerl/include/xmerl.hrl"). -include_lib("xmerl/include/xmerl.hrl").
%% Get command from an xmlElement. Otherwise return undefined.
get_command(Record) when is_record(Record, xmlElement) ->
case xmerl_xpath:string("name(/epp/command/*[1])", Record) of
{xmlObj, string, []} -> undefined;
{xmlObj, string, Command} -> Command
end;
get_command(_) -> undefined.
%% xml_erl expects a list of characters, so let's give it to it. %% xml_erl expects a list of characters, so let's give it to it.
%% Otherwise return an error tuple. %% Otherwise return an error tuple.
parse(Text) when is_list(Text) -> parse_list(Text); parse(Text) when is_list(Text) -> parse_list(Text);

View file

@ -0,0 +1,57 @@
-module(router).
-export([route_request/1, is_valid_epp_command/1]).
-define(validCommands, ["hello", "login", "logout", "check", "info", "poll",
"create", "delete", "renew", "update", "transfer"]).
%% By default, return test values. This might cause issues in production.
-define(baseSessionUrl,
case application:get_env(epp_proxy, epp_session_url) of
undefined -> "https://registry.test/epp/session/";
{ok, Value} -> Value
end).
-define(baseCommandUrl,
case application:get_env(epp_proxy, epp_command_url) of
undefined -> "https://registry.test/epp/command/";
{ok, Value} -> Value
end).
%% Save yourself some checking beforehand.
is_valid_epp_command(Command) ->
lists:member(Command, ?validCommands).
%% Base router
route_request(Command) when is_binary(Command) ->
List = binary_to_list(Command),
url_map(List);
route_request(Command) when is_list(Command) -> url_map(Command).
%% Actually route to places
url_map(Command) when is_list(Command) ->
case Command of
%% Session commands
"hello" -> string:concat(base_session_url(), Command);
"login" -> string:concat(base_session_url(), Command);
"logout" -> string:concat(base_session_url(), Command);
%% Poll commands
"check" -> string:concat(base_command_url(), Command);
"info" -> string:concat(base_command_url(), Command);
"poll" -> string:concat(base_command_url(), Command);
%% Transform commands
"create" -> string:concat(base_command_url(), Command);
"delete" -> string:concat(base_command_url(), Command);
"renew" -> string:concat(base_command_url(), Command);
"update" -> string:concat(base_command_url(), Command);
% Transfer is both poll and query
"transfer" -> string:concat(base_command_url(), Command)
% Anything else should fail.
end.
%% Just return the macros as defined
base_session_url() ->
?baseSessionUrl.
base_command_url() ->
?baseCommandUrl.

View file

@ -0,0 +1,12 @@
-module(epp_xml_SUITE).
-include_lib("common_test/include/ct.hrl").
-export([all/0]).
-export([run_eunit/1]).
all() -> [run_eunit].
%% Run Unit tests
run_eunit(_Config) ->
ok = eunit:test(epp_xml_tests).

View file

@ -15,6 +15,12 @@
</command> </command>
</epp>"). </epp>").
-define(validXMLNotEPPList,
"<user>
<name>test</name>
<email>test@test.com</email>
</user>").
%% parse %% parse
parse_not_a_list_or_binary_test() -> parse_not_a_list_or_binary_test() ->
Input = 1234, Input = 1234,
@ -33,11 +39,11 @@ parse_sample_valid_xml_binary_test() ->
parse_sample_invalid_xml_list_test() -> parse_sample_invalid_xml_list_test() ->
Input = "Some text", Input = "Some text",
{error, Error} = epp_xml:parse(Input). {error, {fatal, _Details}} = epp_xml:parse(Input).
parse_sample_invalid_xml_binary_test() -> parse_sample_invalid_xml_binary_test() ->
Input = list_to_binary("Some text"), Input = list_to_binary("Some text"),
{error, Error} = epp_xml:parse(Input). {error, {fatal, _Details}} = epp_xml:parse(Input).
%% find_cltrid %% find_cltrid
find_cltrid_empty_list_test() -> find_cltrid_empty_list_test() ->
@ -56,5 +62,19 @@ find_cltrid_binary_test() ->
Input = ?sampleCommandList, Input = ?sampleCommandList,
?assertEqual(<<"sample1trid">>, epp_xml:find_cltrid(Input)). ?assertEqual(<<"sample1trid">>, epp_xml:find_cltrid(Input)).
%% get_command %% get_command
get_command_success_test() ->
%% We require an existing xlmElement record to pass around.
{ok, XMLElement} = epp_xml:parse(?sampleCommandList),
Command = epp_xml:get_command(XMLElement),
?assertEqual("login", Command).
get_command_xml_not_epp_failure_test() ->
{ok, XMLElement} = epp_xml:parse(?validXMLNotEPPList),
Command = epp_xml:get_command(XMLElement),
?assertEqual(undefined, Command).
get_command_failure_test() ->
%% Can pass any garbage, should get back undefined.,
Command = epp_xml:get_command("Some random string"),
?assertEqual(undefined, Command).

View file

@ -0,0 +1,12 @@
-module(router_SUITE).
-include_lib("common_test/include/ct.hrl").
-export([all/0]).
-export([run_eunit/1]).
all() -> [run_eunit].
%% Run Unit tests
run_eunit(_Config) ->
ok = eunit:test(router_tests).

View file

@ -0,0 +1,56 @@
-module(router_tests).
-include_lib("eunit/include/eunit.hrl").
is_valid_epp_command_test() ->
Commands = ["hello", "login", "logout", "check", "info", "poll",
"create", "delete", "renew", "update", "transfer"],
lists:foreach(fun (N) ->
?assert(router:is_valid_epp_command(N))
end,
Commands).
%% TODO: Make less verbose and repetitive
hello_url_test() ->
?assertEqual("https://registry.test/epp/session/hello", router:route_request("hello")),
?assertEqual("https://registry.test/epp/session/hello", router:route_request(<<"hello">>)).
login_url_test() ->
?assertEqual("https://registry.test/epp/session/login", router:route_request("login")),
?assertEqual("https://registry.test/epp/session/login", router:route_request(<<"login">>)).
logout_url_test() ->
?assertEqual("https://registry.test/epp/session/logout", router:route_request("logout")),
?assertEqual("https://registry.test/epp/session/logout", router:route_request(<<"logout">>)).
check_url_test() ->
?assertEqual("https://registry.test/epp/command/check", router:route_request("check")),
?assertEqual("https://registry.test/epp/command/check", router:route_request(<<"check">>)).
info_url_test() ->
?assertEqual("https://registry.test/epp/command/info", router:route_request("info")),
?assertEqual("https://registry.test/epp/command/info", router:route_request(<<"info">>)).
poll_url_test() ->
?assertEqual("https://registry.test/epp/command/poll", router:route_request("poll")),
?assertEqual("https://registry.test/epp/command/poll", router:route_request(<<"poll">>)).
create_url_test() ->
?assertEqual("https://registry.test/epp/command/create", router:route_request("create")),
?assertEqual("https://registry.test/epp/command/create", router:route_request(<<"create">>)).
delete_url_test() ->
?assertEqual("https://registry.test/epp/command/delete", router:route_request("delete")),
?assertEqual("https://registry.test/epp/command/delete", router:route_request(<<"delete">>)).
renew_url_test() ->
?assertEqual("https://registry.test/epp/command/renew", router:route_request("renew")),
?assertEqual("https://registry.test/epp/command/renew", router:route_request(<<"renew">>)).
update_url_test() ->
?assertEqual("https://registry.test/epp/command/update", router:route_request("update")),
?assertEqual("https://registry.test/epp/command/update", router:route_request(<<"update">>)).
transfer_url_test() ->
?assertEqual("https://registry.test/epp/command/transfer", router:route_request("transfer")),
?assertEqual("https://registry.test/epp/command/transfer", router:route_request(<<"transfer">>)).

View file

@ -1,3 +1,6 @@
[ [
{epp_proxy, []} {epp_proxy, [{tcp_port, 3333},
{tls_port, 4444},
{epp_session_url, "https://registry.test/epp/session/"},
{epp_command_url, "https://registry.test/epp/command/"}]}
]. ].

6
config/test.config Normal file
View file

@ -0,0 +1,6 @@
[
{epp_proxy, [{tcp_port, 1133},
{tls_port, 4444},
{epp_session_url, "https://registry.test/epp/session/"},
{epp_command_url, "https://registry.test/epp/command/"}]}
].