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