summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_request_connection_pools.rb
blob: ecd1e9861f6bd9e79de754538529d5ddc93089d0 (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
# frozen_string_literal: true
require 'rubygems/test_case'
require 'rubygems/request'
require 'timeout'

class TestGemRequestConnectionPool < Gem::TestCase
  class FakeHttp
    def initialize *args
    end

    def start
    end
  end

  def setup
    super
    @old_client = Gem::Request::ConnectionPools.client
    Gem::Request::ConnectionPools.client = FakeHttp

    @proxy = URI 'http://proxy.example'
  end

  def teardown
    Gem::Request::ConnectionPools.client = @old_client
    super
  end

  def test_to_proxy_substring
    pools = Gem::Request::ConnectionPools.new nil, []

    env_no_proxy = %w[
      ems.example
    ]

    no_proxy = pools.send :no_proxy?, 'rubygems.example', env_no_proxy

    refute no_proxy, 'mismatch'
  end

  def test_to_proxy_empty_string
    pools = Gem::Request::ConnectionPools.new nil, []

    env_no_proxy = [""]

    no_proxy = pools.send :no_proxy?, 'ems.example', env_no_proxy

    refute no_proxy, 'mismatch'
  end

  def test_checkout_same_connection
    uri = URI.parse('http://example/some_endpoint')

    pools = Gem::Request::ConnectionPools.new nil, []
    pool = pools.pool_for uri
    conn = pool.checkout
    pool.checkin conn

    assert_equal conn, pool.checkout
  end

  def test_to_proxy_eh
    pools = Gem::Request::ConnectionPools.new nil, []

    env_no_proxy = %w[
      1.no-proxy.example
      2.no-proxy.example
    ]

    no_proxy = pools.send :no_proxy?, '2.no-proxy.example', env_no_proxy

    assert no_proxy, 'match'

    no_proxy = pools.send :no_proxy?, 'proxy.example', env_no_proxy

    refute no_proxy, 'mismatch'
  end

  def test_to_proxy_eh_wildcard
    pools = Gem::Request::ConnectionPools.new nil, []

    env_no_proxy = %w[
      .no-proxy.example
    ]

    no_proxy = pools.send :no_proxy?, '2.no-proxy.example', env_no_proxy

    assert no_proxy, 'wildcard matching subdomain'

    no_proxy = pools.send :no_proxy?, 'no-proxy.example', env_no_proxy

    assert no_proxy, 'wildcard matching dotless domain'

    no_proxy = pools.send :no_proxy?, 'proxy.example', env_no_proxy

    refute no_proxy, 'wildcard mismatch'
  end

  def test_net_http_args
    pools = Gem::Request::ConnectionPools.new nil, []

    net_http_args = pools.send :net_http_args, URI('http://example'), nil

    assert_equal ['example', 80], net_http_args
  end

  def test_net_http_args_ipv6
    pools = Gem::Request::ConnectionPools.new nil, []

    net_http_args = pools.send :net_http_args, URI('http://[::1]'), nil

    assert_equal ["::1", 80], net_http_args
  end

  def test_net_http_args_proxy
    pools = Gem::Request::ConnectionPools.new nil, []

    net_http_args = pools.send :net_http_args, URI('http://example'), @proxy

    assert_equal ['example', 80, 'proxy.example', 80, nil, nil], net_http_args
  end

  def test_net_http_args_no_proxy
    orig_no_proxy, ENV['no_proxy'] = ENV['no_proxy'], 'example'

    pools = Gem::Request::ConnectionPools.new nil, []

    net_http_args = pools.send :net_http_args, URI('http://example'), @proxy

    assert_equal ['example', 80, nil, nil], net_http_args

  ensure
    ENV['no_proxy'] = orig_no_proxy
  end

  def test_thread_waits_for_connection
    uri = URI.parse('http://example/some_endpoint')
    pools = Gem::Request::ConnectionPools.new nil, []
    pool  = pools.pool_for uri

    pool.checkout

    Thread.new {
      assert_raises(Timeout::Error) do
        Timeout.timeout(1) do
          pool.checkout
        end
      end
    }.join
  end
end