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 clear_config([:http, :adapter]) do
32 Config.put([:http, :adapter], a: 1, b: 2)
35 test "https url with default port" do
36 uri = URI.parse("https://example.com")
38 opts = Gun.options([receive_conn: false], uri)
39 assert opts[:certificates_verification]
40 assert opts[:tls_opts][:log_level] == :warning
43 test "https ipv4 with default port" do
44 uri = URI.parse("https://127.0.0.1")
46 opts = Gun.options([receive_conn: false], uri)
47 assert opts[:certificates_verification]
48 assert opts[:tls_opts][:log_level] == :warning
51 test "https ipv6 with default port" do
52 uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]")
54 opts = Gun.options([receive_conn: false], uri)
55 assert opts[:certificates_verification]
56 assert opts[:tls_opts][:log_level] == :warning
59 test "https url with non standart port" do
60 uri = URI.parse("https://example.com:115")
62 opts = Gun.options([receive_conn: false], uri)
64 assert opts[:certificates_verification]
67 test "get conn on next request" do
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")
74 opts = Gun.options(uri)
76 assert opts[:conn] == nil
77 assert opts[:close_conn] == nil
80 opts = Gun.options(uri)
82 assert is_pid(opts[:conn])
83 assert opts[:close_conn] == false
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)
92 test "default ssl adapter opts with connection" do
94 uri = URI.parse("https://some-domain.com")
96 :ok = Conn.open(uri, :gun_connections)
98 opts = Gun.options(uri)
100 assert opts[:certificates_verification]
101 refute opts[:tls_opts] == []
103 assert opts[:close_conn] == false
104 assert is_pid(opts[:conn])
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)
112 uri = URI.parse("https://some-domain.com")
113 opts = Gun.options([receive_conn: false], uri)
114 assert opts[:proxy] == {'localhost', 8123}
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)
122 uri = URI.parse("https://some-domain.com")
123 opts = Gun.options([receive_conn: false], uri)
124 assert opts[:proxy] == {:socks, 'localhost', 1234}
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)
134 assert opts[:proxy] == {'example.com', 4321}
138 describe "options/1 with receive_conn parameter" do
141 test "receive conn by default" do
142 uri = URI.parse("http://another-domain.com")
143 :ok = Conn.open(uri, :gun_connections)
145 received_opts = Gun.options(uri)
146 assert received_opts[:close_conn] == false
147 assert is_pid(received_opts[:conn])
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)
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
161 describe "after_request/1" do
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)
173 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
179 } = Connections.get_state(:gun_connections)
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)
192 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
195 used_by: [{^self, _}]
198 } = Connections.get_state(:gun_connections)
201 test "with no connection" do
202 uri = URI.parse("http://uniq-domain.com")
204 :ok = Conn.open(uri, :gun_connections)
206 opts = Gun.options([body_as: :chunks], uri)
208 opts = Keyword.delete(opts, :conn)
211 :ok = Gun.after_request(opts)
215 "http:uniq-domain.com:80" => %Pleroma.Gun.Conn{
218 used_by: [{^self, _}]
221 } = Connections.get_state(:gun_connections)
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)
233 "http:127.0.0.1:80" => %Pleroma.Gun.Conn{
239 } = Connections.get_state(:gun_connections)
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)
251 "http:2a03:2880:f10c:83:face:b00c:0:25de:80" => %Pleroma.Gun.Conn{
257 } = Connections.get_state(:gun_connections)