Add license/copyright to all project files
[akkoma] / test / reverse_proxy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ReverseProxyTest do
6 use Pleroma.Web.ConnCase, async: true
7 import ExUnit.CaptureLog
8 import ExUnit.CaptureLog
9 import Mox
10 alias Pleroma.ReverseProxy
11 alias Pleroma.ReverseProxy.ClientMock
12
13 setup_all do
14 {:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.ReverseProxy.ClientMock)
15 :ok
16 end
17
18 setup :verify_on_exit!
19
20 defp user_agent_mock(user_agent, invokes) do
21 json = Jason.encode!(%{"user-agent": user_agent})
22
23 ClientMock
24 |> expect(:request, fn :get, url, _, _, _ ->
25 Registry.register(Pleroma.ReverseProxy.ClientMock, url, 0)
26
27 {:ok, 200,
28 [
29 {"content-type", "application/json"},
30 {"content-length", byte_size(json) |> to_string()}
31 ], %{url: url}}
32 end)
33 |> expect(:stream_body, invokes, fn %{url: url} ->
34 case Registry.lookup(Pleroma.ReverseProxy.ClientMock, url) do
35 [{_, 0}] ->
36 Registry.update_value(Pleroma.ReverseProxy.ClientMock, url, &(&1 + 1))
37 {:ok, json}
38
39 [{_, 1}] ->
40 Registry.unregister(Pleroma.ReverseProxy.ClientMock, url)
41 :done
42 end
43 end)
44 end
45
46 describe "user-agent" do
47 test "don't keep", %{conn: conn} do
48 user_agent_mock("hackney/1.15.1", 2)
49 conn = ReverseProxy.call(conn, "/user-agent")
50 assert json_response(conn, 200) == %{"user-agent" => "hackney/1.15.1"}
51 end
52
53 test "keep", %{conn: conn} do
54 user_agent_mock(Pleroma.Application.user_agent(), 2)
55 conn = ReverseProxy.call(conn, "/user-agent-keep", keep_user_agent: true)
56 assert json_response(conn, 200) == %{"user-agent" => Pleroma.Application.user_agent()}
57 end
58 end
59
60 test "closed connection", %{conn: conn} do
61 ClientMock
62 |> expect(:request, fn :get, "/closed", _, _, _ -> {:ok, 200, [], %{}} end)
63 |> expect(:stream_body, fn _ -> {:error, :closed} end)
64 |> expect(:close, fn _ -> :ok end)
65
66 conn = ReverseProxy.call(conn, "/closed")
67 assert conn.halted
68 end
69
70 describe "max_body " do
71 test "length returns error if content-length more than option", %{conn: conn} do
72 user_agent_mock("hackney/1.15.1", 0)
73
74 assert capture_log(fn ->
75 ReverseProxy.call(conn, "/user-agent", max_body_length: 4)
76 end) =~
77 "[error] Elixir.Pleroma.ReverseProxy: request to \"/user-agent\" failed: :body_too_large"
78 end
79
80 defp stream_mock(invokes, with_close? \\ false) do
81 ClientMock
82 |> expect(:request, fn :get, "/stream-bytes/" <> length, _, _, _ ->
83 Registry.register(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length, 0)
84
85 {:ok, 200, [{"content-type", "application/octet-stream"}],
86 %{url: "/stream-bytes/" <> length}}
87 end)
88 |> expect(:stream_body, invokes, fn %{url: "/stream-bytes/" <> length} ->
89 max = String.to_integer(length)
90
91 case Registry.lookup(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length) do
92 [{_, current}] when current < max ->
93 Registry.update_value(
94 Pleroma.ReverseProxy.ClientMock,
95 "/stream-bytes/" <> length,
96 &(&1 + 10)
97 )
98
99 {:ok, "0123456789"}
100
101 [{_, ^max}] ->
102 Registry.unregister(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length)
103 :done
104 end
105 end)
106
107 if with_close? do
108 expect(ClientMock, :close, fn _ -> :ok end)
109 end
110 end
111
112 test "max_body_size returns error if streaming body more than that option", %{conn: conn} do
113 stream_mock(3, true)
114
115 assert capture_log(fn ->
116 ReverseProxy.call(conn, "/stream-bytes/50", max_body_size: 30)
117 end) =~
118 "[warn] Elixir.Pleroma.ReverseProxy request to /stream-bytes/50 failed while reading/chunking: :body_too_large"
119 end
120 end
121
122 describe "HEAD requests" do
123 test "common", %{conn: conn} do
124 ClientMock
125 |> expect(:request, fn :head, "/head", _, _, _ ->
126 {:ok, 200, [{"content-type", "text/html; charset=utf-8"}]}
127 end)
128
129 conn = ReverseProxy.call(Map.put(conn, :method, "HEAD"), "/head")
130 assert html_response(conn, 200) == ""
131 end
132 end
133
134 defp error_mock(status) when is_integer(status) do
135 ClientMock
136 |> expect(:request, fn :get, "/status/" <> _, _, _, _ ->
137 {:error, status}
138 end)
139 end
140
141 describe "returns error on" do
142 test "500", %{conn: conn} do
143 error_mock(500)
144
145 capture_log(fn -> ReverseProxy.call(conn, "/status/500") end) =~
146 "[error] Elixir.Pleroma.ReverseProxy: request to /status/500 failed with HTTP status 500"
147 end
148
149 test "400", %{conn: conn} do
150 error_mock(400)
151
152 capture_log(fn -> ReverseProxy.call(conn, "/status/400") end) =~
153 "[error] Elixir.Pleroma.ReverseProxy: request to /status/400 failed with HTTP status 400"
154 end
155
156 test "204", %{conn: conn} do
157 ClientMock
158 |> expect(:request, fn :get, "/status/204", _, _, _ -> {:ok, 204, [], %{}} end)
159
160 capture_log(fn ->
161 conn = ReverseProxy.call(conn, "/status/204")
162 assert conn.resp_body == "Request failed: No Content"
163 assert conn.halted
164 end) =~
165 "[error] Elixir.Pleroma.ReverseProxy: request to \"/status/204\" failed with HTTP status 204"
166 end
167 end
168
169 test "streaming", %{conn: conn} do
170 stream_mock(21)
171 conn = ReverseProxy.call(conn, "/stream-bytes/200")
172 assert conn.state == :chunked
173 assert byte_size(conn.resp_body) == 200
174 assert Plug.Conn.get_resp_header(conn, "content-type") == ["application/octet-stream"]
175 end
176
177 defp headers_mock(_) do
178 ClientMock
179 |> expect(:request, fn :get, "/headers", headers, _, _ ->
180 Registry.register(Pleroma.ReverseProxy.ClientMock, "/headers", 0)
181 {:ok, 200, [{"content-type", "application/json"}], %{url: "/headers", headers: headers}}
182 end)
183 |> expect(:stream_body, 2, fn %{url: url, headers: headers} ->
184 case Registry.lookup(Pleroma.ReverseProxy.ClientMock, url) do
185 [{_, 0}] ->
186 Registry.update_value(Pleroma.ReverseProxy.ClientMock, url, &(&1 + 1))
187 headers = for {k, v} <- headers, into: %{}, do: {String.capitalize(k), v}
188 {:ok, Jason.encode!(%{headers: headers})}
189
190 [{_, 1}] ->
191 Registry.unregister(Pleroma.ReverseProxy.ClientMock, url)
192 :done
193 end
194 end)
195
196 :ok
197 end
198
199 describe "keep request headers" do
200 setup [:headers_mock]
201
202 test "header passes", %{conn: conn} do
203 conn =
204 Plug.Conn.put_req_header(
205 conn,
206 "accept",
207 "text/html"
208 )
209 |> ReverseProxy.call("/headers")
210
211 %{"headers" => headers} = json_response(conn, 200)
212 assert headers["Accept"] == "text/html"
213 end
214
215 test "header is filtered", %{conn: conn} do
216 conn =
217 Plug.Conn.put_req_header(
218 conn,
219 "accept-language",
220 "en-US"
221 )
222 |> ReverseProxy.call("/headers")
223
224 %{"headers" => headers} = json_response(conn, 200)
225 refute headers["Accept-Language"]
226 end
227 end
228
229 test "returns 400 on non GET, HEAD requests", %{conn: conn} do
230 conn = ReverseProxy.call(Map.put(conn, :method, "POST"), "/ip")
231 assert conn.status == 400
232 end
233
234 describe "cache resp headers" do
235 test "returns headers", %{conn: conn} do
236 ClientMock
237 |> expect(:request, fn :get, "/cache/" <> ttl, _, _, _ ->
238 {:ok, 200, [{"cache-control", "public, max-age=" <> ttl}], %{}}
239 end)
240 |> expect(:stream_body, fn _ -> :done end)
241
242 conn = ReverseProxy.call(conn, "/cache/10")
243 assert {"cache-control", "public, max-age=10"} in conn.resp_headers
244 end
245
246 test "add cache-control", %{conn: conn} do
247 ClientMock
248 |> expect(:request, fn :get, "/cache", _, _, _ ->
249 {:ok, 200, [{"ETag", "some ETag"}], %{}}
250 end)
251 |> expect(:stream_body, fn _ -> :done end)
252
253 conn = ReverseProxy.call(conn, "/cache")
254 assert {"cache-control", "public"} in conn.resp_headers
255 end
256 end
257
258 defp disposition_headers_mock(headers) do
259 ClientMock
260 |> expect(:request, fn :get, "/disposition", _, _, _ ->
261 Registry.register(Pleroma.ReverseProxy.ClientMock, "/disposition", 0)
262
263 {:ok, 200, headers, %{url: "/disposition"}}
264 end)
265 |> expect(:stream_body, 2, fn %{url: "/disposition"} ->
266 case Registry.lookup(Pleroma.ReverseProxy.ClientMock, "/disposition") do
267 [{_, 0}] ->
268 Registry.update_value(Pleroma.ReverseProxy.ClientMock, "/disposition", &(&1 + 1))
269 {:ok, ""}
270
271 [{_, 1}] ->
272 Registry.unregister(Pleroma.ReverseProxy.ClientMock, "/disposition")
273 :done
274 end
275 end)
276 end
277
278 describe "response content disposition header" do
279 test "not atachment", %{conn: conn} do
280 disposition_headers_mock([
281 {"content-type", "image/gif"},
282 {"content-length", 0}
283 ])
284
285 conn = ReverseProxy.call(conn, "/disposition")
286
287 assert {"content-type", "image/gif"} in conn.resp_headers
288 end
289
290 test "with content-disposition header", %{conn: conn} do
291 disposition_headers_mock([
292 {"content-disposition", "attachment; filename=\"filename.jpg\""},
293 {"content-length", 0}
294 ])
295
296 conn = ReverseProxy.call(conn, "/disposition")
297
298 assert {"content-disposition", "attachment; filename=\"filename.jpg\""} in conn.resp_headers
299 end
300 end
301 end