Check if mogrify available before calling it
[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
9 import ExUnit.CaptureLog
10
11 alias Pleroma.Config
12 alias Pleroma.HTTP.Connection
13
14 describe "parse_host/1" do
15 test "as atom to charlist" do
16 assert Connection.parse_host(:localhost) == 'localhost'
17 end
18
19 test "as string to charlist" do
20 assert Connection.parse_host("localhost.com") == 'localhost.com'
21 end
22
23 test "as string ip to tuple" do
24 assert Connection.parse_host("127.0.0.1") == {127, 0, 0, 1}
25 end
26 end
27
28 describe "parse_proxy/1" do
29 test "ip with port" do
30 assert Connection.parse_proxy("127.0.0.1:8123") == {:ok, {127, 0, 0, 1}, 8123}
31 end
32
33 test "host with port" do
34 assert Connection.parse_proxy("localhost:8123") == {:ok, 'localhost', 8123}
35 end
36
37 test "as tuple" do
38 assert Connection.parse_proxy({:socks4, :localhost, 9050}) ==
39 {:ok, :socks4, 'localhost', 9050}
40 end
41
42 test "as tuple with string host" do
43 assert Connection.parse_proxy({:socks5, "localhost", 9050}) ==
44 {:ok, :socks5, 'localhost', 9050}
45 end
46 end
47
48 describe "parse_proxy/1 errors" do
49 test "ip without port" do
50 capture_log(fn ->
51 assert Connection.parse_proxy("127.0.0.1") == {:error, :invalid_proxy}
52 end) =~ "parsing proxy fail \"127.0.0.1\""
53 end
54
55 test "host without port" do
56 capture_log(fn ->
57 assert Connection.parse_proxy("localhost") == {:error, :invalid_proxy}
58 end) =~ "parsing proxy fail \"localhost\""
59 end
60
61 test "host with bad port" do
62 capture_log(fn ->
63 assert Connection.parse_proxy("localhost:port") == {:error, :invalid_proxy_port}
64 end) =~ "parsing port in proxy fail \"localhost:port\""
65 end
66
67 test "ip with bad port" do
68 capture_log(fn ->
69 assert Connection.parse_proxy("127.0.0.1:15.9") == {:error, :invalid_proxy_port}
70 end) =~ "parsing port in proxy fail \"127.0.0.1:15.9\""
71 end
72
73 test "as tuple without port" do
74 capture_log(fn ->
75 assert Connection.parse_proxy({:socks5, :localhost}) == {:error, :invalid_proxy}
76 end) =~ "parsing proxy fail {:socks5, :localhost}"
77 end
78
79 test "with nil" do
80 assert Connection.parse_proxy(nil) == nil
81 end
82 end
83
84 describe "options/3" do
85 setup do: clear_config([:http, :proxy_url])
86
87 test "without proxy_url in config" do
88 Config.delete([:http, :proxy_url])
89
90 opts = Connection.options(%URI{})
91 refute Keyword.has_key?(opts, :proxy)
92 end
93
94 test "parses string proxy host & port" do
95 Config.put([:http, :proxy_url], "localhost:8123")
96
97 opts = Connection.options(%URI{})
98 assert opts[:proxy] == {'localhost', 8123}
99 end
100
101 test "parses tuple proxy scheme host and port" do
102 Config.put([:http, :proxy_url], {:socks, 'localhost', 1234})
103
104 opts = Connection.options(%URI{})
105 assert opts[:proxy] == {:socks, 'localhost', 1234}
106 end
107
108 test "passed opts have more weight than defaults" do
109 Config.put([:http, :proxy_url], {:socks5, 'localhost', 1234})
110
111 opts = Connection.options(%URI{}, proxy: {'example.com', 4321})
112
113 assert opts[:proxy] == {'example.com', 4321}
114 end
115 end
116
117 describe "format_host/1" do
118 test "with domain" do
119 assert Connection.format_host("example.com") == 'example.com'
120 end
121
122 test "with idna domain" do
123 assert Connection.format_host("ですexample.com") == 'xn--example-183fne.com'
124 end
125
126 test "with ipv4" do
127 assert Connection.format_host("127.0.0.1") == '127.0.0.1'
128 end
129
130 test "with ipv6" do
131 assert Connection.format_host("2a03:2880:f10c:83:face:b00c:0:25de") ==
132 '2a03:2880:f10c:83:face:b00c:0:25de'
133 end
134 end
135 end