7515f4e799cfe7a94db502777f3501f2c24e1ebc
[anni] / test / pleroma / http / adapter_helper / gun_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP.AdapterHelper.GunTest do
6   use ExUnit.Case
7   use Pleroma.Tests.Helpers
8
9   import Mox
10
11   alias Pleroma.HTTP.AdapterHelper.Gun
12
13   setup :verify_on_exit!
14
15   describe "options/1" do
16     setup do: clear_config([:http, :adapter], a: 1, b: 2)
17
18     test "https url with default port" do
19       uri = URI.parse("https://example.com")
20
21       opts = Gun.options([receive_conn: false], uri)
22       assert opts[:certificates_verification]
23     end
24
25     test "https ipv4 with default port" do
26       uri = URI.parse("https://127.0.0.1")
27
28       opts = Gun.options([receive_conn: false], uri)
29       assert opts[:certificates_verification]
30     end
31
32     test "https ipv6 with default port" do
33       uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]")
34
35       opts = Gun.options([receive_conn: false], uri)
36       assert opts[:certificates_verification]
37     end
38
39     test "https url with non standart port" do
40       uri = URI.parse("https://example.com:115")
41
42       opts = Gun.options([receive_conn: false], uri)
43
44       assert opts[:certificates_verification]
45     end
46
47     test "merges with defaul http adapter config" do
48       defaults = Gun.options([receive_conn: false], URI.parse("https://example.com"))
49       assert Keyword.has_key?(defaults, :a)
50       assert Keyword.has_key?(defaults, :b)
51     end
52
53     test "parses string proxy host & port" do
54       clear_config([:http, :proxy_url], "localhost:8123")
55
56       uri = URI.parse("https://some-domain.com")
57       opts = Gun.options([receive_conn: false], uri)
58       assert opts[:proxy] == {'localhost', 8123}
59     end
60
61     test "parses tuple proxy scheme host and port" do
62       clear_config([:http, :proxy_url], {:socks, 'localhost', 1234})
63
64       uri = URI.parse("https://some-domain.com")
65       opts = Gun.options([receive_conn: false], uri)
66       assert opts[:proxy] == {:socks, 'localhost', 1234}
67     end
68
69     test "passed opts have more weight than defaults" do
70       clear_config([:http, :proxy_url], {:socks5, 'localhost', 1234})
71       uri = URI.parse("https://some-domain.com")
72       opts = Gun.options([receive_conn: false, proxy: {'example.com', 4321}], uri)
73
74       assert opts[:proxy] == {'example.com', 4321}
75     end
76   end
77 end