summaryrefslogtreecommitdiff
path: root/lib/vendor/excon/tests/proxy_tests.rb
blob: b1a5e2f28d73599ee0991296dba4ee0b95a52716 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
Shindo.tests('Excon proxy support') do
  env_init

  tests('proxy configuration') do

    tests('no proxy') do
      tests('connection.data[:proxy]').returns(nil) do
        connection = Excon.new('http://foo.com')
        connection.data[:proxy]
      end
    end

    tests('empty proxy') do
      tests('connection.data[:proxy]').returns(nil) do
        connection = Excon.new('http://foo.com', :proxy => '')
        connection.data[:proxy]
      end
    end

    tests('with fully-specified proxy: https://myproxy.net:8080') do
      connection = nil

      tests('connection.data[:proxy][:host]').returns('myproxy.net') do
        connection = Excon.new('http://foo.com', :proxy => 'https://myproxy.net:8080')
        connection.data[:proxy][:host]
      end

      tests('connection.data[:proxy][:port]').returns(8080) do
        connection.data[:proxy][:port]
      end

      tests('connection.data[:proxy][:scheme]').returns('https') do
        connection.data[:proxy][:scheme]
      end
    end

    tests('with fully-specified Unix socket proxy: unix:///') do
      connection = nil

      tests('connection.data[:proxy][:host]').returns(nil) do
        connection = Excon.new('http://foo.com', :proxy => 'unix:///tmp/myproxy.sock')
        connection.data[:proxy][:host]
      end

      tests('connection.data[:proxy][:port]').returns(nil) do
        connection.data[:proxy][:port]
      end

      tests('connection.data[:proxy][:scheme]').returns('unix') do
        connection.data[:proxy][:scheme]
      end

      tests('connection.data[:proxy][:path]').returns('/tmp/myproxy.sock') do
        connection.data[:proxy][:path]
      end
    end

    def env_proxy_tests(env)
      env_init(env)

      tests('an http connection') do
        connection = nil

        tests('connection.data[:proxy][:host]').returns('myproxy') do
          connection = Excon.new('http://foo.com')
          connection.data[:proxy][:host]
        end

        tests('connection.data[:proxy][:port]').returns(8080) do
          connection.data[:proxy][:port]
        end

        tests('connection.data[:proxy][:scheme]').returns('http') do
          connection.data[:proxy][:scheme]
        end

        tests('with disable_proxy set') do
          connection = nil

          tests('connection.data[:proxy]').returns(nil) do
            connection = Excon.new('http://foo.com', :disable_proxy => true)
            connection.data[:proxy]
          end
        end
      end

      tests('an https connection') do
        connection = nil

        tests('connection.data[:proxy][:host]').returns('mysecureproxy') do
          connection = Excon.new('https://secret.com')
          connection.data[:proxy][:host]
        end

        tests('connection.data[:proxy][:port]').returns(8081) do
          connection.data[:proxy][:port]
        end

        tests('connection.data[:proxy][:scheme]').returns('http') do
          connection.data[:proxy][:scheme]
        end

        tests('with disable_proxy set') do
          connection = nil

          tests('connection.data[:proxy]').returns(nil) do
            connection = Excon.new('https://foo.com', :disable_proxy => true)
            connection.data[:proxy]
          end
        end
      end

      tests('http proxy from the environment overrides config') do
        connection = nil

        tests('connection.data[:proxy][:host]').returns('myproxy') do
          connection = Excon.new('http://foo.com', :proxy => 'http://hard.coded.proxy:6666')
          connection.data[:proxy][:host]
        end

        tests('connection.data[:proxy][:port]').returns(8080) do
          connection.data[:proxy][:port]
        end
      end

      tests('an http connection in no_proxy') do
        tests('connection.data[:proxy]').returns(nil) do
          connection = Excon.new('http://somesubdomain.noproxy')
          connection.data[:proxy]
        end
      end

      tests('an http connection not completely matching no_proxy') do
        tests('connection.data[:proxy][:host]').returns('myproxy') do
          connection = Excon.new('http://noproxy2')
          connection.data[:proxy][:host]
        end
      end

      tests('an http connection with subdomain in no_proxy') do
        tests('connection.data[:proxy]').returns(nil) do
          connection = Excon.new('http://a.subdomain.noproxy2')
          connection.data[:proxy]
        end
      end

      env_restore
    end

    tests('with complete proxy config from the environment') do
      env = {
        'http_proxy' => 'http://myproxy:8080',
        'https_proxy' => 'http://mysecureproxy:8081',
        'no_proxy' => 'noproxy, subdomain.noproxy2'
      }
      tests('lowercase') { env_proxy_tests(env) }
      upperenv = {}
      env.each do |k, v|
        upperenv[k.upcase] = v
      end
      tests('uppercase') { env_proxy_tests(upperenv) }
    end

    tests('with only http_proxy config from the environment') do
      env_init({'http_proxy' => 'http://myproxy:8080' })

      tests('an https connection') do
        connection = nil

        tests('connection.data[:proxy][:host]').returns('myproxy') do
          connection = Excon.new('https://secret.com')
          connection.data[:proxy][:host]
        end

        tests('connection.data[:proxy][:port]').returns(8080) do
          connection.data[:proxy][:port]
        end

        tests('connection.data[:proxy][:scheme]').returns('http') do
          connection.data[:proxy][:scheme]
        end
      end

      env_restore
    end

    tests('with a unix socket proxy config from the environment') do
      env_init({
        'http_proxy' => 'unix:///tmp/myproxy.sock',
      })

      tests('an https connection') do
        connection = nil

        tests('connection.data[:proxy][:host]').returns(nil) do
          connection = Excon.new('https://secret.com')
          connection.data[:proxy][:host]
        end

        tests('connection.data[:proxy][:port]').returns(nil) do
          connection.data[:proxy][:port]
        end

        tests('connection.data[:proxy][:scheme]').returns('unix') do
          connection.data[:proxy][:scheme]
        end

        tests('connection.data[:proxy][:path]').returns('/tmp/myproxy.sock') do
          connection.data[:proxy][:path]
        end
      end

      env_restore
    end

  end

  with_rackup('proxy.ru') do

    tests('http proxying: http://foo.com:8080') do
      response = nil

      tests('response.status').returns(200) do
        connection = Excon.new('http://foo.com:8080', :proxy => 'http://127.0.0.1:9292')
        response = connection.request(:method => :get, :path => '/bar', :query => {:alpha => 'kappa'})

        response.status
      end

      # must be absolute form for proxy requests
      tests('sent Request URI').returns('http://foo.com:8080/bar?alpha=kappa') do
        response.headers['Sent-Request-Uri']
      end

      tests('sent Sent-Host header').returns('foo.com:8080') do
        response.headers['Sent-Host']
      end

      tests('sent Proxy-Connection header').returns('Keep-Alive') do
        response.headers['Sent-Proxy-Connection']
      end

      tests('response.body (proxied content)').returns('proxied content') do
        response.body
      end
    end

    tests('http proxying: http://user:pass@foo.com:8080') do
      response = nil

      tests('response.status').returns(200) do
        connection = Excon.new('http://foo.com:8080', :proxy => 'http://user:pass@127.0.0.1:9292')
        response = connection.request(:method => :get, :path => '/bar', :query => {:alpha => 'kappa'})

        response.status
      end

      # must be absolute form for proxy requests
      tests('sent Request URI').returns('http://foo.com:8080/bar?alpha=kappa') do
        response.headers['Sent-Request-Uri']
      end

      tests('sent Host header').returns('foo.com:8080') do
        response.headers['Sent-Host']
      end

      tests('sent Proxy-Connection header').returns('Keep-Alive') do
        response.headers['Sent-Proxy-Connection']
      end

      tests('response.body (proxied content)').returns('proxied content') do
        response.body
      end
    end

  end

  with_unicorn('proxy.ru', 'unix:///tmp/myproxy.sock') do
    pending if RUBY_PLATFORM == 'java' # need to find suitable server for jruby

    tests('http proxying over unix socket: http://foo.com:8080') do
      response = nil

      tests('response.status').returns(200) do
        connection = Excon.new('http://foo.com:8080', :proxy => 'unix:///tmp/myproxy.sock')
        response = connection.request(:method => :get, :path => '/bar', :query => {:alpha => 'kappa'})

        response.status
      end

      tests('sent Sent-Host header').returns('foo.com:8080') do
        response.headers['Sent-Host']
      end

      tests('sent Proxy-Connection header').returns('Keep-Alive') do
        response.headers['Sent-Proxy-Connection']
      end

      tests('response.body (proxied content)').returns('proxied content') do
        response.body
      end
    end
  end

  env_restore
end