} = Connections.get_state(name)
end
- describe "integration test" do
- @describetag :integration
-
- clear_config(Pleroma.Gun) do
- Pleroma.Config.put(Pleroma.Gun, Pleroma.Gun.API)
- end
-
- test "opens connection and change owner", %{name: name} do
- url = "https://httpbin.org"
- :ok = Conn.open(url, name)
- conn = Connections.checkin(url, name)
-
- pid = Process.whereis(name)
-
- assert :gun.info(conn).owner == pid
- end
-
- test "opens connection and reuse it on next request", %{name: name} do
- url = "http://httpbin.org"
- :ok = Conn.open(url, name)
- Process.sleep(250)
- conn = Connections.checkin(url, name)
-
- assert is_pid(conn)
- assert Process.alive?(conn)
-
- reused_conn = Connections.checkin(url, name)
-
- assert conn == reused_conn
-
- %Connections{
- conns: %{
- "http:httpbin.org:80" => %Conn{
- conn: ^conn,
- gun_state: :up
- }
- }
- } = Connections.get_state(name)
- end
-
- test "opens ssl connection and reuse it on next request", %{name: name} do
- url = "https://httpbin.org"
- :ok = Conn.open(url, name)
- Process.sleep(1_000)
- conn = Connections.checkin(url, name)
-
- assert is_pid(conn)
- assert Process.alive?(conn)
-
- reused_conn = Connections.checkin(url, name)
-
- assert conn == reused_conn
-
- %Connections{
- conns: %{
- "https:httpbin.org:443" => %Conn{
- conn: ^conn,
- gun_state: :up
- }
- }
- } = Connections.get_state(name)
- end
-
- test "remove frequently used and idle", %{name: name} do
- self = self()
- https1 = "https://www.google.com"
- https2 = "https://httpbin.org"
-
- :ok = Conn.open(https1, name)
- :ok = Conn.open(https2, name)
- Process.sleep(1_500)
- conn = Connections.checkin(https1, name)
-
- for _ <- 1..4 do
- Connections.checkin(https2, name)
- end
-
- %Connections{
- conns: %{
- "https:httpbin.org:443" => %Conn{
- conn: _,
- gun_state: :up
- },
- "https:www.google.com:443" => %Conn{
- conn: _,
- gun_state: :up
- }
- }
- } = Connections.get_state(name)
-
- :ok = Connections.checkout(conn, self, name)
- http = "http://httpbin.org"
- Process.sleep(1_000)
- :ok = Conn.open(http, name)
- conn = Connections.checkin(http, name)
-
- %Connections{
- conns: %{
- "http:httpbin.org:80" => %Conn{
- conn: ^conn,
- gun_state: :up
- },
- "https:httpbin.org:443" => %Conn{
- conn: _,
- gun_state: :up
- }
- }
- } = Connections.get_state(name)
- end
-
- test "remove earlier used and idle", %{name: name} do
- self = self()
-
- https1 = "https://www.google.com"
- https2 = "https://httpbin.org"
- :ok = Conn.open(https1, name)
- :ok = Conn.open(https2, name)
- Process.sleep(1_500)
-
- Connections.checkin(https1, name)
- conn = Connections.checkin(https1, name)
-
- Process.sleep(1_000)
- Connections.checkin(https2, name)
- Connections.checkin(https2, name)
-
- %Connections{
- conns: %{
- "https:httpbin.org:443" => %Conn{
- conn: _,
- gun_state: :up
- },
- "https:www.google.com:443" => %Conn{
- conn: ^conn,
- gun_state: :up
- }
- }
- } = Connections.get_state(name)
-
- :ok = Connections.checkout(conn, self, name)
- :ok = Connections.checkout(conn, self, name)
-
- http = "http://httpbin.org"
- :ok = Conn.open(http, name)
- Process.sleep(1_000)
-
- conn = Connections.checkin(http, name)
-
- %Connections{
- conns: %{
- "http:httpbin.org:80" => %Conn{
- conn: ^conn,
- gun_state: :up
- },
- "https:httpbin.org:443" => %Conn{
- conn: _,
- gun_state: :up
- }
- }
- } = Connections.get_state(name)
- end
-
- test "doesn't open new conn on pool overflow", %{name: name} do
- self = self()
-
- https1 = "https://www.google.com"
- https2 = "https://httpbin.org"
- :ok = Conn.open(https1, name)
- :ok = Conn.open(https2, name)
- Process.sleep(1_000)
- Connections.checkin(https1, name)
- conn1 = Connections.checkin(https1, name)
- conn2 = Connections.checkin(https2, name)
-
- %Connections{
- conns: %{
- "https:httpbin.org:443" => %Conn{
- conn: ^conn2,
- gun_state: :up,
- conn_state: :active,
- used_by: [{^self, _}]
- },
- "https:www.google.com:443" => %Conn{
- conn: ^conn1,
- gun_state: :up,
- conn_state: :active,
- used_by: [{^self, _}, {^self, _}]
- }
- }
- } = Connections.get_state(name)
-
- refute Connections.checkin("http://httpbin.org", name)
-
- %Connections{
- conns: %{
- "https:httpbin.org:443" => %Conn{
- conn: ^conn2,
- gun_state: :up,
- conn_state: :active,
- used_by: [{^self, _}]
- },
- "https:www.google.com:443" => %Conn{
- conn: ^conn1,
- gun_state: :up,
- conn_state: :active,
- used_by: [{^self, _}, {^self, _}]
- }
- }
- } = Connections.get_state(name)
- end
-
- test "get idle connection with the smallest crf", %{
- name: name
- } do
- self = self()
-
- https1 = "https://www.google.com"
- https2 = "https://httpbin.org"
-
- :ok = Conn.open(https1, name)
- :ok = Conn.open(https2, name)
- Process.sleep(1_500)
- Connections.checkin(https1, name)
- Connections.checkin(https2, name)
- Connections.checkin(https1, name)
- conn1 = Connections.checkin(https1, name)
- conn2 = Connections.checkin(https2, name)
-
- %Connections{
- conns: %{
- "https:httpbin.org:443" => %Conn{
- conn: ^conn2,
- gun_state: :up,
- conn_state: :active,
- used_by: [{^self, _}, {^self, _}],
- crf: crf2
- },
- "https:www.google.com:443" => %Conn{
- conn: ^conn1,
- gun_state: :up,
- conn_state: :active,
- used_by: [{^self, _}, {^self, _}, {^self, _}],
- crf: crf1
- }
- }
- } = Connections.get_state(name)
-
- assert crf1 > crf2
-
- :ok = Connections.checkout(conn1, self, name)
- :ok = Connections.checkout(conn1, self, name)
- :ok = Connections.checkout(conn1, self, name)
-
- :ok = Connections.checkout(conn2, self, name)
- :ok = Connections.checkout(conn2, self, name)
-
- %Connections{
- conns: %{
- "https:httpbin.org:443" => %Conn{
- conn: ^conn2,
- gun_state: :up,
- conn_state: :idle,
- used_by: []
- },
- "https:www.google.com:443" => %Conn{
- conn: ^conn1,
- gun_state: :up,
- conn_state: :idle,
- used_by: []
- }
- }
- } = Connections.get_state(name)
-
- http = "http://httpbin.org"
- :ok = Conn.open(http, name)
- Process.sleep(1_000)
- conn = Connections.checkin(http, name)
-
- %Connections{
- conns: %{
- "https:www.google.com:443" => %Conn{
- conn: ^conn1,
- gun_state: :up,
- conn_state: :idle,
- used_by: [],
- crf: crf1
- },
- "http:httpbin.org:80" => %Conn{
- conn: ^conn,
- gun_state: :up,
- conn_state: :active,
- used_by: [{^self, _}],
- crf: crf
- }
- }
- } = Connections.get_state(name)
-
- assert crf1 > crf
- end
- end
-
describe "with proxy" do
test "as ip", %{name: name} do
url = "http://proxy-string.com"
+++ /dev/null
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
-# SPDX-License-Identifier: AGPL-3.0-only
-
-defmodule Pleroma.ReverseProxy.Client.TeslaTest do
- use ExUnit.Case
- use Pleroma.Tests.Helpers
- alias Pleroma.ReverseProxy.Client
- @moduletag :integration
-
- clear_config_all(Pleroma.Gun) do
- Pleroma.Config.put(Pleroma.Gun, Pleroma.Gun.API)
- end
-
- setup do
- Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun)
-
- on_exit(fn ->
- Application.put_env(:tesla, :adapter, Tesla.Mock)
- end)
- end
-
- test "get response body stream" do
- {:ok, status, headers, ref} =
- Client.Tesla.request(
- :get,
- "http://httpbin.org/stream-bytes/10",
- [{"accept", "application/octet-stream"}],
- "",
- []
- )
-
- assert status == 200
- assert headers != []
-
- {:ok, response, ref} = Client.Tesla.stream_body(ref)
- check_ref(ref)
- assert is_binary(response)
- assert byte_size(response) == 10
-
- assert :done == Client.Tesla.stream_body(ref)
- assert :ok = Client.Tesla.close(ref)
- end
-
- test "head response" do
- {:ok, status, headers} = Client.Tesla.request(:head, "https://httpbin.org/get", [], "")
-
- assert status == 200
- assert headers != []
- end
-
- test "get error response" do
- {:ok, status, headers, _body} =
- Client.Tesla.request(
- :get,
- "https://httpbin.org/status/500",
- [],
- ""
- )
-
- assert status == 500
- assert headers != []
- end
-
- describe "client error" do
- setup do
- adapter = Application.get_env(:tesla, :adapter)
- Application.put_env(:tesla, :adapter, Tesla.Adapter.Hackney)
-
- on_exit(fn -> Application.put_env(:tesla, :adapter, adapter) end)
- :ok
- end
-
- test "adapter doesn't support reading body in chunks" do
- assert_raise RuntimeError,
- "Elixir.Tesla.Adapter.Hackney doesn't support reading body in chunks",
- fn ->
- Client.Tesla.request(
- :get,
- "http://httpbin.org/stream-bytes/10",
- [{"accept", "application/octet-stream"}],
- ""
- )
- end
- end
- end
-
- defp check_ref(%{pid: pid, stream: stream} = ref) do
- assert is_pid(pid)
- assert is_reference(stream)
- assert ref[:fin]
- end
-end
assert {"content-disposition", "attachment; filename=\"filename.jpg\""} in conn.resp_headers
end
end
-
- describe "tesla client using gun integration" do
- @describetag :integration
-
- clear_config(Pleroma.ReverseProxy.Client) do
- Pleroma.Config.put(Pleroma.ReverseProxy.Client, Pleroma.ReverseProxy.Client.Tesla)
- end
-
- clear_config(Pleroma.Gun) do
- Pleroma.Config.put(Pleroma.Gun, Pleroma.Gun.API)
- end
-
- setup do
- adapter = Application.get_env(:tesla, :adapter)
- Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun)
-
- on_exit(fn ->
- Application.put_env(:tesla, :adapter, adapter)
- end)
- end
-
- test "common", %{conn: conn} do
- conn = ReverseProxy.call(conn, "http://httpbin.org/stream-bytes/10")
- assert byte_size(conn.resp_body) == 10
- assert conn.state == :chunked
- assert conn.status == 200
- end
-
- test "ssl", %{conn: conn} do
- conn = ReverseProxy.call(conn, "https://httpbin.org/stream-bytes/10")
- assert byte_size(conn.resp_body) == 10
- assert conn.state == :chunked
- assert conn.status == 200
- end
-
- test "follow redirects", %{conn: conn} do
- conn = ReverseProxy.call(conn, "https://httpbin.org/redirect/5")
- assert conn.state == :chunked
- assert conn.status == 200
- end
- end
end