80 lines
2.6 KiB
Elixir
80 lines
2.6 KiB
Elixir
defmodule Chronoscope.Gemini.ClientTest do
|
|
use Chronoscope.Case, async: true
|
|
|
|
alias Chronoscope.Certificate
|
|
alias Chronoscope.DateTimeMock
|
|
alias Chronoscope.Gemini.Client
|
|
alias Chronoscope.SSLMock
|
|
|
|
import Mox
|
|
|
|
setup :verify_on_exit!
|
|
|
|
setup _tags do
|
|
DateTimeMock
|
|
|> stub(:utc_now, fn -> ~U[2024-04-21 01:23:45Z] end)
|
|
|
|
:ok
|
|
end
|
|
|
|
describe "Chronoscope.Gemini.Client.init()" do
|
|
test "initializes successfully" do
|
|
assert Client.init(%{host: "localhost", port: 4444, path: "/"}) ==
|
|
{:ok,
|
|
%{
|
|
resource: %{host: "localhost", port: 4444, path: "/"},
|
|
response: {:error, "initializing"},
|
|
last_request: ~U[2024-04-21 01:23:15Z]
|
|
}}
|
|
end
|
|
end
|
|
|
|
describe "Chronoscope.Gemini.Client.handle_call()" do
|
|
test ":terminate" do
|
|
assert Client.handle_call(:terminate, nil, %{state: true}) == {:stop, :normal, self(), %{state: true}}
|
|
end
|
|
|
|
test ":list" do
|
|
assert Client.handle_call(:list, nil, %{state: true}) == {:reply, %{state: true}, %{state: true}}
|
|
end
|
|
|
|
test ":connect - cached" do
|
|
assert Client.handle_call(:connect, nil, %{
|
|
resource: %{host: "localhost", port: 4444, path: "/"},
|
|
response: {:error, "initializing"},
|
|
last_request: ~U[2024-04-21 01:23:45Z]
|
|
}) ==
|
|
{:reply, {:error, "initializing"},
|
|
%{
|
|
resource: %{host: "localhost", port: 4444, path: "/"},
|
|
response: {:error, "initializing"},
|
|
last_request: ~U[2024-04-21 01:23:45Z]
|
|
}}
|
|
end
|
|
|
|
test ":connect - not cached" do
|
|
response = "20 text/gemini\r\nHello!" |> to_charlist()
|
|
peercert = peercert()
|
|
peercert_expiration = Certificate.expiration_date(peercert)
|
|
|
|
SSLMock
|
|
|> expect(:connect, fn ~c"localhost", 4444, _, _ -> {:ok, :socket} end)
|
|
|> expect(:send, fn :socket, _ -> send_ssl_response(response) end)
|
|
|> expect(:peercert, fn :socket -> {:ok, peercert} end)
|
|
|> expect(:close, fn :socket -> :ok end)
|
|
|
|
assert {:reply, {:ok, %{cert_expiration: ^peercert_expiration}},
|
|
%{
|
|
resource: %{host: "localhost", port: 4444, path: "/"},
|
|
response: {:ok, %{cert_expiration: ^peercert_expiration}},
|
|
last_request: ~U[2024-04-21 01:23:45Z]
|
|
}} =
|
|
Client.handle_call(:connect, nil, %{
|
|
resource: %{host: "localhost", port: 4444, path: "/"},
|
|
response: {:error, "initializing"},
|
|
last_request: ~U[2024-04-21 01:23:00Z]
|
|
})
|
|
end
|
|
end
|
|
end
|