summaryrefslogtreecommitdiff
path: root/test/http/adapter_test.exs
blob: 37e47dabe83bb9e315fead480028fa460bc73941 (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
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.HTTP.AdapterTest do
  use ExUnit.Case, async: true

  alias Pleroma.HTTP.Adapter

  describe "domain_or_ip/1" do
    test "with domain" do
      assert Adapter.domain_or_ip("example.com") == {:domain, 'example.com'}
    end

    test "with idna domain" do
      assert Adapter.domain_or_ip("ですexample.com") == {:domain, 'xn--example-183fne.com'}
    end

    test "with ipv4" do
      assert Adapter.domain_or_ip("127.0.0.1") == {:ip, {127, 0, 0, 1}}
    end

    test "with ipv6" do
      assert Adapter.domain_or_ip("2a03:2880:f10c:83:face:b00c:0:25de") ==
               {:ip, {10_755, 10_368, 61_708, 131, 64_206, 45_068, 0, 9_694}}
    end
  end

  describe "domain_or_fallback/1" do
    test "with domain" do
      assert Adapter.domain_or_fallback("example.com") == 'example.com'
    end

    test "with idna domain" do
      assert Adapter.domain_or_fallback("ですexample.com") == 'xn--example-183fne.com'
    end

    test "with ipv4" do
      assert Adapter.domain_or_fallback("127.0.0.1") == '127.0.0.1'
    end

    test "with ipv6" do
      assert Adapter.domain_or_fallback("2a03:2880:f10c:83:face:b00c:0:25de") ==
               '2a03:2880:f10c:83:face:b00c:0:25de'
    end
  end

  describe "format_proxy/1" do
    test "with nil" do
      assert Adapter.format_proxy(nil) == nil
    end

    test "with string" do
      assert Adapter.format_proxy("127.0.0.1:8123") == {{127, 0, 0, 1}, 8123}
    end

    test "localhost with port" do
      assert Adapter.format_proxy("localhost:8123") == {'localhost', 8123}
    end

    test "tuple" do
      assert Adapter.format_proxy({:socks4, :localhost, 9050}) == {:socks4, 'localhost', 9050}
    end
  end
end