Add more tests

This commit is contained in:
Mike Cifelli 2024-05-07 10:45:05 -04:00
parent b0872a4fed
commit 31431bcf23
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
1 changed files with 59 additions and 0 deletions

View File

@ -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