1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ReverseProxyTest do
6 use Pleroma.Web.ConnCase, async: true
7 import ExUnit.CaptureLog
9 alias Pleroma.ReverseProxy
10 alias Pleroma.ReverseProxy.ClientMock
13 {:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.ReverseProxy.ClientMock)
17 setup :verify_on_exit!
19 defp user_agent_mock(user_agent, invokes) do
20 json = Jason.encode!(%{"user-agent": user_agent})
23 |> expect(:request, fn :get, url, _, _, _ ->
24 Registry.register(Pleroma.ReverseProxy.ClientMock, url, 0)
28 {"content-type", "application/json"},
29 {"content-length", byte_size(json) |> to_string()}
32 |> expect(:stream_body, invokes, fn %{url: url} ->
33 case Registry.lookup(Pleroma.ReverseProxy.ClientMock, url) do
35 Registry.update_value(Pleroma.ReverseProxy.ClientMock, url, &(&1 + 1))
39 Registry.unregister(Pleroma.ReverseProxy.ClientMock, url)
45 describe "user-agent" do
46 test "don't keep", %{conn: conn} do
47 user_agent_mock("hackney/1.15.1", 2)
48 conn = ReverseProxy.call(conn, "/user-agent")
49 assert json_response(conn, 200) == %{"user-agent" => "hackney/1.15.1"}
52 test "keep", %{conn: conn} do
53 user_agent_mock(Pleroma.Application.user_agent(), 2)
54 conn = ReverseProxy.call(conn, "/user-agent-keep", keep_user_agent: true)
55 assert json_response(conn, 200) == %{"user-agent" => Pleroma.Application.user_agent()}
59 test "closed connection", %{conn: conn} do
61 |> expect(:request, fn :get, "/closed", _, _, _ -> {:ok, 200, [], %{}} end)
62 |> expect(:stream_body, fn _ -> {:error, :closed} end)
63 |> expect(:close, fn _ -> :ok end)
65 conn = ReverseProxy.call(conn, "/closed")
69 describe "max_body " do
70 test "length returns error if content-length more than option", %{conn: conn} do
71 user_agent_mock("hackney/1.15.1", 0)
73 assert capture_log(fn ->
74 ReverseProxy.call(conn, "/user-agent", max_body_length: 4)
76 "[error] Elixir.Pleroma.ReverseProxy: request to \"/user-agent\" failed: :body_too_large"
79 defp stream_mock(invokes, with_close? \\ false) do
81 |> expect(:request, fn :get, "/stream-bytes/" <> length, _, _, _ ->
82 Registry.register(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length, 0)
84 {:ok, 200, [{"content-type", "application/octet-stream"}],
85 %{url: "/stream-bytes/" <> length}}
87 |> expect(:stream_body, invokes, fn %{url: "/stream-bytes/" <> length} ->
88 max = String.to_integer(length)
90 case Registry.lookup(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length) do
91 [{_, current}] when current < max ->
92 Registry.update_value(
93 Pleroma.ReverseProxy.ClientMock,
94 "/stream-bytes/" <> length,
101 Registry.unregister(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length)
107 expect(ClientMock, :close, fn _ -> :ok end)
111 test "max_body_size returns error if streaming body more than that option", %{conn: conn} do
114 assert capture_log(fn ->
115 ReverseProxy.call(conn, "/stream-bytes/50", max_body_size: 30)
117 "[warn] Elixir.Pleroma.ReverseProxy request to /stream-bytes/50 failed while reading/chunking: :body_too_large"
121 describe "HEAD requests" do
122 test "common", %{conn: conn} do
124 |> expect(:request, fn :head, "/head", _, _, _ ->
125 {:ok, 200, [{"content-type", "text/html; charset=utf-8"}]}
128 conn = ReverseProxy.call(Map.put(conn, :method, "HEAD"), "/head")
129 assert html_response(conn, 200) == ""
133 defp error_mock(status) when is_integer(status) do
135 |> expect(:request, fn :get, "/status/" <> _, _, _, _ ->
140 describe "returns error on" do
141 test "500", %{conn: conn} do
144 capture_log(fn -> ReverseProxy.call(conn, "/status/500") end) =~
145 "[error] Elixir.Pleroma.ReverseProxy: request to /status/500 failed with HTTP status 500"
148 test "400", %{conn: conn} do
151 capture_log(fn -> ReverseProxy.call(conn, "/status/400") end) =~
152 "[error] Elixir.Pleroma.ReverseProxy: request to /status/400 failed with HTTP status 400"
155 test "204", %{conn: conn} do
157 |> expect(:request, fn :get, "/status/204", _, _, _ -> {:ok, 204, [], %{}} end)
160 conn = ReverseProxy.call(conn, "/status/204")
161 assert conn.resp_body == "Request failed: No Content"
164 "[error] Elixir.Pleroma.ReverseProxy: request to \"/status/204\" failed with HTTP status 204"
168 test "streaming", %{conn: conn} do
170 conn = ReverseProxy.call(conn, "/stream-bytes/200")
171 assert conn.state == :chunked
172 assert byte_size(conn.resp_body) == 200
173 assert Plug.Conn.get_resp_header(conn, "content-type") == ["application/octet-stream"]
176 defp headers_mock(_) do
178 |> expect(:request, fn :get, "/headers", headers, _, _ ->
179 Registry.register(Pleroma.ReverseProxy.ClientMock, "/headers", 0)
180 {:ok, 200, [{"content-type", "application/json"}], %{url: "/headers", headers: headers}}
182 |> expect(:stream_body, 2, fn %{url: url, headers: headers} ->
183 case Registry.lookup(Pleroma.ReverseProxy.ClientMock, url) do
185 Registry.update_value(Pleroma.ReverseProxy.ClientMock, url, &(&1 + 1))
186 headers = for {k, v} <- headers, into: %{}, do: {String.capitalize(k), v}
187 {:ok, Jason.encode!(%{headers: headers})}
190 Registry.unregister(Pleroma.ReverseProxy.ClientMock, url)
198 describe "keep request headers" do
199 setup [:headers_mock]
201 test "header passes", %{conn: conn} do
203 Plug.Conn.put_req_header(
208 |> ReverseProxy.call("/headers")
210 %{"headers" => headers} = json_response(conn, 200)
211 assert headers["Accept"] == "text/html"
214 test "header is filtered", %{conn: conn} do
216 Plug.Conn.put_req_header(
221 |> ReverseProxy.call("/headers")
223 %{"headers" => headers} = json_response(conn, 200)
224 refute headers["Accept-Language"]
228 test "returns 400 on non GET, HEAD requests", %{conn: conn} do
229 conn = ReverseProxy.call(Map.put(conn, :method, "POST"), "/ip")
230 assert conn.status == 400
233 describe "cache resp headers" do
234 test "returns headers", %{conn: conn} do
236 |> expect(:request, fn :get, "/cache/" <> ttl, _, _, _ ->
237 {:ok, 200, [{"cache-control", "public, max-age=" <> ttl}], %{}}
239 |> expect(:stream_body, fn _ -> :done end)
241 conn = ReverseProxy.call(conn, "/cache/10")
242 assert {"cache-control", "public, max-age=10"} in conn.resp_headers
245 test "add cache-control", %{conn: conn} do
247 |> expect(:request, fn :get, "/cache", _, _, _ ->
248 {:ok, 200, [{"ETag", "some ETag"}], %{}}
250 |> expect(:stream_body, fn _ -> :done end)
252 conn = ReverseProxy.call(conn, "/cache")
253 assert {"cache-control", "public"} in conn.resp_headers
257 defp disposition_headers_mock(headers) do
259 |> expect(:request, fn :get, "/disposition", _, _, _ ->
260 Registry.register(Pleroma.ReverseProxy.ClientMock, "/disposition", 0)
262 {:ok, 200, headers, %{url: "/disposition"}}
264 |> expect(:stream_body, 2, fn %{url: "/disposition"} ->
265 case Registry.lookup(Pleroma.ReverseProxy.ClientMock, "/disposition") do
267 Registry.update_value(Pleroma.ReverseProxy.ClientMock, "/disposition", &(&1 + 1))
271 Registry.unregister(Pleroma.ReverseProxy.ClientMock, "/disposition")
277 describe "response content disposition header" do
278 test "not atachment", %{conn: conn} do
279 disposition_headers_mock([
280 {"content-type", "image/gif"},
281 {"content-length", 0}
284 conn = ReverseProxy.call(conn, "/disposition")
286 assert {"content-type", "image/gif"} in conn.resp_headers
289 test "with content-disposition header", %{conn: conn} do
290 disposition_headers_mock([
291 {"content-disposition", "attachment; filename=\"filename.jpg\""},
292 {"content-length", 0}
295 conn = ReverseProxy.call(conn, "/disposition")
297 assert {"content-disposition", "attachment; filename=\"filename.jpg\""} in conn.resp_headers