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 |> expect(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(1000) end) end)
27 |> expect(:await_up, fn _, _ -> {:ok, :http} end)
28 |> expect(: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 refute opts[:tls_opts] == []
43 assert opts[:tls_opts][:verify_fun] ==
44 {&:ssl_verify_hostname.verify_fun/3, [check_hostname: 'example.com']}
46 assert File.exists?(opts[:tls_opts][:cacertfile])
49 test "https ipv4 with default port" do
50 uri = URI.parse("https://127.0.0.1")
52 opts = Gun.options([receive_conn: false], uri)
54 assert opts[:tls_opts][:verify_fun] ==
55 {&:ssl_verify_hostname.verify_fun/3, [check_hostname: '127.0.0.1']}
58 test "https ipv6 with default port" do
59 uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]")
61 opts = Gun.options([receive_conn: false], uri)
63 assert opts[:tls_opts][:verify_fun] ==
64 {&:ssl_verify_hostname.verify_fun/3,
65 [check_hostname: '2a03:2880:f10c:83:face:b00c:0:25de']}
68 test "https url with non standart port" do
69 uri = URI.parse("https://example.com:115")
71 opts = Gun.options([receive_conn: false], uri)
73 assert opts[:certificates_verification]
74 assert opts[:transport] == :tls
77 test "get conn on next request" do
79 level = Application.get_env(:logger, :level)
80 Logger.configure(level: :debug)
81 on_exit(fn -> Logger.configure(level: level) end)
82 uri = URI.parse("http://some-domain2.com")
84 assert capture_log(fn ->
85 opts = Gun.options(uri)
87 assert opts[:conn] == nil
88 assert opts[:close_conn] == nil
90 "Gun connections pool checkin was not successful. Trying to open conn for next request."
92 opts = Gun.options(uri)
94 assert is_pid(opts[:conn])
95 assert opts[:close_conn] == false
98 test "merges with defaul http adapter config" do
99 defaults = Gun.options([receive_conn: false], URI.parse("https://example.com"))
100 assert Keyword.has_key?(defaults, :a)
101 assert Keyword.has_key?(defaults, :b)
104 test "default ssl adapter opts with connection" do
106 uri = URI.parse("https://some-domain.com")
108 :ok = Conn.open(uri, :gun_connections)
110 opts = Gun.options(uri)
112 assert opts[:certificates_verification]
113 refute opts[:tls_opts] == []
115 assert opts[:close_conn] == false
116 assert is_pid(opts[:conn])
119 test "parses string proxy host & port" do
120 proxy = Config.get([:http, :proxy_url])
121 Config.put([:http, :proxy_url], "localhost:8123")
122 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
124 uri = URI.parse("https://some-domain.com")
125 opts = Gun.options([receive_conn: false], uri)
126 assert opts[:proxy] == {'localhost', 8123}
129 test "parses tuple proxy scheme host and port" do
130 proxy = Config.get([:http, :proxy_url])
131 Config.put([:http, :proxy_url], {:socks, 'localhost', 1234})
132 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
134 uri = URI.parse("https://some-domain.com")
135 opts = Gun.options([receive_conn: false], uri)
136 assert opts[:proxy] == {:socks, 'localhost', 1234}
139 test "passed opts have more weight than defaults" do
140 proxy = Config.get([:http, :proxy_url])
141 Config.put([:http, :proxy_url], {:socks5, 'localhost', 1234})
142 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
143 uri = URI.parse("https://some-domain.com")
144 opts = Gun.options([receive_conn: false, proxy: {'example.com', 4321}], uri)
146 assert opts[:proxy] == {'example.com', 4321}
150 describe "options/1 with receive_conn parameter" do
153 test "receive conn by default" do
154 uri = URI.parse("http://another-domain.com")
155 :ok = Conn.open(uri, :gun_connections)
157 received_opts = Gun.options(uri)
158 assert received_opts[:close_conn] == false
159 assert is_pid(received_opts[:conn])
162 test "don't receive conn if receive_conn is false" do
163 uri = URI.parse("http://another-domain.com")
164 :ok = Conn.open(uri, :gun_connections)
166 opts = [receive_conn: false]
167 received_opts = Gun.options(opts, uri)
168 assert received_opts[:close_conn] == nil
169 assert received_opts[:conn] == nil
173 describe "after_request/1" do
176 test "body_as not chunks" do
177 uri = URI.parse("http://some-domain.com")
178 :ok = Conn.open(uri, :gun_connections)
179 opts = Gun.options(uri)
180 :ok = Gun.after_request(opts)
185 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
191 } = Connections.get_state(:gun_connections)
194 test "body_as chunks" do
195 uri = URI.parse("http://some-domain.com")
196 :ok = Conn.open(uri, :gun_connections)
197 opts = Gun.options([body_as: :chunks], uri)
198 :ok = Gun.after_request(opts)
204 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
207 used_by: [{^self, _}]
210 } = Connections.get_state(:gun_connections)
213 test "with no connection" do
214 uri = URI.parse("http://uniq-domain.com")
216 :ok = Conn.open(uri, :gun_connections)
218 opts = Gun.options([body_as: :chunks], uri)
220 opts = Keyword.delete(opts, :conn)
223 :ok = Gun.after_request(opts)
227 "http:uniq-domain.com:80" => %Pleroma.Gun.Conn{
230 used_by: [{^self, _}]
233 } = Connections.get_state(:gun_connections)
237 uri = URI.parse("http://127.0.0.1")
238 :ok = Conn.open(uri, :gun_connections)
239 opts = Gun.options(uri)
240 :ok = Gun.after_request(opts)
245 "http:127.0.0.1:80" => %Pleroma.Gun.Conn{
251 } = Connections.get_state(:gun_connections)
255 uri = URI.parse("http://[2a03:2880:f10c:83:face:b00c:0:25de]")
256 :ok = Conn.open(uri, :gun_connections)
257 opts = Gun.options(uri)
258 :ok = Gun.after_request(opts)
263 "http:2a03:2880:f10c:83:face:b00c:0:25de:80" => %Pleroma.Gun.Conn{
269 } = Connections.get_state(:gun_connections)
273 describe "format_host/1" do
274 test "with domain" do
275 assert Gun.format_host("example.com") == 'example.com'
278 test "with idna domain" do
279 assert Gun.format_host("ですexample.com") == 'xn--example-183fne.com'
283 assert Gun.format_host("127.0.0.1") == '127.0.0.1'
287 assert Gun.format_host("2a03:2880:f10c:83:face:b00c:0:25de") ==
288 '2a03:2880:f10c:83:face:b00c:0:25de'