diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/http/adapter_helper/gun_test.exs | 233 | ||||
-rw-r--r-- | test/http/adapter_helper_test.exs | 28 | ||||
-rw-r--r-- | test/http/connection_test.exs | 128 | ||||
-rw-r--r-- | test/http/gun_test.exs | 84 | ||||
-rw-r--r-- | test/http/hackney_test.exs (renamed from test/http/adapter_helper/hackney_test.exs) | 4 | ||||
-rw-r--r-- | test/http/middleware/follow_redirects_test.exs | 284 | ||||
-rw-r--r-- | test/http/proxy_test.exs | 90 | ||||
-rw-r--r-- | test/http/request/builder_test.exs (renamed from test/http/request_builder_test.exs) | 18 |
8 files changed, 492 insertions, 377 deletions
diff --git a/test/http/adapter_helper/gun_test.exs b/test/http/adapter_helper/gun_test.exs deleted file mode 100644 index 95a8cf814..000000000 --- a/test/http/adapter_helper/gun_test.exs +++ /dev/null @@ -1,233 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.HTTP.AdapterHelper.GunTest do - use ExUnit.Case - use Pleroma.Tests.Helpers - - import Mox - - alias Pleroma.Config - alias Pleroma.HTTP.AdapterHelper.Gun - alias Pleroma.Pool.Connections - - setup :verify_on_exit! - setup :set_mox_global - - defp gun_mock do - Pleroma.GunMock - |> stub(:open, fn _, _, _ -> - Task.start_link(fn -> Process.sleep(1000) end) - end) - |> stub(:await_up, fn _, _ -> {:ok, :http} end) - |> stub(:set_owner, fn _, _ -> :ok end) - - :ok - end - - describe "options/1" do - setup do: clear_config([:http, :adapter], a: 1, b: 2) - - test "https url with default port" do - uri = URI.parse("https://example.com") - - opts = Gun.options([receive_conn: false], uri) - assert opts[:certificates_verification] - assert opts[:tls_opts][:log_level] == :warning - end - - test "https ipv4 with default port" do - uri = URI.parse("https://127.0.0.1") - - opts = Gun.options([receive_conn: false], uri) - assert opts[:certificates_verification] - assert opts[:tls_opts][:log_level] == :warning - end - - test "https ipv6 with default port" do - uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]") - - opts = Gun.options([receive_conn: false], uri) - assert opts[:certificates_verification] - assert opts[:tls_opts][:log_level] == :warning - end - - test "https url with non standart port" do - uri = URI.parse("https://example.com:115") - - opts = Gun.options([receive_conn: false], uri) - - assert opts[:certificates_verification] - end - - test "merges with defaul http adapter config" do - defaults = Gun.options([receive_conn: false], URI.parse("https://example.com")) - assert Keyword.has_key?(defaults, :a) - assert Keyword.has_key?(defaults, :b) - end - - test "default ssl adapter opts with connection" do - gun_mock() - uri = URI.parse("https://some-domain.com") - - opts = Gun.options(uri) - - assert opts[:certificates_verification] - refute opts[:tls_opts] == [] - - assert opts[:close_conn] == false - assert is_pid(opts[:conn]) - end - - test "parses string proxy host & port" do - proxy = Config.get([:http, :proxy_url]) - Config.put([:http, :proxy_url], "localhost:8123") - on_exit(fn -> Config.put([:http, :proxy_url], proxy) end) - - uri = URI.parse("https://some-domain.com") - opts = Gun.options([receive_conn: false], uri) - assert opts[:proxy] == {'localhost', 8123} - end - - test "parses tuple proxy scheme host and port" do - proxy = Config.get([:http, :proxy_url]) - Config.put([:http, :proxy_url], {:socks, 'localhost', 1234}) - on_exit(fn -> Config.put([:http, :proxy_url], proxy) end) - - uri = URI.parse("https://some-domain.com") - opts = Gun.options([receive_conn: false], uri) - assert opts[:proxy] == {:socks, 'localhost', 1234} - end - - test "passed opts have more weight than defaults" do - proxy = Config.get([:http, :proxy_url]) - Config.put([:http, :proxy_url], {:socks5, 'localhost', 1234}) - on_exit(fn -> Config.put([:http, :proxy_url], proxy) end) - uri = URI.parse("https://some-domain.com") - opts = Gun.options([receive_conn: false, proxy: {'example.com', 4321}], uri) - - assert opts[:proxy] == {'example.com', 4321} - end - end - - describe "options/1 with receive_conn parameter" do - setup do: gun_mock() - - test "receive conn by default" do - uri = URI.parse("http://another-domain.com") - - received_opts = Gun.options(uri) - assert received_opts[:close_conn] == false - assert is_pid(received_opts[:conn]) - end - - test "don't receive conn if receive_conn is false" do - uri = URI.parse("http://another-domain.com") - - opts = [receive_conn: false] - received_opts = Gun.options(opts, uri) - assert received_opts[:close_conn] == nil - assert received_opts[:conn] == nil - end - end - - describe "after_request/1" do - setup do: gun_mock() - - test "body_as not chunks" do - uri = URI.parse("http://some-domain-5.com") - opts = Gun.options(uri) - :ok = Gun.after_request(opts) - conn = opts[:conn] - - assert match?( - %Connections{ - conns: %{ - "http:some-domain-5.com:80" => %Pleroma.Gun.Conn{ - conn: ^conn, - conn_state: :idle, - used_by: [] - } - } - }, - Connections.get_state(:gun_connections) - ) - end - - test "body_as chunks" do - uri = URI.parse("http://some-domain-6.com") - opts = Gun.options([body_as: :chunks], uri) - :ok = Gun.after_request(opts) - conn = opts[:conn] - self = self() - - assert match?( - %Connections{ - conns: %{ - "http:some-domain-6.com:80" => %Pleroma.Gun.Conn{ - conn: ^conn, - conn_state: :active, - used_by: [{^self, _}] - } - } - }, - Connections.get_state(:gun_connections) - ) - end - - test "with no connection" do - uri = URI.parse("http://uniq-domain.com") - opts = Gun.options([body_as: :chunks], uri) - conn = opts[:conn] - opts = Keyword.delete(opts, :conn) - self = self() - - :ok = Gun.after_request(opts) - - assert %Connections{ - conns: %{ - "http:uniq-domain.com:80" => %Pleroma.Gun.Conn{ - conn: ^conn, - conn_state: :active, - used_by: [{^self, _}] - } - } - } = Connections.get_state(:gun_connections) - end - - test "with ipv4" do - uri = URI.parse("http://127.0.0.1") - opts = Gun.options(uri) - :ok = Gun.after_request(opts) - conn = opts[:conn] - - assert %Connections{ - conns: %{ - "http:127.0.0.1:80" => %Pleroma.Gun.Conn{ - conn: ^conn, - conn_state: :idle, - used_by: [] - } - } - } = Connections.get_state(:gun_connections) - end - - test "with ipv6" do - uri = URI.parse("http://[2a03:2880:f10c:83:face:b00c:0:25de]") - opts = Gun.options(uri) - :ok = Gun.after_request(opts) - conn = opts[:conn] - - assert %Connections{ - conns: %{ - "http:2a03:2880:f10c:83:face:b00c:0:25de:80" => %Pleroma.Gun.Conn{ - conn: ^conn, - conn_state: :idle, - used_by: [] - } - } - } = Connections.get_state(:gun_connections) - end - end -end diff --git a/test/http/adapter_helper_test.exs b/test/http/adapter_helper_test.exs deleted file mode 100644 index 24d501ad5..000000000 --- a/test/http/adapter_helper_test.exs +++ /dev/null @@ -1,28 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.HTTP.AdapterHelperTest do - use ExUnit.Case, async: true - - alias Pleroma.HTTP.AdapterHelper - - describe "format_proxy/1" do - test "with nil" do - assert AdapterHelper.format_proxy(nil) == nil - end - - test "with string" do - assert AdapterHelper.format_proxy("127.0.0.1:8123") == {{127, 0, 0, 1}, 8123} - end - - test "localhost with port" do - assert AdapterHelper.format_proxy("localhost:8123") == {'localhost', 8123} - end - - test "tuple" do - assert AdapterHelper.format_proxy({:socks4, :localhost, 9050}) == - {:socks4, 'localhost', 9050} - end - end -end diff --git a/test/http/connection_test.exs b/test/http/connection_test.exs index 5cc78ad5b..19b39a6b7 100644 --- a/test/http/connection_test.exs +++ b/test/http/connection_test.exs @@ -3,133 +3,51 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.HTTP.ConnectionTest do - use ExUnit.Case, async: true - use Pleroma.Tests.Helpers + use ExUnit.Case - import ExUnit.CaptureLog - - alias Pleroma.Config alias Pleroma.HTTP.Connection - describe "parse_host/1" do + describe "format_host/1" do test "as atom to charlist" do - assert Connection.parse_host(:localhost) == 'localhost' + assert Connection.format_host(:localhost) == 'localhost' end test "as string to charlist" do - assert Connection.parse_host("localhost.com") == 'localhost.com' + assert Connection.format_host("localhost.com") == 'localhost.com' end test "as string ip to tuple" do - assert Connection.parse_host("127.0.0.1") == {127, 0, 0, 1} - end - end - - describe "parse_proxy/1" do - test "ip with port" do - assert Connection.parse_proxy("127.0.0.1:8123") == {:ok, {127, 0, 0, 1}, 8123} - end - - test "host with port" do - assert Connection.parse_proxy("localhost:8123") == {:ok, 'localhost', 8123} - end - - test "as tuple" do - assert Connection.parse_proxy({:socks4, :localhost, 9050}) == - {:ok, :socks4, 'localhost', 9050} - end - - test "as tuple with string host" do - assert Connection.parse_proxy({:socks5, "localhost", 9050}) == - {:ok, :socks5, 'localhost', 9050} - end - end - - describe "parse_proxy/1 errors" do - test "ip without port" do - capture_log(fn -> - assert Connection.parse_proxy("127.0.0.1") == {:error, :invalid_proxy} - end) =~ "parsing proxy fail \"127.0.0.1\"" - end - - test "host without port" do - capture_log(fn -> - assert Connection.parse_proxy("localhost") == {:error, :invalid_proxy} - end) =~ "parsing proxy fail \"localhost\"" - end - - test "host with bad port" do - capture_log(fn -> - assert Connection.parse_proxy("localhost:port") == {:error, :invalid_proxy_port} - end) =~ "parsing port in proxy fail \"localhost:port\"" - end - - test "ip with bad port" do - capture_log(fn -> - assert Connection.parse_proxy("127.0.0.1:15.9") == {:error, :invalid_proxy_port} - end) =~ "parsing port in proxy fail \"127.0.0.1:15.9\"" - end - - test "as tuple without port" do - capture_log(fn -> - assert Connection.parse_proxy({:socks5, :localhost}) == {:error, :invalid_proxy} - end) =~ "parsing proxy fail {:socks5, :localhost}" - end - - test "with nil" do - assert Connection.parse_proxy(nil) == nil + assert Connection.format_host("127.0.0.1") == {127, 0, 0, 1} end end - describe "options/3" do - setup do: clear_config([:http, :proxy_url]) - - test "without proxy_url in config" do - Config.delete([:http, :proxy_url]) - - opts = Connection.options(%URI{}) - refute Keyword.has_key?(opts, :proxy) - end - - test "parses string proxy host & port" do - Config.put([:http, :proxy_url], "localhost:8123") - - opts = Connection.options(%URI{}) - assert opts[:proxy] == {'localhost', 8123} - end - - test "parses tuple proxy scheme host and port" do - Config.put([:http, :proxy_url], {:socks, 'localhost', 1234}) - - opts = Connection.options(%URI{}) - assert opts[:proxy] == {:socks, 'localhost', 1234} + describe "options/2" do + test "defaults" do + assert Connection.options(%URI{}) == [env: :test, pool: :federation] end test "passed opts have more weight than defaults" do - Config.put([:http, :proxy_url], {:socks5, 'localhost', 1234}) - - opts = Connection.options(%URI{}, proxy: {'example.com', 4321}) - - assert opts[:proxy] == {'example.com', 4321} + assert Connection.options(%URI{}, pool: :media) == [env: :test, pool: :media] end - end - describe "format_host/1" do - test "with domain" do - assert Connection.format_host("example.com") == 'example.com' - end + test "adding defaults for hackney adapter" do + initial = Application.get_env(:tesla, :adapter) + Application.put_env(:tesla, :adapter, Tesla.Adapter.Hackney) + on_exit(fn -> Application.put_env(:tesla, :adapter, initial) end) - test "with idna domain" do - assert Connection.format_host("ですexample.com") == 'xn--example-183fne.com' + refute %URI{scheme: "https", host: "example.com"} + |> Connection.options() + |> Keyword.delete(:pool) == [] end - test "with ipv4" do - assert Connection.format_host("127.0.0.1") == '127.0.0.1' - end + test "adding defaults for gun adapter" do + initial = Application.get_env(:tesla, :adapter) + Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun) + on_exit(fn -> Application.put_env(:tesla, :adapter, initial) end) - test "with ipv6" do - assert Connection.format_host("2a03:2880:f10c:83:face:b00c:0:25de") == - '2a03:2880:f10c:83:face:b00c:0:25de' + refute %URI{scheme: "https", host: "example.com"} + |> Connection.options() + |> Keyword.delete(:pool) == [] end end end diff --git a/test/http/gun_test.exs b/test/http/gun_test.exs new file mode 100644 index 000000000..c64cf7125 --- /dev/null +++ b/test/http/gun_test.exs @@ -0,0 +1,84 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.HTTP.GunTest do + use ExUnit.Case + use Pleroma.Tests.Helpers + + alias Pleroma.HTTP.Gun + + describe "options/1" do + test "https url with default port" do + uri = URI.parse("https://example.com") + + opts = Gun.options([reuse_conn: false], uri) + assert opts[:certificates_verification] + assert opts[:tls_opts][:log_level] == :warning + assert opts[:reuse_conn] == false + end + + test "https ipv4 with default port" do + uri = URI.parse("https://127.0.0.1") + + opts = Gun.options([reuse_conn: false], uri) + assert opts[:certificates_verification] + end + + test "https ipv6 with default port" do + uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]") + + opts = Gun.options([reuse_conn: false], uri) + assert opts[:certificates_verification] + end + + test "https url with non standart port" do + uri = URI.parse("https://example.com:115") + + opts = Gun.options([reuse_conn: false], uri) + + assert opts[:certificates_verification] + end + + test "merges with defaul http adapter config" do + clear_config([:http, :adapter], a: 1, b: 2) + defaults = Gun.options([reuse_conn: false], URI.parse("https://example.com")) + assert Keyword.has_key?(defaults, :a) + assert Keyword.has_key?(defaults, :b) + end + + test "default ssl adapter opts with connection" do + uri = URI.parse("https://some-domain.com") + + opts = Gun.options(uri) + + assert opts[:certificates_verification] + assert opts[:reuse_conn] + refute opts[:tls_opts] == [] + end + + test "parses string proxy host & port" do + clear_config([:http, :proxy_url], "localhost:8123") + + uri = URI.parse("https://some-domain.com") + opts = Gun.options([reuse_conn: false], uri) + assert opts[:proxy] == {'localhost', 8123} + end + + test "parses tuple proxy scheme host and port" do + clear_config([:http, :proxy_url], {:socks, 'localhost', 1234}) + + uri = URI.parse("https://some-domain.com") + opts = Gun.options([reuse_conn: false], uri) + assert opts[:proxy] == {:socks, 'localhost', 1234} + end + + test "passed opts have more weight than defaults" do + clear_config([:http, :proxy_url], {:socks5, 'localhost', 1234}) + + uri = URI.parse("https://some-domain.com") + opts = Gun.options([reuse_conn: false, proxy: {'example.com', 4321}], uri) + assert opts[:proxy] == {'example.com', 4321} + end + end +end diff --git a/test/http/adapter_helper/hackney_test.exs b/test/http/hackney_test.exs index 3f7e708e0..492eb511e 100644 --- a/test/http/adapter_helper/hackney_test.exs +++ b/test/http/hackney_test.exs @@ -2,11 +2,11 @@ # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> # SPDX-License-Identifier: AGPL-3.0-only -defmodule Pleroma.HTTP.AdapterHelper.HackneyTest do +defmodule Pleroma.HTTP.HackneyTest do use ExUnit.Case, async: true use Pleroma.Tests.Helpers - alias Pleroma.HTTP.AdapterHelper.Hackney + alias Pleroma.HTTP.Hackney setup_all do uri = URI.parse("http://domain.com") diff --git a/test/http/middleware/follow_redirects_test.exs b/test/http/middleware/follow_redirects_test.exs new file mode 100644 index 000000000..533aff2c1 --- /dev/null +++ b/test/http/middleware/follow_redirects_test.exs @@ -0,0 +1,284 @@ +defmodule Pleroma.HTTP.Middleware.FollowRedirectsTest do + use ExUnit.Case + + import Mox + + alias Pleroma.Gun.Conn + alias Pleroma.HTTP.Middleware.FollowRedirects + alias Pleroma.Pool.Connections + + setup :verify_on_exit! + setup :set_mox_global + + defp gun_mock do + Pleroma.GunMock + |> stub(:open, fn _, _, _ -> + Task.start_link(fn -> Process.sleep(1000) end) + end) + |> stub(:await_up, fn _, _ -> {:ok, :http} end) + |> stub(:set_owner, fn _, _ -> :ok end) + + :ok + end + + setup do + gun_mock() + + env = %Tesla.Env{ + body: "", + headers: [ + {"user-agent", "Pleroma"} + ], + method: :get, + opts: [ + adapter: [ + pool: :media, + reuse_conn: true + ] + ] + } + + {:ok, env: env} + end + + defmodule NoRedirect do + def call(env, _opts) do + opts = env.opts[:adapter] + assert opts[:reuse_conn] + assert opts[:conn] + assert opts[:close_conn] == false + + {:ok, %{env | status: 200, body: opts[:conn]}} + end + end + + describe "checkin/checkout conn without redirects" do + setup do + next = [{NoRedirect, :call, [[]]}] + {:ok, next: next} + end + + test "common", %{env: env, next: next} do + env = %{env | url: "https://common.com/media/common.jpg"} + assert {:ok, %{body: conn}} = FollowRedirects.call(env, next) + + assert match?( + %Connections{ + conns: %{ + "https:common.com:443" => %Conn{ + awaited_by: [], + conn: ^conn, + conn_state: :idle, + gun_state: :up, + retries: 0, + used_by: [] + } + } + }, + Connections.get_state(:gun_connections) + ) + end + + test "reverse proxy call", %{env: env, next: next} do + env = + put_in(env.opts[:adapter][:body_as], :chunks) + |> Map.put(:url, "https://chunks.com/media/chunks.jpg") + + assert {:ok, %{body: conn}} = FollowRedirects.call(env, next) + + self = self() + + assert match?( + %Connections{ + conns: %{ + "https:chunks.com:443" => %Conn{ + awaited_by: [], + conn: ^conn, + conn_state: :active, + gun_state: :up, + retries: 0, + used_by: [{^self, _}] + } + } + }, + Connections.get_state(:gun_connections) + ) + end + end + + defmodule OneRedirect do + def call(%{url: "https://first-redirect.com"} = env, _opts) do + opts = env.opts[:adapter] + assert opts[:reuse_conn] + assert opts[:conn] + assert opts[:close_conn] == false + + {:ok, %{env | status: 302, body: opts[:conn], headers: [{"location", opts[:final_url]}]}} + end + + def call(env, _opts) do + opts = env.opts[:adapter] + assert opts[:reuse_conn] + assert opts[:conn] + assert opts[:close_conn] == false + + {:ok, %{env | status: 200, body: opts[:conn]}} + end + end + + describe "checkin/checkout with 1 redirect" do + setup do + next = [{OneRedirect, :call, [[]]}] + + {:ok, next: next} + end + + test "common with redirect", %{env: env, next: next} do + adapter_opts = Keyword.put(env.opts[:adapter], :final_url, "https://another-final-url.com") + + env = + put_in(env.opts[:adapter], adapter_opts) + |> Map.put(:url, "https://first-redirect.com") + + assert {:ok, %{body: conn}} = FollowRedirects.call(env, next) + + assert match?( + %Connections{ + conns: %{ + "https:first-redirect.com:443" => %Conn{ + awaited_by: [], + conn: _, + conn_state: :idle, + gun_state: :up, + retries: 0, + used_by: [] + }, + "https:another-final-url.com:443" => %Conn{ + awaited_by: [], + conn: ^conn, + conn_state: :idle, + gun_state: :up, + retries: 0, + used_by: [] + } + } + }, + Connections.get_state(:gun_connections) + ) + end + + test "reverse proxy with redirect", %{env: env, next: next} do + adapter_opts = + Keyword.merge(env.opts[:adapter], body_as: :chunks, final_url: "https://final-url.com") + + env = + put_in(env.opts[:adapter], adapter_opts) + |> Map.put(:url, "https://first-redirect.com") + + assert {:ok, %{body: conn}} = FollowRedirects.call(env, next) + + self = self() + + assert match?( + %Connections{ + conns: %{ + "https:first-redirect.com:443" => %Conn{ + awaited_by: [], + conn: _, + conn_state: :idle, + gun_state: :up, + retries: 0, + used_by: [] + }, + "https:final-url.com:443" => %Conn{ + awaited_by: [], + conn: ^conn, + conn_state: :active, + gun_state: :up, + retries: 0, + used_by: [{^self, _}] + } + } + }, + Connections.get_state(:gun_connections) + ) + end + end + + defmodule TwoRedirect do + def call(%{url: "https://1-redirect.com"} = env, _opts) do + opts = env.opts[:adapter] + assert opts[:reuse_conn] + assert opts[:conn] + assert opts[:close_conn] == false + + {:ok, + %{env | status: 302, body: opts[:conn], headers: [{"location", "https://2-redirect.com"}]}} + end + + def call(%{url: "https://2-redirect.com"} = env, _opts) do + opts = env.opts[:adapter] + assert opts[:reuse_conn] + assert opts[:conn] + assert opts[:close_conn] == false + + {:ok, %{env | status: 302, body: opts[:conn], headers: [{"location", opts[:final_url]}]}} + end + + def call(env, _opts) do + opts = env.opts[:adapter] + assert opts[:reuse_conn] + assert opts[:conn] + assert opts[:close_conn] == false + + {:ok, %{env | status: 200, body: opts[:conn]}} + end + end + + describe "checkin/checkout conn with max redirects" do + setup do + next = [{TwoRedirect, :call, [[]]}] + {:ok, next: next} + end + + test "common with max redirects", %{env: env, next: next} do + adapter_opts = + Keyword.merge(env.opts[:adapter], + final_url: "https://some-final-url.com" + ) + + env = + put_in(env.opts[:adapter], adapter_opts) + |> Map.put(:url, "https://1-redirect.com") + + assert match?( + {:error, {FollowRedirects, :too_many_redirects}}, + FollowRedirects.call(env, next, max_redirects: 1) + ) + + assert match?( + %Connections{ + conns: %{ + "https:1-redirect.com:443" => %Conn{ + awaited_by: [], + conn: _, + conn_state: :idle, + gun_state: :up, + retries: 0, + used_by: [] + }, + "https:2-redirect.com:443" => %Conn{ + awaited_by: [], + conn: _, + conn_state: :idle, + gun_state: :up, + retries: 0, + used_by: [] + } + } + }, + Connections.get_state(:gun_connections) + ) + end + end +end diff --git a/test/http/proxy_test.exs b/test/http/proxy_test.exs new file mode 100644 index 000000000..ba5a9ca85 --- /dev/null +++ b/test/http/proxy_test.exs @@ -0,0 +1,90 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.HTTP.ProxyTest do + use ExUnit.Case, async: true + use Pleroma.Tests.Helpers + + import ExUnit.CaptureLog + + alias Pleroma.HTTP.Proxy + + describe "parse_proxy/1" do + test "ip with port" do + assert Proxy.parse_proxy("127.0.0.1:8123") == {:ok, {127, 0, 0, 1}, 8123} + end + + test "host with port" do + assert Proxy.parse_proxy("localhost:8123") == {:ok, 'localhost', 8123} + end + + test "as tuple" do + assert Proxy.parse_proxy({:socks4, :localhost, 9050}) == + {:ok, :socks4, 'localhost', 9050} + end + + test "as tuple with string host" do + assert Proxy.parse_proxy({:socks5, "localhost", 9050}) == + {:ok, :socks5, 'localhost', 9050} + end + end + + describe "parse_proxy/1 errors" do + test "ip without port" do + capture_log(fn -> + assert Proxy.parse_proxy("127.0.0.1") == {:error, :invalid_proxy} + end) =~ "parsing proxy fail \"127.0.0.1\"" + end + + test "host without port" do + capture_log(fn -> + assert Proxy.parse_proxy("localhost") == {:error, :invalid_proxy} + end) =~ "parsing proxy fail \"localhost\"" + end + + test "host with bad port" do + capture_log(fn -> + assert Proxy.parse_proxy("localhost:port") == {:error, :invalid_proxy_port} + end) =~ "parsing port in proxy fail \"localhost:port\"" + end + + test "ip with bad port" do + capture_log(fn -> + assert Proxy.parse_proxy("127.0.0.1:15.9") == {:error, :invalid_proxy_port} + end) =~ "parsing port in proxy fail \"127.0.0.1:15.9\"" + end + + test "as tuple without port" do + capture_log(fn -> + assert Proxy.parse_proxy({:socks5, :localhost}) == {:error, :invalid_proxy} + end) =~ "parsing proxy fail {:socks5, :localhost}" + end + + test "with nil" do + assert Proxy.parse_proxy(nil) == nil + end + end + + describe "maybe_add_proxy/1" do + test "proxy as ip with port" do + clear_config([:http, :proxy_url], "127.0.0.1:8123") + + assert Proxy.maybe_add_proxy([]) == [proxy: {{127, 0, 0, 1}, 8123}] + end + + test "proxy as localhost with port" do + clear_config([:http, :proxy_url], "localhost:8123") + assert Proxy.maybe_add_proxy([]) == [proxy: {'localhost', 8123}] + end + + test "proxy as tuple" do + clear_config([:http, :proxy_url], {:socks4, :localhost, 9050}) + assert Proxy.maybe_add_proxy([]) == [proxy: {:socks4, 'localhost', 9050}] + end + + test "without proxy" do + assert Proxy.maybe_add_proxy([]) == [] + end + end +end diff --git a/test/http/request_builder_test.exs b/test/http/request/builder_test.exs index f11528c3f..eda87fad1 100644 --- a/test/http/request_builder_test.exs +++ b/test/http/request/builder_test.exs @@ -2,26 +2,26 @@ # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> # SPDX-License-Identifier: AGPL-3.0-only -defmodule Pleroma.HTTP.RequestBuilderTest do +defmodule Pleroma.HTTP.Request.BuilderTest do use ExUnit.Case, async: true use Pleroma.Tests.Helpers alias Pleroma.Config alias Pleroma.HTTP.Request - alias Pleroma.HTTP.RequestBuilder + alias Pleroma.HTTP.Request.Builder describe "headers/2" do setup do: clear_config([:http, :send_user_agent]) setup do: clear_config([:http, :user_agent]) test "don't send pleroma user agent" do - assert RequestBuilder.headers(%Request{}, []) == %Request{headers: []} + assert Builder.headers(%Request{}, []) == %Request{headers: []} end test "send pleroma user agent" do Config.put([:http, :send_user_agent], true) Config.put([:http, :user_agent], :default) - assert RequestBuilder.headers(%Request{}, []) == %Request{ + assert Builder.headers(%Request{}, []) == %Request{ headers: [{"user-agent", Pleroma.Application.user_agent()}] } end @@ -30,7 +30,7 @@ defmodule Pleroma.HTTP.RequestBuilderTest do Config.put([:http, :send_user_agent], true) Config.put([:http, :user_agent], "totally-not-pleroma") - assert RequestBuilder.headers(%Request{}, []) == %Request{ + assert Builder.headers(%Request{}, []) == %Request{ headers: [{"user-agent", "totally-not-pleroma"}] } end @@ -55,7 +55,7 @@ defmodule Pleroma.HTTP.RequestBuilderTest do } ] } - } = RequestBuilder.add_param(%Request{}, :file, "filename.png", "some-path/filename.png") + } = Builder.add_param(%Request{}, :file, "filename.png", "some-path/filename.png") end test "add key to body" do @@ -71,17 +71,17 @@ defmodule Pleroma.HTTP.RequestBuilderTest do } ] } - } = RequestBuilder.add_param(%{}, :body, "somekey", "someval") + } = Builder.add_param(%{}, :body, "somekey", "someval") end test "add form parameter" do - assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{ + assert Builder.add_param(%{}, :form, "somename", "someval") == %{ body: %{"somename" => "someval"} } end test "add for location" do - assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{ + assert Builder.add_param(%{}, :some_location, "somekey", "someval") == %{ some_location: [{"somekey", "someval"}] } end |