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.ConnectionTest do
7 use Pleroma.Tests.Helpers
9 import ExUnit.CaptureLog
12 alias Pleroma.HTTP.Connection
14 describe "parse_host/1" do
15 test "as atom to charlist" do
16 assert Connection.parse_host(:localhost) == 'localhost'
19 test "as string to charlist" do
20 assert Connection.parse_host("localhost.com") == 'localhost.com'
23 test "as string ip to tuple" do
24 assert Connection.parse_host("127.0.0.1") == {127, 0, 0, 1}
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}
33 test "host with port" do
34 assert Connection.parse_proxy("localhost:8123") == {:ok, 'localhost', 8123}
38 assert Connection.parse_proxy({:socks4, :localhost, 9050}) ==
39 {:ok, :socks4, 'localhost', 9050}
42 test "as tuple with string host" do
43 assert Connection.parse_proxy({:socks5, "localhost", 9050}) ==
44 {:ok, :socks5, 'localhost', 9050}
48 describe "parse_proxy/1 errors" do
49 test "ip without port" do
51 assert Connection.parse_proxy("127.0.0.1") == {:error, :invalid_proxy}
52 end) =~ "parsing proxy fail \"127.0.0.1\""
55 test "host without port" do
57 assert Connection.parse_proxy("localhost") == {:error, :invalid_proxy}
58 end) =~ "parsing proxy fail \"localhost\""
61 test "host with bad port" do
63 assert Connection.parse_proxy("localhost:port") == {:error, :invalid_proxy_port}
64 end) =~ "parsing port in proxy fail \"localhost:port\""
67 test "ip with bad port" do
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\""
73 test "as tuple without port" do
75 assert Connection.parse_proxy({:socks5, :localhost}) == {:error, :invalid_proxy}
76 end) =~ "parsing proxy fail {:socks5, :localhost}"
80 assert Connection.parse_proxy(nil) == nil
84 describe "options/3" do
85 setup do: clear_config([:http, :proxy_url])
87 test "without proxy_url in config" do
88 Config.delete([:http, :proxy_url])
90 opts = Connection.options(%URI{})
91 refute Keyword.has_key?(opts, :proxy)
94 test "parses string proxy host & port" do
95 Config.put([:http, :proxy_url], "localhost:8123")
97 opts = Connection.options(%URI{})
98 assert opts[:proxy] == {'localhost', 8123}
101 test "parses tuple proxy scheme host and port" do
102 Config.put([:http, :proxy_url], {:socks, 'localhost', 1234})
104 opts = Connection.options(%URI{})
105 assert opts[:proxy] == {:socks, 'localhost', 1234}
108 test "passed opts have more weight than defaults" do
109 Config.put([:http, :proxy_url], {:socks5, 'localhost', 1234})
111 opts = Connection.options(%URI{}, proxy: {'example.com', 4321})
113 assert opts[:proxy] == {'example.com', 4321}
117 describe "format_host/1" do
118 test "with domain" do
119 assert Connection.format_host("example.com") == 'example.com'
122 test "with idna domain" do
123 assert Connection.format_host("ですexample.com") == 'xn--example-183fne.com'
127 assert Connection.format_host("127.0.0.1") == '127.0.0.1'
131 assert Connection.format_host("2a03:2880:f10c:83:face:b00c:0:25de") ==
132 '2a03:2880:f10c:83:face:b00c:0:25de'