summaryrefslogtreecommitdiff
path: root/test/http/connection_test.exs
blob: 99eab40263eafcae51cfc4f1116b7ab863e26f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
defmodule Pleroma.HTTP.ConnectionTest do
  use ExUnit.Case, async: true
  import ExUnit.CaptureLog
  alias Pleroma.HTTP.Connection

  describe "parse_host/1" do
    test "as atom" do
      assert Connection.parse_host(:localhost) == 'localhost'
    end

    test "as string" do
      assert Connection.parse_host("localhost.com") == 'localhost.com'
    end

    test "as string ip" do
      assert Connection.parse_host("127.0.0.1") == {127, 0, 0, 1}
    end
  end

  describe "parse_proxy/1" do
    test "ip with port" do
      assert Connection.parse_proxy("127.0.0.1:8123") == {:ok, {127, 0, 0, 1}, 8123}
    end

    test "host with port" do
      assert Connection.parse_proxy("localhost:8123") == {:ok, 'localhost', 8123}
    end

    test "as tuple" do
      assert Connection.parse_proxy({:socks5, :localhost, 9050}) == {:ok, 'localhost', 9050}
    end

    test "as tuple with string host" do
      assert Connection.parse_proxy({:socks5, "localhost", 9050}) == {:ok, 'localhost', 9050}
    end

    test "ip without port" do
      capture_log(fn ->
        assert Connection.parse_proxy("127.0.0.1") == {:error, :error_parsing_proxy}
      end) =~ "parsing proxy fail \"127.0.0.1\""
    end

    test "host without port" do
      capture_log(fn ->
        assert Connection.parse_proxy("localhost") == {:error, :error_parsing_proxy}
      end) =~ "parsing proxy fail \"localhost\""
    end

    test "host with bad port" do
      capture_log(fn ->
        assert Connection.parse_proxy("localhost:port") == {:error, :error_parsing_port_in_proxy}
      end) =~ "parsing port in proxy fail \"localhost:port\""
    end

    test "as tuple without port" do
      capture_log(fn ->
        assert Connection.parse_proxy({:socks5, :localhost}) == {:error, :error_parsing_proxy}
      end) =~ "parsing proxy fail {:socks5, :localhost}"
    end

    test "with nil" do
      assert Connection.parse_proxy(nil) == nil
    end
  end
end