logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

gun_test.exs (2720B)


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