Merge branch 'develop' into gun
[akkoma] / test / http / adapter_helper / gun_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP.AdapterHelper.GunTest do
6 use ExUnit.Case, async: true
7 use Pleroma.Tests.Helpers
8
9 import ExUnit.CaptureLog
10 import Mox
11
12 alias Pleroma.Config
13 alias Pleroma.Gun.Conn
14 alias Pleroma.HTTP.AdapterHelper.Gun
15 alias Pleroma.Pool.Connections
16
17 setup :verify_on_exit!
18
19 defp gun_mock(_) do
20 gun_mock()
21 :ok
22 end
23
24 defp gun_mock do
25 Pleroma.GunMock
26 |> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(1000) end) end)
27 |> stub(:await_up, fn _, _ -> {:ok, :http} end)
28 |> stub(:set_owner, fn _, _ -> :ok end)
29 end
30
31 describe "options/1" do
32 clear_config([:http, :adapter]) do
33 Config.put([:http, :adapter], a: 1, b: 2)
34 end
35
36 test "https url with default port" do
37 uri = URI.parse("https://example.com")
38
39 opts = Gun.options([receive_conn: false], uri)
40 assert opts[:certificates_verification]
41 refute opts[:tls_opts] == []
42
43 assert opts[:tls_opts][:verify_fun] ==
44 {&:ssl_verify_hostname.verify_fun/3, [check_hostname: 'example.com']}
45
46 assert File.exists?(opts[:tls_opts][:cacertfile])
47 end
48
49 test "https ipv4 with default port" do
50 uri = URI.parse("https://127.0.0.1")
51
52 opts = Gun.options([receive_conn: false], uri)
53
54 assert opts[:tls_opts][:verify_fun] ==
55 {&:ssl_verify_hostname.verify_fun/3, [check_hostname: '127.0.0.1']}
56 end
57
58 test "https ipv6 with default port" do
59 uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]")
60
61 opts = Gun.options([receive_conn: false], uri)
62
63 assert opts[:tls_opts][:verify_fun] ==
64 {&:ssl_verify_hostname.verify_fun/3,
65 [check_hostname: '2a03:2880:f10c:83:face:b00c:0:25de']}
66 end
67
68 test "https url with non standart port" do
69 uri = URI.parse("https://example.com:115")
70
71 opts = Gun.options([receive_conn: false], uri)
72
73 assert opts[:certificates_verification]
74 assert opts[:transport] == :tls
75 end
76
77 test "get conn on next request" do
78 gun_mock()
79 level = Application.get_env(:logger, :level)
80 Logger.configure(level: :debug)
81 on_exit(fn -> Logger.configure(level: level) end)
82 uri = URI.parse("http://some-domain2.com")
83
84 assert capture_log(fn ->
85 opts = Gun.options(uri)
86
87 assert opts[:conn] == nil
88 assert opts[:close_conn] == nil
89 end) =~
90 "Gun connections pool checkin was not successful. Trying to open conn for next request."
91
92 opts = Gun.options(uri)
93
94 assert is_pid(opts[:conn])
95 assert opts[:close_conn] == false
96 end
97
98 test "merges with defaul http adapter config" do
99 defaults = Gun.options([receive_conn: false], URI.parse("https://example.com"))
100 assert Keyword.has_key?(defaults, :a)
101 assert Keyword.has_key?(defaults, :b)
102 end
103
104 test "default ssl adapter opts with connection" do
105 gun_mock()
106 uri = URI.parse("https://some-domain.com")
107
108 :ok = Conn.open(uri, :gun_connections)
109
110 opts = Gun.options(uri)
111
112 assert opts[:certificates_verification]
113 refute opts[:tls_opts] == []
114
115 assert opts[:close_conn] == false
116 assert is_pid(opts[:conn])
117 end
118
119 test "parses string proxy host & port" do
120 proxy = Config.get([:http, :proxy_url])
121 Config.put([:http, :proxy_url], "localhost:8123")
122 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
123
124 uri = URI.parse("https://some-domain.com")
125 opts = Gun.options([receive_conn: false], uri)
126 assert opts[:proxy] == {'localhost', 8123}
127 end
128
129 test "parses tuple proxy scheme host and port" do
130 proxy = Config.get([:http, :proxy_url])
131 Config.put([:http, :proxy_url], {:socks, 'localhost', 1234})
132 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
133
134 uri = URI.parse("https://some-domain.com")
135 opts = Gun.options([receive_conn: false], uri)
136 assert opts[:proxy] == {:socks, 'localhost', 1234}
137 end
138
139 test "passed opts have more weight than defaults" do
140 proxy = Config.get([:http, :proxy_url])
141 Config.put([:http, :proxy_url], {:socks5, 'localhost', 1234})
142 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
143 uri = URI.parse("https://some-domain.com")
144 opts = Gun.options([receive_conn: false, proxy: {'example.com', 4321}], uri)
145
146 assert opts[:proxy] == {'example.com', 4321}
147 end
148 end
149
150 describe "options/1 with receive_conn parameter" do
151 setup :gun_mock
152
153 test "receive conn by default" do
154 uri = URI.parse("http://another-domain.com")
155 :ok = Conn.open(uri, :gun_connections)
156
157 received_opts = Gun.options(uri)
158 assert received_opts[:close_conn] == false
159 assert is_pid(received_opts[:conn])
160 end
161
162 test "don't receive conn if receive_conn is false" do
163 uri = URI.parse("http://another-domain.com")
164 :ok = Conn.open(uri, :gun_connections)
165
166 opts = [receive_conn: false]
167 received_opts = Gun.options(opts, uri)
168 assert received_opts[:close_conn] == nil
169 assert received_opts[:conn] == nil
170 end
171 end
172
173 describe "after_request/1" do
174 setup :gun_mock
175
176 test "body_as not chunks" do
177 uri = URI.parse("http://some-domain.com")
178 :ok = Conn.open(uri, :gun_connections)
179 opts = Gun.options(uri)
180 :ok = Gun.after_request(opts)
181 conn = opts[:conn]
182
183 assert %Connections{
184 conns: %{
185 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
186 conn: ^conn,
187 conn_state: :idle,
188 used_by: []
189 }
190 }
191 } = Connections.get_state(:gun_connections)
192 end
193
194 test "body_as chunks" do
195 uri = URI.parse("http://some-domain.com")
196 :ok = Conn.open(uri, :gun_connections)
197 opts = Gun.options([body_as: :chunks], uri)
198 :ok = Gun.after_request(opts)
199 conn = opts[:conn]
200 self = self()
201
202 assert %Connections{
203 conns: %{
204 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
205 conn: ^conn,
206 conn_state: :active,
207 used_by: [{^self, _}]
208 }
209 }
210 } = Connections.get_state(:gun_connections)
211 end
212
213 test "with no connection" do
214 uri = URI.parse("http://uniq-domain.com")
215
216 :ok = Conn.open(uri, :gun_connections)
217
218 opts = Gun.options([body_as: :chunks], uri)
219 conn = opts[:conn]
220 opts = Keyword.delete(opts, :conn)
221 self = self()
222
223 :ok = Gun.after_request(opts)
224
225 assert %Connections{
226 conns: %{
227 "http:uniq-domain.com:80" => %Pleroma.Gun.Conn{
228 conn: ^conn,
229 conn_state: :active,
230 used_by: [{^self, _}]
231 }
232 }
233 } = Connections.get_state(:gun_connections)
234 end
235
236 test "with ipv4" do
237 uri = URI.parse("http://127.0.0.1")
238 :ok = Conn.open(uri, :gun_connections)
239 opts = Gun.options(uri)
240 :ok = Gun.after_request(opts)
241 conn = opts[:conn]
242
243 assert %Connections{
244 conns: %{
245 "http:127.0.0.1:80" => %Pleroma.Gun.Conn{
246 conn: ^conn,
247 conn_state: :idle,
248 used_by: []
249 }
250 }
251 } = Connections.get_state(:gun_connections)
252 end
253
254 test "with ipv6" do
255 uri = URI.parse("http://[2a03:2880:f10c:83:face:b00c:0:25de]")
256 :ok = Conn.open(uri, :gun_connections)
257 opts = Gun.options(uri)
258 :ok = Gun.after_request(opts)
259 conn = opts[:conn]
260
261 assert %Connections{
262 conns: %{
263 "http:2a03:2880:f10c:83:face:b00c:0:25de:80" => %Pleroma.Gun.Conn{
264 conn: ^conn,
265 conn_state: :idle,
266 used_by: []
267 }
268 }
269 } = Connections.get_state(:gun_connections)
270 end
271 end
272
273 describe "format_host/1" do
274 test "with domain" do
275 assert Gun.format_host("example.com") == 'example.com'
276 end
277
278 test "with idna domain" do
279 assert Gun.format_host("ですexample.com") == 'xn--example-183fne.com'
280 end
281
282 test "with ipv4" do
283 assert Gun.format_host("127.0.0.1") == '127.0.0.1'
284 end
285
286 test "with ipv6" do
287 assert Gun.format_host("2a03:2880:f10c:83:face:b00c:0:25de") ==
288 '2a03:2880:f10c:83:face:b00c:0:25de'
289 end
290 end
291 end