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