summaryrefslogtreecommitdiff
path: root/test/pleroma/http
diff options
context:
space:
mode:
Diffstat (limited to 'test/pleroma/http')
-rw-r--r--test/pleroma/http/adapter_helper/gun_test.exs84
-rw-r--r--test/pleroma/http/adapter_helper/hackney_test.exs35
-rw-r--r--test/pleroma/http/adapter_helper_test.exs28
-rw-r--r--test/pleroma/http/ex_aws_test.exs54
-rw-r--r--test/pleroma/http/request_builder_test.exs85
-rw-r--r--test/pleroma/http/tzdata_test.exs35
6 files changed, 321 insertions, 0 deletions
diff --git a/test/pleroma/http/adapter_helper/gun_test.exs b/test/pleroma/http/adapter_helper/gun_test.exs
new file mode 100644
index 000000000..80589c73d
--- /dev/null
+++ b/test/pleroma/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/pleroma/http/adapter_helper/hackney_test.exs b/test/pleroma/http/adapter_helper/hackney_test.exs
new file mode 100644
index 000000000..f2361ff0b
--- /dev/null
+++ b/test/pleroma/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/pleroma/http/adapter_helper_test.exs b/test/pleroma/http/adapter_helper_test.exs
new file mode 100644
index 000000000..24d501ad5
--- /dev/null
+++ b/test/pleroma/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/pleroma/http/ex_aws_test.exs b/test/pleroma/http/ex_aws_test.exs
new file mode 100644
index 000000000..d0b00ca26
--- /dev/null
+++ b/test/pleroma/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/pleroma/http/request_builder_test.exs b/test/pleroma/http/request_builder_test.exs
new file mode 100644
index 000000000..fab909905
--- /dev/null
+++ b/test/pleroma/http/request_builder_test.exs
@@ -0,0 +1,85 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.HTTP.RequestBuilderTest do
+ use ExUnit.Case
+ use Pleroma.Tests.Helpers
+ alias Pleroma.HTTP.Request
+ alias Pleroma.HTTP.RequestBuilder
+
+ describe "headers/2" do
+ test "don't send pleroma user agent" do
+ assert RequestBuilder.headers(%Request{}, []) == %Request{headers: []}
+ end
+
+ test "send pleroma user agent" do
+ clear_config([:http, :send_user_agent], true)
+ clear_config([:http, :user_agent], :default)
+
+ assert RequestBuilder.headers(%Request{}, []) == %Request{
+ headers: [{"user-agent", Pleroma.Application.user_agent()}]
+ }
+ end
+
+ test "send custom user agent" do
+ clear_config([:http, :send_user_agent], true)
+ clear_config([:http, :user_agent], "totally-not-pleroma")
+
+ assert RequestBuilder.headers(%Request{}, []) == %Request{
+ headers: [{"user-agent", "totally-not-pleroma"}]
+ }
+ end
+ end
+
+ describe "add_param/4" do
+ test "add file parameter" do
+ %Request{
+ body: %Tesla.Multipart{
+ boundary: _,
+ content_type_params: [],
+ parts: [
+ %Tesla.Multipart.Part{
+ body: %File.Stream{
+ line_or_bytes: 2048,
+ modes: [:raw, :read_ahead, :read, :binary],
+ path: "some-path/filename.png",
+ raw: true
+ },
+ dispositions: [name: "filename.png", filename: "filename.png"],
+ headers: []
+ }
+ ]
+ }
+ } = RequestBuilder.add_param(%Request{}, :file, "filename.png", "some-path/filename.png")
+ end
+
+ test "add key to body" do
+ %{
+ body: %Tesla.Multipart{
+ boundary: _,
+ content_type_params: [],
+ parts: [
+ %Tesla.Multipart.Part{
+ body: "\"someval\"",
+ dispositions: [name: "somekey"],
+ headers: [{"content-type", "application/json"}]
+ }
+ ]
+ }
+ } = RequestBuilder.add_param(%{}, :body, "somekey", "someval")
+ end
+
+ test "add form parameter" do
+ assert RequestBuilder.add_param(%{}, :form, "somename", "someval") == %{
+ body: %{"somename" => "someval"}
+ }
+ end
+
+ test "add for location" do
+ assert RequestBuilder.add_param(%{}, :some_location, "somekey", "someval") == %{
+ some_location: [{"somekey", "someval"}]
+ }
+ end
+ end
+end
diff --git a/test/pleroma/http/tzdata_test.exs b/test/pleroma/http/tzdata_test.exs
new file mode 100644
index 000000000..3e605d33b
--- /dev/null
+++ b/test/pleroma/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