From 31431bcf236e37aa0a9c4f44b7cd7b2cdc8a5879 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Tue, 7 May 2024 10:45:05 -0400 Subject: [PATCH] Add more tests --- .../gemini/connection_client_test.exs | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 test/chronoscope/gemini/connection_client_test.exs diff --git a/test/chronoscope/gemini/connection_client_test.exs b/test/chronoscope/gemini/connection_client_test.exs new file mode 100644 index 0000000..c462204 --- /dev/null +++ b/test/chronoscope/gemini/connection_client_test.exs @@ -0,0 +1,59 @@ +defmodule Chronoscope.Gemini.ConnectionClientTest do + use Chronoscope.Case, async: true + + alias Chronoscope.Certificate + alias Chronoscope.Gemini.ConnectionClient + alias Chronoscope.Gemini.Request + alias Chronoscope.SSLMock + + import Mox + + setup :verify_on_exit! + + setup _tags do + resource = %{host: "localhost", port: 1965, path: "/"} + + {:ok, %{resource: resource, request: Request.create(resource)}} + end + + @timeout 3000 + + describe "Chronoscope.Gemini.ConnectClient.connect()" do + test "handles an empty response", %{resource: resource, request: request} do + SSLMock + |> expect(:connect, fn ~c"localhost", 1965, _tls_options, @timeout -> {:ok, :socket} end) + |> expect(:send, fn :socket, ^request -> send_ssl_response([]) end) + |> expect(:peercert, fn :socket -> {:ok, peercert()} end) + |> expect(:close, fn :socket -> :ok end) + + assert ConnectionClient.connect(resource) == {:error, "bad response: "} + end + + test "handles a good response", %{resource: resource, request: request} do + peercert = peercert() + peercert_expiration = Certificate.expiration_date(peercert) + response = "20 text/gemini\r\nHello\r\nWorld!\r\n" + + SSLMock + |> expect(:connect, fn ~c"localhost", 1965, _tls_options, @timeout -> {:ok, :socket} end) + |> expect(:send, fn :socket, ^request -> send_ssl_response(response) end) + |> expect(:peercert, fn :socket -> {:ok, peercert} end) + |> expect(:close, fn :socket -> :ok end) + + assert {:ok, %{cert_expiration: ^peercert_expiration, status_code: 20, meta: "text/gemini", body: "Hello\r\nWorld!\r\n"}} = + ConnectionClient.connect(resource) + end + + test "handles a bad response", %{resource: resource, request: request} do + response = "HTTP/1.1 400 Bad Request\r\nServer: nginx\r\n" + + SSLMock + |> expect(:connect, fn ~c"localhost", 1965, _tls_options, @timeout -> {:ok, :socket} end) + |> expect(:send, fn :socket, ^request -> send_ssl_response(response) end) + |> expect(:peercert, fn :socket -> {:ok, peercert()} end) + |> expect(:close, fn :socket -> :ok end) + + assert {:error, "bad response: HTTP/1.1 400 Bad Request\r\nServer: nginx\r\n"} = ConnectionClient.connect(resource) + end + end +end