summaryrefslogtreecommitdiff
path: root/test/support/reverse_proxy_client_case.ex
blob: 36df1ed9514a1d4ecea74c3ecd737e5c8245481d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

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 == 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