Update Copyrights for gun related files
[akkoma] / test / http / connection_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.ConnectionTest do
6 use ExUnit.Case
7 use Pleroma.Tests.Helpers
8 import ExUnit.CaptureLog
9 alias Pleroma.Config
10 alias Pleroma.HTTP.Connection
11
12 setup_all do
13 {:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.GunMock)
14 :ok
15 end
16
17 describe "parse_host/1" do
18 test "as atom to charlist" do
19 assert Connection.parse_host(:localhost) == 'localhost'
20 end
21
22 test "as string to charlist" do
23 assert Connection.parse_host("localhost.com") == 'localhost.com'
24 end
25
26 test "as string ip to tuple" do
27 assert Connection.parse_host("127.0.0.1") == {127, 0, 0, 1}
28 end
29 end
30
31 describe "parse_proxy/1" do
32 test "ip with port" do
33 assert Connection.parse_proxy("127.0.0.1:8123") == {:ok, {127, 0, 0, 1}, 8123}
34 end
35
36 test "host with port" do
37 assert Connection.parse_proxy("localhost:8123") == {:ok, 'localhost', 8123}
38 end
39
40 test "as tuple" do
41 assert Connection.parse_proxy({:socks4, :localhost, 9050}) ==
42 {:ok, :socks4, 'localhost', 9050}
43 end
44
45 test "as tuple with string host" do
46 assert Connection.parse_proxy({:socks5, "localhost", 9050}) ==
47 {:ok, :socks5, 'localhost', 9050}
48 end
49 end
50
51 describe "parse_proxy/1 errors" do
52 test "ip without port" do
53 capture_log(fn ->
54 assert Connection.parse_proxy("127.0.0.1") == {:error, :invalid_proxy}
55 end) =~ "parsing proxy fail \"127.0.0.1\""
56 end
57
58 test "host without port" do
59 capture_log(fn ->
60 assert Connection.parse_proxy("localhost") == {:error, :invalid_proxy}
61 end) =~ "parsing proxy fail \"localhost\""
62 end
63
64 test "host with bad port" do
65 capture_log(fn ->
66 assert Connection.parse_proxy("localhost:port") == {:error, :invalid_proxy_port}
67 end) =~ "parsing port in proxy fail \"localhost:port\""
68 end
69
70 test "ip with bad port" do
71 capture_log(fn ->
72 assert Connection.parse_proxy("127.0.0.1:15.9") == {:error, :invalid_proxy_port}
73 end) =~ "parsing port in proxy fail \"127.0.0.1:15.9\""
74 end
75
76 test "as tuple without port" do
77 capture_log(fn ->
78 assert Connection.parse_proxy({:socks5, :localhost}) == {:error, :invalid_proxy}
79 end) =~ "parsing proxy fail {:socks5, :localhost}"
80 end
81
82 test "with nil" do
83 assert Connection.parse_proxy(nil) == nil
84 end
85 end
86
87 describe "options/3" do
88 clear_config([:http, :proxy_url])
89
90 test "without proxy_url in config" do
91 Config.delete([:http, :proxy_url])
92
93 opts = Connection.options(%URI{})
94 refute Keyword.has_key?(opts, :proxy)
95 end
96
97 test "parses string proxy host & port" do
98 Config.put([:http, :proxy_url], "localhost:8123")
99
100 opts = Connection.options(%URI{})
101 assert opts[:proxy] == {'localhost', 8123}
102 end
103
104 test "parses tuple proxy scheme host and port" do
105 Config.put([:http, :proxy_url], {:socks, 'localhost', 1234})
106
107 opts = Connection.options(%URI{})
108 assert opts[:proxy] == {:socks, 'localhost', 1234}
109 end
110
111 test "passed opts have more weight than defaults" do
112 Config.put([:http, :proxy_url], {:socks5, 'localhost', 1234})
113
114 opts = Connection.options(%URI{}, proxy: {'example.com', 4321})
115
116 assert opts[:proxy] == {'example.com', 4321}
117 end
118
119 test "default ssl adapter opts with connection" do
120 adapter = Application.get_env(:tesla, :adapter)
121 Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun)
122 on_exit(fn -> Application.put_env(:tesla, :adapter, adapter) end)
123
124 uri = URI.parse("https://some-domain.com")
125
126 pid = Process.whereis(:federation)
127 :ok = Pleroma.Gun.Conn.open(uri, :gun_connections, genserver_pid: pid)
128
129 opts = Connection.options(uri)
130
131 assert opts[:certificates_verification]
132 tls_opts = opts[:tls_opts]
133 assert tls_opts[:verify] == :verify_peer
134 assert tls_opts[:depth] == 20
135 assert tls_opts[:reuse_sessions] == false
136
137 assert opts[:original] == "some-domain.com:443"
138 assert opts[:close_conn] == false
139 assert is_pid(opts[:conn])
140 end
141 end
142 end