Clean up code
This commit is contained in:
parent
ee863033eb
commit
2abffa1120
|
@ -7,23 +7,23 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentController do
|
||||||
|
|
||||||
@default_port 4460
|
@default_port 4460
|
||||||
|
|
||||||
def get(conn, %{"host" => _, "port" => port} = params) do
|
def get(conn, %{"host" => host, "port" => port}) do
|
||||||
try do
|
try do
|
||||||
handle_get(conn, Map.put(params, "port", String.to_integer(port)))
|
handle_get(conn, %{host: host, port: String.to_integer(port)})
|
||||||
rescue
|
rescue
|
||||||
ArgumentError -> bad_request_response(conn, "invalid port")
|
ArgumentError -> bad_request_response(conn, "invalid port")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(conn, %{"host" => _} = params) do
|
def get(conn, %{"host" => host}) do
|
||||||
handle_get(conn, Map.put(params, "port", @default_port))
|
handle_get(conn, %{host: host, port: @default_port})
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(conn, _params) do
|
def get(conn, _params) do
|
||||||
bad_request_response(conn, "missing host")
|
bad_request_response(conn, "missing host")
|
||||||
end
|
end
|
||||||
|
|
||||||
defp handle_get(conn, %{"host" => host, "port" => port}) when is_integer(port) and port > 0 and port < 65536 do
|
defp handle_get(conn, %{host: host, port: port}) when port > 0 and port < 65536 do
|
||||||
case nts_behaviour().key_establishment(host, port) do
|
case nts_behaviour().key_establishment(host, port) do
|
||||||
{:ok, response} ->
|
{:ok, response} ->
|
||||||
json(conn, %{status: :ok, response: format_response(response)})
|
json(conn, %{status: :ok, response: format_response(response)})
|
||||||
|
@ -34,7 +34,7 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentController do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp handle_get(conn, _params) do
|
defp handle_get(conn, _params) do
|
||||||
bad_request_response(conn, "invalid port")
|
bad_request_response(conn, "port out of range")
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_response(response) do
|
defp format_response(response) do
|
||||||
|
|
|
@ -32,7 +32,7 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentControllerTest do
|
||||||
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=65536")
|
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=65536")
|
||||||
|> json_response(400)
|
|> json_response(400)
|
||||||
|
|
||||||
assert %{"error" => "invalid port"} == response
|
assert %{"error" => "port out of range"} == response
|
||||||
end
|
end
|
||||||
|
|
||||||
test "handles a negative port number", %{conn: conn} do
|
test "handles a negative port number", %{conn: conn} do
|
||||||
|
@ -41,7 +41,7 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentControllerTest do
|
||||||
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=-4460")
|
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=-4460")
|
||||||
|> json_response(400)
|
|> json_response(400)
|
||||||
|
|
||||||
assert %{"error" => "invalid port"} == response
|
assert %{"error" => "port out of range"} == response
|
||||||
end
|
end
|
||||||
|
|
||||||
test "handles a bad port number", %{conn: conn} do
|
test "handles a bad port number", %{conn: conn} do
|
||||||
|
|
Loading…
Reference in New Issue