reverse proxy clients integration tests

tesla reverse proxy client fixes
This commit is contained in:
Alex S 2019-08-09 12:25:58 +03:00
parent 02daf21d78
commit c51aa48e60
5 changed files with 107 additions and 3 deletions

View File

@ -2,7 +2,6 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do
@behaviour Pleroma.ReverseProxy.Client @behaviour Pleroma.ReverseProxy.Client
@adapters [Tesla.Adapter.Gun] @adapters [Tesla.Adapter.Gun]
alias Pleroma.HTTP
def request(method, url, headers, body, opts \\ []) do def request(method, url, headers, body, opts \\ []) do
adapter_opts = adapter_opts =
@ -10,8 +9,16 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do
|> Keyword.put(:chunks_response, true) |> Keyword.put(:chunks_response, true)
with {:ok, response} <- with {:ok, response} <-
HTTP.request(method, url, body, headers, Keyword.put(opts, :adapter, adapter_opts)) do Pleroma.HTTP.request(
{:ok, response.status, response.headers, response.body} method,
url,
body,
headers,
Keyword.put(opts, :adapter, adapter_opts)
) do
if is_map(response.body),
do: {:ok, response.status, response.headers, response.body},
else: {:ok, response.status, response.headers}
else else
{:error, error} -> {:error, error} {:error, error} -> {:error, error}
end end
@ -23,6 +30,7 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do
case read_chunk!(client) do case read_chunk!(client) do
{:fin, body} -> {:ok, body, Map.put(client, :fin, true)} {:fin, body} -> {:ok, body, Map.put(client, :fin, true)}
{:nofin, part} -> {:ok, part, client} {:nofin, part} -> {:ok, part, client}
{:error, error} -> {:error, error}
end end
end end

View File

@ -0,0 +1,7 @@
defmodule Pleroma.ReverseProxy.Client.HackneyTest do
use Pleroma.ReverseProxyClientCase, client: Pleroma.ReverseProxy.Client.Hackney
defp check_ref(ref) do
assert is_reference(ref)
end
end

View File

@ -0,0 +1,9 @@
defmodule Pleroma.ReverseProxy.Client.TeslaTest do
use Pleroma.ReverseProxyClientCase, client: Pleroma.ReverseProxy.Client.Tesla
defp check_ref(%{pid: pid, stream: stream} = ref) do
assert is_pid(pid)
assert is_reference(stream)
assert ref[:fin]
end
end

View File

@ -329,4 +329,8 @@ defmodule Pleroma.ReverseProxyTest do
assert {"content-disposition", "attachment; filename=\"filename.jpg\""} in conn.resp_headers assert {"content-disposition", "attachment; filename=\"filename.jpg\""} in conn.resp_headers
end end
end end
describe "integration tests" do
@describetag :integration
end
end end

View File

@ -0,0 +1,76 @@
defmodule Pleroma.ReverseProxyClientCase do
defmacro __using__(client: client) do
quote do
use ExUnit.Case
@moduletag :integration
@client unquote(client)
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.request(
:get,
"http://httpbin.org/stream-bytes/10",
[{"accept", "application/octet-stream"}],
"",
[]
)
assert status == 200
assert headers != []
{:ok, response, ref} = @client.stream_body(ref)
check_ref(ref)
assert is_binary(response)
assert byte_size(response) == 10
assert :done == @client.stream_body(ref)
end
test "head response" do
{:ok, status, headers} = @client.request(:head, "http://httpbin.org/get", [], "", [])
assert status == 200
assert headers != []
end
test "get error response" do
case @client.request(
:get,
"http://httpbin.org/status/500",
[],
"",
[]
) do
{:ok, status, headers, ref} ->
assert status == 500
assert headers != []
check_ref(ref)
assert :ok = @client.close(ref)
{:ok, status, headers} ->
assert headers != []
end
end
test "head error response" do
{:ok, status, headers} =
@client.request(
:head,
"http://httpbin.org/status/500",
[],
"",
[]
)
assert status == 500
assert headers != []
end
end
end
end