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
12 alias Pleroma.Gun.Conn
13 alias Pleroma.HTTP.AdapterHelper.Gun
14 alias Pleroma.Pool.Connections
16 setup :verify_on_exit!
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)
30 describe "options/1" do
31 setup do: clear_config([:http, :adapter], a: 1, b: 2)
33 test "https url with default port" do
34 uri = URI.parse("https://example.com")
36 opts = Gun.options([receive_conn: false], uri)
37 assert opts[:certificates_verification]
38 assert opts[:tls_opts][:log_level] == :warning
41 test "https ipv4 with default port" do
42 uri = URI.parse("https://127.0.0.1")
44 opts = Gun.options([receive_conn: false], uri)
45 assert opts[:certificates_verification]
46 assert opts[:tls_opts][:log_level] == :warning
49 test "https ipv6 with default port" do
50 uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]")
52 opts = Gun.options([receive_conn: false], uri)
53 assert opts[:certificates_verification]
54 assert opts[:tls_opts][:log_level] == :warning
57 test "https url with non standart port" do
58 uri = URI.parse("https://example.com:115")
60 opts = Gun.options([receive_conn: false], uri)
62 assert opts[:certificates_verification]
65 test "get conn on next request" do
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")
72 opts = Gun.options(uri)
74 assert opts[:conn] == nil
75 assert opts[:close_conn] == nil
78 opts = Gun.options(uri)
80 assert is_pid(opts[:conn])
81 assert opts[:close_conn] == false
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)
90 test "default ssl adapter opts with connection" do
92 uri = URI.parse("https://some-domain.com")
94 :ok = Conn.open(uri, :gun_connections)
96 opts = Gun.options(uri)
98 assert opts[:certificates_verification]
99 refute opts[:tls_opts] == []
101 assert opts[:close_conn] == false
102 assert is_pid(opts[:conn])
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)
110 uri = URI.parse("https://some-domain.com")
111 opts = Gun.options([receive_conn: false], uri)
112 assert opts[:proxy] == {'localhost', 8123}
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)
120 uri = URI.parse("https://some-domain.com")
121 opts = Gun.options([receive_conn: false], uri)
122 assert opts[:proxy] == {:socks, 'localhost', 1234}
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)
132 assert opts[:proxy] == {'example.com', 4321}
136 describe "options/1 with receive_conn parameter" do
139 test "receive conn by default" do
140 uri = URI.parse("http://another-domain.com")
141 :ok = Conn.open(uri, :gun_connections)
143 received_opts = Gun.options(uri)
144 assert received_opts[:close_conn] == false
145 assert is_pid(received_opts[:conn])
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)
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
159 describe "after_request/1" do
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)
171 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
177 } = Connections.get_state(:gun_connections)
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)
190 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
193 used_by: [{^self, _}]
196 } = Connections.get_state(:gun_connections)
199 test "with no connection" do
200 uri = URI.parse("http://uniq-domain.com")
202 :ok = Conn.open(uri, :gun_connections)
204 opts = Gun.options([body_as: :chunks], uri)
206 opts = Keyword.delete(opts, :conn)
209 :ok = Gun.after_request(opts)
213 "http:uniq-domain.com:80" => %Pleroma.Gun.Conn{
216 used_by: [{^self, _}]
219 } = Connections.get_state(:gun_connections)
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)
231 "http:127.0.0.1:80" => %Pleroma.Gun.Conn{
237 } = Connections.get_state(:gun_connections)
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)
249 "http:2a03:2880:f10c:83:face:b00c:0:25de:80" => %Pleroma.Gun.Conn{
255 } = Connections.get_state(:gun_connections)