Clean up code

This commit is contained in:
Mike Cifelli 2024-03-30 11:36:17 -04:00
parent ee863033eb
commit 2abffa1120
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
2 changed files with 8 additions and 8 deletions

View File

@ -7,23 +7,23 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentController do
@default_port 4460
def get(conn, %{"host" => _, "port" => port} = params) do
def get(conn, %{"host" => host, "port" => port}) 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
ArgumentError -> bad_request_response(conn, "invalid port")
end
end
def get(conn, %{"host" => _} = params) do
handle_get(conn, Map.put(params, "port", @default_port))
def get(conn, %{"host" => host}) do
handle_get(conn, %{host: host, port: @default_port})
end
def get(conn, _params) do
bad_request_response(conn, "missing host")
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
{:ok, response} ->
json(conn, %{status: :ok, response: format_response(response)})
@ -34,7 +34,7 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentController do
end
defp handle_get(conn, _params) do
bad_request_response(conn, "invalid port")
bad_request_response(conn, "port out of range")
end
defp format_response(response) do

View File

@ -32,7 +32,7 @@ defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentControllerTest do
|> get(~p"/api/v1/nts/key-establishment?host=localhost&port=65536")
|> json_response(400)
assert %{"error" => "invalid port"} == response
assert %{"error" => "port out of range"} == response
end
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")
|> json_response(400)
assert %{"error" => "invalid port"} == response
assert %{"error" => "port out of range"} == response
end
test "handles a bad port number", %{conn: conn} do