X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Freverse_proxy%2Freverse_proxy_test.exs;h=c677066b3ecb2f1615b0335a7310989195e4ec7f;hb=c74018e6a7a19a40a75c343ddadc199d9990597e;hp=1ab3cc4bb60436b3e27f0f491d80606eb8576c39;hpb=514c899275a32e6ef63305f9424c50344d41b12e;p=akkoma
diff --git a/test/reverse_proxy/reverse_proxy_test.exs b/test/reverse_proxy/reverse_proxy_test.exs
index 1ab3cc4bb..c677066b3 100644
--- a/test/reverse_proxy/reverse_proxy_test.exs
+++ b/test/reverse_proxy/reverse_proxy_test.exs
@@ -1,16 +1,19 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# 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
@@ -21,7 +24,7 @@ defmodule Pleroma.ReverseProxyTest do
ClientMock
|> expect(:request, fn :get, url, _, _, _ ->
- Registry.register(Pleroma.ReverseProxy.ClientMock, url, 0)
+ Registry.register(ClientMock, url, 0)
{:ok, 200,
[
@@ -30,13 +33,13 @@ defmodule Pleroma.ReverseProxyTest do
], %{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)
@@ -81,7 +84,7 @@ defmodule Pleroma.ReverseProxyTest do
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}}
@@ -89,10 +92,10 @@ defmodule Pleroma.ReverseProxyTest do
|> 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)
)
@@ -100,7 +103,7 @@ defmodule Pleroma.ReverseProxyTest do
{:ok, "0123456789", client}
[{_, ^max}] ->
- Registry.unregister(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length)
+ Registry.unregister(ClientMock, "/stream-bytes/" <> length)
:done
end
end)
@@ -214,24 +217,24 @@ defmodule Pleroma.ReverseProxyTest do
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)
@@ -244,7 +247,7 @@ defmodule Pleroma.ReverseProxyTest do
test "header passes", %{conn: conn} do
conn =
- Plug.Conn.put_req_header(
+ Conn.put_req_header(
conn,
"accept",
"text/html"
@@ -257,7 +260,7 @@ defmodule Pleroma.ReverseProxyTest do
test "header is filtered", %{conn: conn} do
conn =
- Plug.Conn.put_req_header(
+ Conn.put_req_header(
conn,
"accept-language",
"en-US"
@@ -275,17 +278,6 @@ defmodule Pleroma.ReverseProxyTest do
end
describe "cache resp headers" do
- test "returns headers", %{conn: conn} do
- ClientMock
- |> expect(:request, fn :get, "/cache/" <> ttl, _, _, _ ->
- {:ok, 200, [{"cache-control", "public, max-age=" <> ttl}], %{}}
- end)
- |> expect(:stream_body, fn _ -> :done end)
-
- conn = ReverseProxy.call(conn, "/cache/10")
- assert {"cache-control", "public, max-age=10"} in conn.resp_headers
- end
-
test "add cache-control", %{conn: conn} do
ClientMock
|> expect(:request, fn :get, "/cache", _, _, _ ->
@@ -294,25 +286,25 @@ defmodule Pleroma.ReverseProxyTest do
|> expect(:stream_body, fn _ -> :done end)
conn = ReverseProxy.call(conn, "/cache")
- assert {"cache-control", "public"} in conn.resp_headers
+ assert {"cache-control", "public, max-age=1209600"} in conn.resp_headers
end
end
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)
@@ -341,45 +333,4 @@ defmodule Pleroma.ReverseProxyTest do
assert {"content-disposition", "attachment; filename=\"filename.jpg\""} in conn.resp_headers
end
end
-
- describe "tesla client using gun integration" do
- @describetag :integration
-
- clear_config([Pleroma.ReverseProxy.Client]) do
- Pleroma.Config.put([Pleroma.ReverseProxy.Client], Pleroma.ReverseProxy.Client.Tesla)
- end
-
- clear_config([Pleroma.Gun.API]) do
- Pleroma.Config.put([Pleroma.Gun.API], Pleroma.Gun)
- end
-
- setup 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)
- end
-
- test "common", %{conn: conn} do
- conn = ReverseProxy.call(conn, "http://httpbin.org/stream-bytes/10")
- assert byte_size(conn.resp_body) == 10
- assert conn.state == :chunked
- assert conn.status == 200
- end
-
- test "ssl", %{conn: conn} do
- conn = ReverseProxy.call(conn, "https://httpbin.org/stream-bytes/10")
- assert byte_size(conn.resp_body) == 10
- assert conn.state == :chunked
- assert conn.status == 200
- end
-
- test "follow redirects", %{conn: conn} do
- conn = ReverseProxy.call(conn, "https://httpbin.org/redirect/5")
- assert conn.state == :chunked
- assert conn.status == 200
- end
- end
end