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