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