use Pleroma.Tests.Helpers
import ExUnit.CaptureLog
- import Mox
alias Pleroma.Config
alias Pleroma.HTTP.Connection
- setup :verify_on_exit!
-
describe "parse_host/1" do
test "as atom to charlist" do
assert Connection.parse_host(:localhost) == 'localhost'
assert opts[:proxy] == {'example.com', 4321}
end
-
- test "default ssl adapter opts with connection" do
- adapter = Application.get_env(:tesla, :adapter)
- Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun)
- on_exit(fn -> Application.put_env(:tesla, :adapter, adapter) end)
-
- uri = URI.parse("https://some-domain.com")
-
- Pleroma.GunMock
- |> expect(:open, fn 'some-domain.com', 443, _ ->
- Task.start_link(fn -> Process.sleep(1000) end)
- end)
- |> expect(:await_up, fn _, _ -> {:ok, :http2} end)
- |> expect(:set_owner, fn _, _ -> :ok end)
-
- :ok = Pleroma.Gun.Conn.open(uri, :gun_connections)
-
- opts = Connection.options(uri)
-
- assert opts[:certificates_verification]
- refute opts[:tls_opts] == []
-
- assert opts[:close_conn] == false
- assert is_pid(opts[:conn])
- end
end
end
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ReverseProxyTest do
- use Pleroma.Web.ConnCase
+ use Pleroma.Web.ConnCase, async: true
+
import ExUnit.CaptureLog
import Mox
+
alias Pleroma.ReverseProxy
alias Pleroma.ReverseProxy.ClientMock
+ alias Plug.Conn
setup_all do
- {:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.ReverseProxy.ClientMock)
+ {:ok, _} = Registry.start_link(keys: :unique, name: ClientMock)
:ok
end
ClientMock
|> expect(:request, fn :get, url, _, _, _ ->
- Registry.register(Pleroma.ReverseProxy.ClientMock, url, 0)
+ Registry.register(ClientMock, url, 0)
{:ok, 200,
[
], %{url: url}}
end)
|> expect(:stream_body, invokes, fn %{url: url} = client ->
- case Registry.lookup(Pleroma.ReverseProxy.ClientMock, url) do
+ case Registry.lookup(ClientMock, url) do
[{_, 0}] ->
- Registry.update_value(Pleroma.ReverseProxy.ClientMock, url, &(&1 + 1))
+ Registry.update_value(ClientMock, url, &(&1 + 1))
{:ok, json, client}
[{_, 1}] ->
- Registry.unregister(Pleroma.ReverseProxy.ClientMock, url)
+ Registry.unregister(ClientMock, url)
:done
end
end)
defp stream_mock(invokes, with_close? \\ false) do
ClientMock
|> expect(:request, fn :get, "/stream-bytes/" <> length, _, _, _ ->
- Registry.register(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length, 0)
+ Registry.register(ClientMock, "/stream-bytes/" <> length, 0)
{:ok, 200, [{"content-type", "application/octet-stream"}],
%{url: "/stream-bytes/" <> length}}
|> expect(:stream_body, invokes, fn %{url: "/stream-bytes/" <> length} = client ->
max = String.to_integer(length)
- case Registry.lookup(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length) do
+ case Registry.lookup(ClientMock, "/stream-bytes/" <> length) do
[{_, current}] when current < max ->
Registry.update_value(
- Pleroma.ReverseProxy.ClientMock,
+ ClientMock,
"/stream-bytes/" <> length,
&(&1 + 10)
)
{:ok, "0123456789", client}
[{_, ^max}] ->
- Registry.unregister(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length)
+ Registry.unregister(ClientMock, "/stream-bytes/" <> length)
:done
end
end)
conn = ReverseProxy.call(conn, "/stream-bytes/200")
assert conn.state == :chunked
assert byte_size(conn.resp_body) == 200
- assert Plug.Conn.get_resp_header(conn, "content-type") == ["application/octet-stream"]
+ assert Conn.get_resp_header(conn, "content-type") == ["application/octet-stream"]
end
defp headers_mock(_) do
ClientMock
|> expect(:request, fn :get, "/headers", headers, _, _ ->
- Registry.register(Pleroma.ReverseProxy.ClientMock, "/headers", 0)
+ Registry.register(ClientMock, "/headers", 0)
{:ok, 200, [{"content-type", "application/json"}], %{url: "/headers", headers: headers}}
end)
|> expect(:stream_body, 2, fn %{url: url, headers: headers} = client ->
- case Registry.lookup(Pleroma.ReverseProxy.ClientMock, url) do
+ case Registry.lookup(ClientMock, url) do
[{_, 0}] ->
- Registry.update_value(Pleroma.ReverseProxy.ClientMock, url, &(&1 + 1))
+ Registry.update_value(ClientMock, url, &(&1 + 1))
headers = for {k, v} <- headers, into: %{}, do: {String.capitalize(k), v}
{:ok, Jason.encode!(%{headers: headers}), client}
[{_, 1}] ->
- Registry.unregister(Pleroma.ReverseProxy.ClientMock, url)
+ Registry.unregister(ClientMock, url)
:done
end
end)
test "header passes", %{conn: conn} do
conn =
- Plug.Conn.put_req_header(
+ Conn.put_req_header(
conn,
"accept",
"text/html"
test "header is filtered", %{conn: conn} do
conn =
- Plug.Conn.put_req_header(
+ Conn.put_req_header(
conn,
"accept-language",
"en-US"
defp disposition_headers_mock(headers) do
ClientMock
|> expect(:request, fn :get, "/disposition", _, _, _ ->
- Registry.register(Pleroma.ReverseProxy.ClientMock, "/disposition", 0)
+ Registry.register(ClientMock, "/disposition", 0)
{:ok, 200, headers, %{url: "/disposition"}}
end)
|> expect(:stream_body, 2, fn %{url: "/disposition"} = client ->
- case Registry.lookup(Pleroma.ReverseProxy.ClientMock, "/disposition") do
+ case Registry.lookup(ClientMock, "/disposition") do
[{_, 0}] ->
- Registry.update_value(Pleroma.ReverseProxy.ClientMock, "/disposition", &(&1 + 1))
+ Registry.update_value(ClientMock, "/disposition", &(&1 + 1))
{:ok, "", client}
[{_, 1}] ->
- Registry.unregister(Pleroma.ReverseProxy.ClientMock, "/disposition")
+ Registry.unregister(ClientMock, "/disposition")
:done
end
end)