tesla_test.exs (2418B)
1 # Pleroma: A lightweight social networking server 2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> 3 # SPDX-License-Identifier: AGPL-3.0-only 4 5 defmodule Pleroma.ReverseProxy.Client.TeslaTest do 6 use ExUnit.Case 7 use Pleroma.Tests.Helpers 8 alias Pleroma.ReverseProxy.Client 9 @moduletag :integration 10 11 clear_config_all(Pleroma.Gun) do 12 Pleroma.Config.put(Pleroma.Gun, Pleroma.Gun.API) 13 end 14 15 setup do 16 Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun) 17 18 on_exit(fn -> 19 Application.put_env(:tesla, :adapter, Tesla.Mock) 20 end) 21 end 22 23 test "get response body stream" do 24 {:ok, status, headers, ref} = 25 Client.Tesla.request( 26 :get, 27 "http://httpbin.org/stream-bytes/10", 28 [{"accept", "application/octet-stream"}], 29 "", 30 [] 31 ) 32 33 assert status == 200 34 assert headers != [] 35 36 {:ok, response, ref} = Client.Tesla.stream_body(ref) 37 check_ref(ref) 38 assert is_binary(response) 39 assert byte_size(response) == 10 40 41 assert :done == Client.Tesla.stream_body(ref) 42 assert :ok = Client.Tesla.close(ref) 43 end 44 45 test "head response" do 46 {:ok, status, headers} = Client.Tesla.request(:head, "https://httpbin.org/get", [], "") 47 48 assert status == 200 49 assert headers != [] 50 end 51 52 test "get error response" do 53 {:ok, status, headers, _body} = 54 Client.Tesla.request( 55 :get, 56 "https://httpbin.org/status/500", 57 [], 58 "" 59 ) 60 61 assert status == 500 62 assert headers != [] 63 end 64 65 describe "client error" do 66 setup do 67 adapter = Application.get_env(:tesla, :adapter) 68 Application.put_env(:tesla, :adapter, Tesla.Adapter.Hackney) 69 70 on_exit(fn -> Application.put_env(:tesla, :adapter, adapter) end) 71 :ok 72 end 73 74 test "adapter doesn't support reading body in chunks" do 75 assert_raise RuntimeError, 76 "Elixir.Tesla.Adapter.Hackney doesn't support reading body in chunks", 77 fn -> 78 Client.Tesla.request( 79 :get, 80 "http://httpbin.org/stream-bytes/10", 81 [{"accept", "application/octet-stream"}], 82 "" 83 ) 84 end 85 end 86 end 87 88 defp check_ref(%{pid: pid, stream: stream} = ref) do 89 assert is_pid(pid) 90 assert is_reference(stream) 91 assert ref[:fin] 92 end 93 end