1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTTP.AdapterHelper.GunTest do
6 use ExUnit.Case, async: true
7 use Pleroma.Tests.Helpers
9 import ExUnit.CaptureLog
13 alias Pleroma.Gun.Conn
14 alias Pleroma.HTTP.AdapterHelper.Gun
15 alias Pleroma.Pool.Connections
17 setup :verify_on_exit!
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)
31 describe "options/1" do
32 clear_config([:http, :adapter]) do
33 Config.put([:http, :adapter], a: 1, b: 2)
36 test "https url with default port" do
37 uri = URI.parse("https://example.com")
39 opts = Gun.options([receive_conn: false], uri)
40 assert opts[:certificates_verification]
41 assert opts[:tls_opts][:log_level] == :warning
44 test "https ipv4 with default port" do
45 uri = URI.parse("https://127.0.0.1")
47 opts = Gun.options([receive_conn: false], uri)
48 assert opts[:certificates_verification]
49 assert opts[:tls_opts][:log_level] == :warning
52 test "https ipv6 with default port" do
53 uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]")
55 opts = Gun.options([receive_conn: false], uri)
56 assert opts[:certificates_verification]
57 assert opts[:tls_opts][:log_level] == :warning
60 test "https url with non standart port" do
61 uri = URI.parse("https://example.com:115")
63 opts = Gun.options([receive_conn: false], uri)
65 assert opts[:certificates_verification]
66 assert opts[:transport] == :tls
69 test "get conn on next request" do
71 level = Application.get_env(:logger, :level)
72 Logger.configure(level: :debug)
73 on_exit(fn -> Logger.configure(level: level) end)
74 uri = URI.parse("http://some-domain2.com")
76 assert capture_log(fn ->
77 opts = Gun.options(uri)
79 assert opts[:conn] == nil
80 assert opts[:close_conn] == nil
82 "Gun connections pool checkin was not successful. Trying to open conn for next request."
84 opts = Gun.options(uri)
86 assert is_pid(opts[:conn])
87 assert opts[:close_conn] == false
90 test "merges with defaul http adapter config" do
91 defaults = Gun.options([receive_conn: false], URI.parse("https://example.com"))
92 assert Keyword.has_key?(defaults, :a)
93 assert Keyword.has_key?(defaults, :b)
96 test "default ssl adapter opts with connection" do
98 uri = URI.parse("https://some-domain.com")
100 :ok = Conn.open(uri, :gun_connections)
102 opts = Gun.options(uri)
104 assert opts[:certificates_verification]
105 refute opts[:tls_opts] == []
107 assert opts[:close_conn] == false
108 assert is_pid(opts[:conn])
111 test "parses string proxy host & port" do
112 proxy = Config.get([:http, :proxy_url])
113 Config.put([:http, :proxy_url], "localhost:8123")
114 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
116 uri = URI.parse("https://some-domain.com")
117 opts = Gun.options([receive_conn: false], uri)
118 assert opts[:proxy] == {'localhost', 8123}
121 test "parses tuple proxy scheme host and port" do
122 proxy = Config.get([:http, :proxy_url])
123 Config.put([:http, :proxy_url], {:socks, 'localhost', 1234})
124 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
126 uri = URI.parse("https://some-domain.com")
127 opts = Gun.options([receive_conn: false], uri)
128 assert opts[:proxy] == {:socks, 'localhost', 1234}
131 test "passed opts have more weight than defaults" do
132 proxy = Config.get([:http, :proxy_url])
133 Config.put([:http, :proxy_url], {:socks5, 'localhost', 1234})
134 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
135 uri = URI.parse("https://some-domain.com")
136 opts = Gun.options([receive_conn: false, proxy: {'example.com', 4321}], uri)
138 assert opts[:proxy] == {'example.com', 4321}
142 describe "options/1 with receive_conn parameter" do
145 test "receive conn by default" do
146 uri = URI.parse("http://another-domain.com")
147 :ok = Conn.open(uri, :gun_connections)
149 received_opts = Gun.options(uri)
150 assert received_opts[:close_conn] == false
151 assert is_pid(received_opts[:conn])
154 test "don't receive conn if receive_conn is false" do
155 uri = URI.parse("http://another-domain.com")
156 :ok = Conn.open(uri, :gun_connections)
158 opts = [receive_conn: false]
159 received_opts = Gun.options(opts, uri)
160 assert received_opts[:close_conn] == nil
161 assert received_opts[:conn] == nil
165 describe "after_request/1" do
168 test "body_as not chunks" do
169 uri = URI.parse("http://some-domain.com")
170 :ok = Conn.open(uri, :gun_connections)
171 opts = Gun.options(uri)
172 :ok = Gun.after_request(opts)
177 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
183 } = Connections.get_state(:gun_connections)
186 test "body_as chunks" do
187 uri = URI.parse("http://some-domain.com")
188 :ok = Conn.open(uri, :gun_connections)
189 opts = Gun.options([body_as: :chunks], uri)
190 :ok = Gun.after_request(opts)
196 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
199 used_by: [{^self, _}]
202 } = Connections.get_state(:gun_connections)
205 test "with no connection" do
206 uri = URI.parse("http://uniq-domain.com")
208 :ok = Conn.open(uri, :gun_connections)
210 opts = Gun.options([body_as: :chunks], uri)
212 opts = Keyword.delete(opts, :conn)
215 :ok = Gun.after_request(opts)
219 "http:uniq-domain.com:80" => %Pleroma.Gun.Conn{
222 used_by: [{^self, _}]
225 } = Connections.get_state(:gun_connections)
229 uri = URI.parse("http://127.0.0.1")
230 :ok = Conn.open(uri, :gun_connections)
231 opts = Gun.options(uri)
232 :ok = Gun.after_request(opts)
237 "http:127.0.0.1:80" => %Pleroma.Gun.Conn{
243 } = Connections.get_state(:gun_connections)
247 uri = URI.parse("http://[2a03:2880:f10c:83:face:b00c:0:25de]")
248 :ok = Conn.open(uri, :gun_connections)
249 opts = Gun.options(uri)
250 :ok = Gun.after_request(opts)
255 "http:2a03:2880:f10c:83:face:b00c:0:25de:80" => %Pleroma.Gun.Conn{
261 } = Connections.get_state(:gun_connections)