18025b98676acd113408cdbf2e29ccb6557b71e3
[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 Mox
10
11 alias Pleroma.Config
12 alias Pleroma.Gun.Conn
13 alias Pleroma.HTTP.AdapterHelper.Gun
14 alias Pleroma.Pool.Connections
15
16 setup :verify_on_exit!
17
18 defp gun_mock(_) do
19 gun_mock()
20 :ok
21 end
22
23 defp gun_mock do
24 Pleroma.GunMock
25 |> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(1000) end) end)
26 |> stub(:await_up, fn _, _ -> {:ok, :http} end)
27 |> stub(:set_owner, fn _, _ -> :ok end)
28 end
29
30 describe "options/1" do
31 clear_config([:http, :adapter]) do
32 Config.put([:http, :adapter], a: 1, b: 2)
33 end
34
35 test "https url with default port" do
36 uri = URI.parse("https://example.com")
37
38 opts = Gun.options([receive_conn: false], uri)
39 assert opts[:certificates_verification]
40 assert opts[:tls_opts][:log_level] == :warning
41 end
42
43 test "https ipv4 with default port" do
44 uri = URI.parse("https://127.0.0.1")
45
46 opts = Gun.options([receive_conn: false], uri)
47 assert opts[:certificates_verification]
48 assert opts[:tls_opts][:log_level] == :warning
49 end
50
51 test "https ipv6 with default port" do
52 uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]")
53
54 opts = Gun.options([receive_conn: false], uri)
55 assert opts[:certificates_verification]
56 assert opts[:tls_opts][:log_level] == :warning
57 end
58
59 test "https url with non standart port" do
60 uri = URI.parse("https://example.com:115")
61
62 opts = Gun.options([receive_conn: false], uri)
63
64 assert opts[:certificates_verification]
65 end
66
67 test "get conn on next request" do
68 gun_mock()
69 level = Application.get_env(:logger, :level)
70 Logger.configure(level: :debug)
71 on_exit(fn -> Logger.configure(level: level) end)
72 uri = URI.parse("http://some-domain2.com")
73
74 opts = Gun.options(uri)
75
76 assert opts[:conn] == nil
77 assert opts[:close_conn] == nil
78
79 Process.sleep(50)
80 opts = Gun.options(uri)
81
82 assert is_pid(opts[:conn])
83 assert opts[:close_conn] == false
84 end
85
86 test "merges with defaul http adapter config" do
87 defaults = Gun.options([receive_conn: false], URI.parse("https://example.com"))
88 assert Keyword.has_key?(defaults, :a)
89 assert Keyword.has_key?(defaults, :b)
90 end
91
92 test "default ssl adapter opts with connection" do
93 gun_mock()
94 uri = URI.parse("https://some-domain.com")
95
96 :ok = Conn.open(uri, :gun_connections)
97
98 opts = Gun.options(uri)
99
100 assert opts[:certificates_verification]
101 refute opts[:tls_opts] == []
102
103 assert opts[:close_conn] == false
104 assert is_pid(opts[:conn])
105 end
106
107 test "parses string proxy host & port" do
108 proxy = Config.get([:http, :proxy_url])
109 Config.put([:http, :proxy_url], "localhost:8123")
110 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
111
112 uri = URI.parse("https://some-domain.com")
113 opts = Gun.options([receive_conn: false], uri)
114 assert opts[:proxy] == {'localhost', 8123}
115 end
116
117 test "parses tuple proxy scheme host and port" do
118 proxy = Config.get([:http, :proxy_url])
119 Config.put([:http, :proxy_url], {:socks, 'localhost', 1234})
120 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
121
122 uri = URI.parse("https://some-domain.com")
123 opts = Gun.options([receive_conn: false], uri)
124 assert opts[:proxy] == {:socks, 'localhost', 1234}
125 end
126
127 test "passed opts have more weight than defaults" do
128 proxy = Config.get([:http, :proxy_url])
129 Config.put([:http, :proxy_url], {:socks5, 'localhost', 1234})
130 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
131 uri = URI.parse("https://some-domain.com")
132 opts = Gun.options([receive_conn: false, proxy: {'example.com', 4321}], uri)
133
134 assert opts[:proxy] == {'example.com', 4321}
135 end
136 end
137
138 describe "options/1 with receive_conn parameter" do
139 setup :gun_mock
140
141 test "receive conn by default" do
142 uri = URI.parse("http://another-domain.com")
143 :ok = Conn.open(uri, :gun_connections)
144
145 received_opts = Gun.options(uri)
146 assert received_opts[:close_conn] == false
147 assert is_pid(received_opts[:conn])
148 end
149
150 test "don't receive conn if receive_conn is false" do
151 uri = URI.parse("http://another-domain.com")
152 :ok = Conn.open(uri, :gun_connections)
153
154 opts = [receive_conn: false]
155 received_opts = Gun.options(opts, uri)
156 assert received_opts[:close_conn] == nil
157 assert received_opts[:conn] == nil
158 end
159 end
160
161 describe "after_request/1" do
162 setup :gun_mock
163
164 test "body_as not chunks" do
165 uri = URI.parse("http://some-domain.com")
166 :ok = Conn.open(uri, :gun_connections)
167 opts = Gun.options(uri)
168 :ok = Gun.after_request(opts)
169 conn = opts[:conn]
170
171 assert %Connections{
172 conns: %{
173 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
174 conn: ^conn,
175 conn_state: :idle,
176 used_by: []
177 }
178 }
179 } = Connections.get_state(:gun_connections)
180 end
181
182 test "body_as chunks" do
183 uri = URI.parse("http://some-domain.com")
184 :ok = Conn.open(uri, :gun_connections)
185 opts = Gun.options([body_as: :chunks], uri)
186 :ok = Gun.after_request(opts)
187 conn = opts[:conn]
188 self = self()
189
190 assert %Connections{
191 conns: %{
192 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
193 conn: ^conn,
194 conn_state: :active,
195 used_by: [{^self, _}]
196 }
197 }
198 } = Connections.get_state(:gun_connections)
199 end
200
201 test "with no connection" do
202 uri = URI.parse("http://uniq-domain.com")
203
204 :ok = Conn.open(uri, :gun_connections)
205
206 opts = Gun.options([body_as: :chunks], uri)
207 conn = opts[:conn]
208 opts = Keyword.delete(opts, :conn)
209 self = self()
210
211 :ok = Gun.after_request(opts)
212
213 assert %Connections{
214 conns: %{
215 "http:uniq-domain.com:80" => %Pleroma.Gun.Conn{
216 conn: ^conn,
217 conn_state: :active,
218 used_by: [{^self, _}]
219 }
220 }
221 } = Connections.get_state(:gun_connections)
222 end
223
224 test "with ipv4" do
225 uri = URI.parse("http://127.0.0.1")
226 :ok = Conn.open(uri, :gun_connections)
227 opts = Gun.options(uri)
228 :ok = Gun.after_request(opts)
229 conn = opts[:conn]
230
231 assert %Connections{
232 conns: %{
233 "http:127.0.0.1:80" => %Pleroma.Gun.Conn{
234 conn: ^conn,
235 conn_state: :idle,
236 used_by: []
237 }
238 }
239 } = Connections.get_state(:gun_connections)
240 end
241
242 test "with ipv6" do
243 uri = URI.parse("http://[2a03:2880:f10c:83:face:b00c:0:25de]")
244 :ok = Conn.open(uri, :gun_connections)
245 opts = Gun.options(uri)
246 :ok = Gun.after_request(opts)
247 conn = opts[:conn]
248
249 assert %Connections{
250 conns: %{
251 "http:2a03:2880:f10c:83:face:b00c:0:25de:80" => %Pleroma.Gun.Conn{
252 conn: ^conn,
253 conn_state: :idle,
254 used_by: []
255 }
256 }
257 } = Connections.get_state(:gun_connections)
258 end
259 end
260 end