diff options
Diffstat (limited to 'test/http')
| -rw-r--r-- | test/http/adapter_helper/gun_test.exs | 84 | ||||
| -rw-r--r-- | test/http/adapter_helper/hackney_test.exs | 35 | ||||
| -rw-r--r-- | test/http/adapter_helper_test.exs | 28 | ||||
| -rw-r--r-- | test/http/ex_aws_test.exs | 54 | ||||
| -rw-r--r-- | test/http/request_builder_test.exs | 47 | ||||
| -rw-r--r-- | test/http/tzdata_test.exs | 35 | 
6 files changed, 250 insertions, 33 deletions
diff --git a/test/http/adapter_helper/gun_test.exs b/test/http/adapter_helper/gun_test.exs new file mode 100644 index 000000000..80589c73d --- /dev/null +++ b/test/http/adapter_helper/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.AdapterHelper.GunTest do +  use ExUnit.Case, async: true +  use Pleroma.Tests.Helpers + +  import Mox + +  alias Pleroma.Config +  alias Pleroma.HTTP.AdapterHelper.Gun + +  setup :verify_on_exit! + +  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] +    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] +    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] +    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 "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 +end diff --git a/test/http/adapter_helper/hackney_test.exs b/test/http/adapter_helper/hackney_test.exs new file mode 100644 index 000000000..f2361ff0b --- /dev/null +++ b/test/http/adapter_helper/hackney_test.exs @@ -0,0 +1,35 @@ +# 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.HackneyTest do +  use ExUnit.Case, async: true +  use Pleroma.Tests.Helpers + +  alias Pleroma.HTTP.AdapterHelper.Hackney + +  setup_all do +    uri = URI.parse("http://domain.com") +    {:ok, uri: uri} +  end + +  describe "options/2" do +    setup do: clear_config([:http, :adapter], a: 1, b: 2) + +    test "add proxy and opts from config", %{uri: uri} do +      opts = Hackney.options([proxy: "localhost:8123"], uri) + +      assert opts[:a] == 1 +      assert opts[:b] == 2 +      assert opts[:proxy] == "localhost:8123" +    end + +    test "respect connection opts and no proxy", %{uri: uri} do +      opts = Hackney.options([a: 2, b: 1], uri) + +      assert opts[:a] == 2 +      assert opts[:b] == 1 +      refute Keyword.has_key?(opts, :proxy) +    end +  end +end diff --git a/test/http/adapter_helper_test.exs b/test/http/adapter_helper_test.exs new file mode 100644 index 000000000..24d501ad5 --- /dev/null +++ b/test/http/adapter_helper_test.exs @@ -0,0 +1,28 @@ +# 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/ex_aws_test.exs b/test/http/ex_aws_test.exs new file mode 100644 index 000000000..d0b00ca26 --- /dev/null +++ b/test/http/ex_aws_test.exs @@ -0,0 +1,54 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.HTTP.ExAwsTest do +  use ExUnit.Case + +  import Tesla.Mock +  alias Pleroma.HTTP + +  @url "https://s3.amazonaws.com/test_bucket/test_image.jpg" + +  setup do +    mock(fn +      %{method: :get, url: @url, headers: [{"x-amz-bucket-region", "us-east-1"}]} -> +        %Tesla.Env{ +          status: 200, +          body: "image-content", +          headers: [{"x-amz-bucket-region", "us-east-1"}] +        } + +      %{method: :post, url: @url, body: "image-content-2"} -> +        %Tesla.Env{status: 200, body: "image-content-2"} +    end) + +    :ok +  end + +  describe "request" do +    test "get" do +      assert HTTP.ExAws.request(:get, @url, "", [{"x-amz-bucket-region", "us-east-1"}]) == { +               :ok, +               %{ +                 body: "image-content", +                 headers: [{"x-amz-bucket-region", "us-east-1"}], +                 status_code: 200 +               } +             } +    end + +    test "post" do +      assert HTTP.ExAws.request(:post, @url, "image-content-2", [ +               {"x-amz-bucket-region", "us-east-1"} +             ]) == { +               :ok, +               %{ +                 body: "image-content-2", +                 headers: [], +                 status_code: 200 +               } +             } +    end +  end +end diff --git a/test/http/request_builder_test.exs b/test/http/request_builder_test.exs index 11a9314ae..fab909905 100644 --- a/test/http/request_builder_test.exs +++ b/test/http/request_builder_test.exs @@ -3,57 +3,38 @@  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.HTTP.RequestBuilderTest do -  use ExUnit.Case, async: true +  use ExUnit.Case    use Pleroma.Tests.Helpers +  alias Pleroma.HTTP.Request    alias Pleroma.HTTP.RequestBuilder    describe "headers/2" do -    clear_config([:http, :send_user_agent]) -    clear_config([:http, :user_agent]) -      test "don't send pleroma user agent" do -      assert RequestBuilder.headers(%{}, []) == %{headers: []} +      assert RequestBuilder.headers(%Request{}, []) == %Request{headers: []}      end      test "send pleroma user agent" do -      Pleroma.Config.put([:http, :send_user_agent], true) -      Pleroma.Config.put([:http, :user_agent], :default) +      clear_config([:http, :send_user_agent], true) +      clear_config([:http, :user_agent], :default) -      assert RequestBuilder.headers(%{}, []) == %{ -               headers: [{"User-Agent", Pleroma.Application.user_agent()}] +      assert RequestBuilder.headers(%Request{}, []) == %Request{ +               headers: [{"user-agent", Pleroma.Application.user_agent()}]               }      end      test "send custom user agent" do -      Pleroma.Config.put([:http, :send_user_agent], true) -      Pleroma.Config.put([:http, :user_agent], "totally-not-pleroma") +      clear_config([:http, :send_user_agent], true) +      clear_config([:http, :user_agent], "totally-not-pleroma") -      assert RequestBuilder.headers(%{}, []) == %{ -               headers: [{"User-Agent", "totally-not-pleroma"}] +      assert RequestBuilder.headers(%Request{}, []) == %Request{ +               headers: [{"user-agent", "totally-not-pleroma"}]               }      end    end -  describe "add_optional_params/3" do -    test "don't add if keyword is empty" do -      assert RequestBuilder.add_optional_params(%{}, %{}, []) == %{} -    end - -    test "add query parameter" do -      assert RequestBuilder.add_optional_params( -               %{}, -               %{query: :query, body: :body, another: :val}, -               [ -                 {:query, "param1=val1¶m2=val2"}, -                 {:body, "some body"} -               ] -             ) == %{query: "param1=val1¶m2=val2", body: "some body"} -    end -  end -    describe "add_param/4" do      test "add file parameter" do -      %{ +      %Request{          body: %Tesla.Multipart{            boundary: _,            content_type_params: [], @@ -70,7 +51,7 @@ defmodule Pleroma.HTTP.RequestBuilderTest do              }            ]          } -      } = RequestBuilder.add_param(%{}, :file, "filename.png", "some-path/filename.png") +      } = RequestBuilder.add_param(%Request{}, :file, "filename.png", "some-path/filename.png")      end      test "add key to body" do @@ -82,7 +63,7 @@ defmodule Pleroma.HTTP.RequestBuilderTest do              %Tesla.Multipart.Part{                body: "\"someval\"",                dispositions: [name: "somekey"], -              headers: ["Content-Type": "application/json"] +              headers: [{"content-type", "application/json"}]              }            ]          } diff --git a/test/http/tzdata_test.exs b/test/http/tzdata_test.exs new file mode 100644 index 000000000..3e605d33b --- /dev/null +++ b/test/http/tzdata_test.exs @@ -0,0 +1,35 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.HTTP.TzdataTest do +  use ExUnit.Case + +  import Tesla.Mock +  alias Pleroma.HTTP +  @url "https://data.iana.org/time-zones/tzdata-latest.tar.gz" + +  setup do +    mock(fn +      %{method: :head, url: @url} -> +        %Tesla.Env{status: 200, body: ""} + +      %{method: :get, url: @url} -> +        %Tesla.Env{status: 200, body: "hello"} +    end) + +    :ok +  end + +  describe "head/1" do +    test "returns successfully result" do +      assert HTTP.Tzdata.head(@url, [], []) == {:ok, {200, []}} +    end +  end + +  describe "get/1" do +    test "returns successfully result" do +      assert HTTP.Tzdata.get(@url, [], []) == {:ok, {200, [], "hello"}} +    end +  end +end  | 
