80589c73d5f452cdccb64d4b164afc23055464f7
[akkoma] / test / http / adapter_helper / gun_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.AdapterHelper.GunTest do
6 use ExUnit.Case, async: true
7 use Pleroma.Tests.Helpers
8
9 import Mox
10
11 alias Pleroma.Config
12 alias Pleroma.HTTP.AdapterHelper.Gun
13
14 setup :verify_on_exit!
15
16 describe "options/1" do
17 setup do: clear_config([:http, :adapter], a: 1, b: 2)
18
19 test "https url with default port" do
20 uri = URI.parse("https://example.com")
21
22 opts = Gun.options([receive_conn: false], uri)
23 assert opts[:certificates_verification]
24 end
25
26 test "https ipv4 with default port" do
27 uri = URI.parse("https://127.0.0.1")
28
29 opts = Gun.options([receive_conn: false], uri)
30 assert opts[:certificates_verification]
31 end
32
33 test "https ipv6 with default port" do
34 uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]")
35
36 opts = Gun.options([receive_conn: false], uri)
37 assert opts[:certificates_verification]
38 end
39
40 test "https url with non standart port" do
41 uri = URI.parse("https://example.com:115")
42
43 opts = Gun.options([receive_conn: false], uri)
44
45 assert opts[:certificates_verification]
46 end
47
48 test "merges with defaul http adapter config" do
49 defaults = Gun.options([receive_conn: false], URI.parse("https://example.com"))
50 assert Keyword.has_key?(defaults, :a)
51 assert Keyword.has_key?(defaults, :b)
52 end
53
54 test "parses string proxy host & port" do
55 proxy = Config.get([:http, :proxy_url])
56 Config.put([:http, :proxy_url], "localhost:8123")
57 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
58
59 uri = URI.parse("https://some-domain.com")
60 opts = Gun.options([receive_conn: false], uri)
61 assert opts[:proxy] == {'localhost', 8123}
62 end
63
64 test "parses tuple proxy scheme host and port" do
65 proxy = Config.get([:http, :proxy_url])
66 Config.put([:http, :proxy_url], {:socks, 'localhost', 1234})
67 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
68
69 uri = URI.parse("https://some-domain.com")
70 opts = Gun.options([receive_conn: false], uri)
71 assert opts[:proxy] == {:socks, 'localhost', 1234}
72 end
73
74 test "passed opts have more weight than defaults" do
75 proxy = Config.get([:http, :proxy_url])
76 Config.put([:http, :proxy_url], {:socks5, 'localhost', 1234})
77 on_exit(fn -> Config.put([:http, :proxy_url], proxy) end)
78 uri = URI.parse("https://some-domain.com")
79 opts = Gun.options([receive_conn: false, proxy: {'example.com', 4321}], uri)
80
81 assert opts[:proxy] == {'example.com', 4321}
82 end
83 end
84 end