summaryrefslogtreecommitdiff
path: root/test/http/adapter_helper/gun_test.exs
blob: 2e961826ede9fb72fdbb05be211799b70c77d79b (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# 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.Gun.Conn
  alias Pleroma.HTTP.AdapterHelper.Gun
  alias Pleroma.Pool.Connections

  setup :verify_on_exit!

  defp gun_mock(_) do
    gun_mock()
    :ok
  end

  defp gun_mock do
    Pleroma.GunMock
    |> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(1000) end) end)
    |> stub(:await_up, fn _, _ -> {:ok, :http} end)
    |> stub(:set_owner, fn _, _ -> :ok end)
  end

  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]
      assert opts[:tls_opts][:log_level] == :warning
    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]
      assert opts[:tls_opts][:log_level] == :warning
    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]
      assert opts[:tls_opts][:log_level] == :warning
    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 "get conn on next request" do
      gun_mock()
      level = Application.get_env(:logger, :level)
      Logger.configure(level: :debug)
      on_exit(fn -> Logger.configure(level: level) end)
      uri = URI.parse("http://some-domain2.com")

      opts = Gun.options(uri)

      assert opts[:conn] == nil
      assert opts[:close_conn] == nil

      Process.sleep(50)
      opts = Gun.options(uri)

      assert is_pid(opts[:conn])
      assert opts[:close_conn] == false
    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 "default ssl adapter opts with connection" do
      gun_mock()
      uri = URI.parse("https://some-domain.com")

      :ok = Conn.open(uri, :gun_connections)

      opts = Gun.options(uri)

      assert opts[:certificates_verification]
      refute opts[:tls_opts] == []

      assert opts[:close_conn] == false
      assert is_pid(opts[:conn])
    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

  describe "options/1 with receive_conn parameter" do
    setup :gun_mock

    test "receive conn by default" do
      uri = URI.parse("http://another-domain.com")
      :ok = Conn.open(uri, :gun_connections)

      received_opts = Gun.options(uri)
      assert received_opts[:close_conn] == false
      assert is_pid(received_opts[:conn])
    end

    test "don't receive conn if receive_conn is false" do
      uri = URI.parse("http://another-domain.com")
      :ok = Conn.open(uri, :gun_connections)

      opts = [receive_conn: false]
      received_opts = Gun.options(opts, uri)
      assert received_opts[:close_conn] == nil
      assert received_opts[:conn] == nil
    end
  end

  describe "after_request/1" do
    setup :gun_mock

    test "body_as not chunks" do
      uri = URI.parse("http://some-domain.com")
      :ok = Conn.open(uri, :gun_connections)
      opts = Gun.options(uri)
      :ok = Gun.after_request(opts)
      conn = opts[:conn]

      assert %Connections{
               conns: %{
                 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
                   conn: ^conn,
                   conn_state: :idle,
                   used_by: []
                 }
               }
             } = Connections.get_state(:gun_connections)
    end

    test "body_as chunks" do
      uri = URI.parse("http://some-domain.com")
      :ok = Conn.open(uri, :gun_connections)
      opts = Gun.options([body_as: :chunks], uri)
      :ok = Gun.after_request(opts)
      conn = opts[:conn]
      self = self()

      assert %Connections{
               conns: %{
                 "http:some-domain.com:80" => %Pleroma.Gun.Conn{
                   conn: ^conn,
                   conn_state: :active,
                   used_by: [{^self, _}]
                 }
               }
             } = Connections.get_state(:gun_connections)
    end

    test "with no connection" do
      uri = URI.parse("http://uniq-domain.com")

      :ok = Conn.open(uri, :gun_connections)

      opts = Gun.options([body_as: :chunks], uri)
      conn = opts[:conn]
      opts = Keyword.delete(opts, :conn)
      self = self()

      :ok = Gun.after_request(opts)

      assert %Connections{
               conns: %{
                 "http:uniq-domain.com:80" => %Pleroma.Gun.Conn{
                   conn: ^conn,
                   conn_state: :active,
                   used_by: [{^self, _}]
                 }
               }
             } = Connections.get_state(:gun_connections)
    end

    test "with ipv4" do
      uri = URI.parse("http://127.0.0.1")
      :ok = Conn.open(uri, :gun_connections)
      opts = Gun.options(uri)
      :ok = Gun.after_request(opts)
      conn = opts[:conn]

      assert %Connections{
               conns: %{
                 "http:127.0.0.1:80" => %Pleroma.Gun.Conn{
                   conn: ^conn,
                   conn_state: :idle,
                   used_by: []
                 }
               }
             } = Connections.get_state(:gun_connections)
    end

    test "with ipv6" do
      uri = URI.parse("http://[2a03:2880:f10c:83:face:b00c:0:25de]")
      :ok = Conn.open(uri, :gun_connections)
      opts = Gun.options(uri)
      :ok = Gun.after_request(opts)
      conn = opts[:conn]

      assert %Connections{
               conns: %{
                 "http:2a03:2880:f10c:83:face:b00c:0:25de:80" => %Pleroma.Gun.Conn{
                   conn: ^conn,
                   conn_state: :idle,
                   used_by: []
                 }
               }
             } = Connections.get_state(:gun_connections)
    end
  end
end