net-http-persistent 2.4.1 → 2.5

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/History.txt CHANGED
@@ -1,6 +1,23 @@
1
+ === 2.5 / 2012-02-07
2
+
3
+ * Minor enhancements
4
+ * The proxy may be changed at any time.
5
+ * The allowed SSL version may now be set via #ssl_version.
6
+ Issue #16 by astera
7
+ * Added Net::HTTP::Persistent#override_headers which allows overriding
8
+ * Net::HTTP default headers like User-Agent. See
9
+ Net::HTTP::Persistent@Headers for details. Issue #17 by andkerosine
10
+
11
+ * Bug fixes
12
+ * The ruby 1.8 speed monkeypatch now handles EAGAIN for windows users.
13
+ Issue #12 by Alwyn Schoeman
14
+ * Fixed POST example in README. Submitted by injekt.
15
+ * Fixed various bugs in the shutdown of connections especially cross-thread
16
+ (which you shouldn't be doing anyways).
17
+
1
18
  === 2.4.1 / 2012-02-03
2
19
 
3
- * Bug fix
20
+ * Bug fixes
4
21
  * When FakeWeb or WebMock are loaded SSL sessions will not be reused to
5
22
  prevent breakage of testing frameworks. Issue #13 by Matt Brictson, pull
6
23
  request #14 by Zachary Scott
@@ -10,7 +27,7 @@
10
27
 
11
28
  === 2.4 / 2012-01-31
12
29
 
13
- * Minor Enhancement
30
+ * Minor enhancement
14
31
  * net-http-persistent now complains if OpenSSL::SSL::VERIFY_PEER is equal to
15
32
  OpenSSL::SSL::VERIFY_NONE. If you have a platform that is broken this way
16
33
  you must define the constant:
data/README.rdoc CHANGED
@@ -29,21 +29,21 @@ The following example will make two requests to the same server. The
29
29
  connection is kept alive between requests:
30
30
 
31
31
  require 'net/http/persistent'
32
-
32
+
33
33
  uri = URI 'http://example.com/awesome/web/service'
34
-
34
+
35
35
  http = Net::HTTP::Persistent.new 'my_app_name'
36
-
36
+
37
37
  # perform a GET
38
38
  response = http.request uri
39
-
39
+
40
40
  # create a POST
41
41
  post_uri = uri + 'create'
42
42
  post = Net::HTTP::Post.new post_uri.path
43
43
  post.set_form_data 'some' => 'cool data'
44
-
44
+
45
45
  # perform the POST, the URI is always required
46
- response http.request post_uri, post
46
+ response = http.request post_uri, post
47
47
 
48
48
  Please see the documentation for +Net::HTTP::Persistent+ for more information,
49
49
  including how to handle SSL connections.
@@ -13,7 +13,7 @@ class Net::BufferedIO #:nodoc:
13
13
  if @io.respond_to? :read_nonblock then
14
14
  begin
15
15
  @rbuf << @io.read_nonblock(65536)
16
- rescue Errno::EWOULDBLOCK => e
16
+ rescue Errno::EWOULDBLOCK, Errno::EAGAIN => e
17
17
  retry if IO.select [@io], nil, nil, @read_timeout
18
18
  raise Timeout::Error, e.message
19
19
  end
@@ -60,14 +60,28 @@ end
60
60
  # #private_key :: The client's SSL private key
61
61
  # #reuse_ssl_sessions :: Reuse a previously opened SSL session for a new
62
62
  # connection
63
+ # #ssl_version :: Which specific SSL version to use
63
64
  # #verify_callback :: For server certificate verification
64
65
  # #verify_mode :: How connections should be verified
65
66
  #
66
67
  # == Proxies
67
68
  #
68
- # A proxy can be used either by providing a URI as the second argument of
69
- # ::new or by giving <code>:ENV</code> as the second argument which will
70
- # consult environment variables. See proxy_from_env for details.
69
+ # A proxy can be set through #proxy= or at initialization time by providing a
70
+ # second argument to ::new. The proxy may be the URI of the proxy server or
71
+ # <code>:ENV</code> which will consult environment variables.
72
+ #
73
+ # See #proxy= and #proxy_from_env for details.
74
+ #
75
+ # == Headers
76
+ #
77
+ # Headers may be specified for use in every request. #headers are appended to
78
+ # any headers on the request. #override_headers replace existing headers on
79
+ # the request.
80
+ #
81
+ # The difference between the two can be seen in setting the User-Agent. Using
82
+ # <code>http.headers['User-Agent'] = 'MyUserAgent'</code> will send "Ruby,
83
+ # MyUserAgent" while <code>http.override_headers['User-Agent'] =
84
+ # 'MyUserAgent'</code> will send "MyUserAgent".
71
85
  #
72
86
  # == Tuning
73
87
  #
@@ -96,6 +110,14 @@ end
96
110
  # The amount of time to wait for a connection to be opened. Set through
97
111
  # #open_timeout.
98
112
  #
113
+ # === Idle Timeout
114
+ #
115
+ # If a connection has not been used in this many seconds it will be reset when
116
+ # a request would use the connection. The default idle timeout is unlimited.
117
+ # If you know the server's idle timeout setting this value will eliminate
118
+ # failures from attempting non-idempotent requests on closed connections. Set
119
+ # through #idle_timeout.
120
+ #
99
121
  # === Socket Options
100
122
  #
101
123
  # Socket options may be set on newly-created connections. See #socket_options
@@ -107,8 +129,9 @@ end
107
129
  # setting retry_change_requests to true requests will automatically be retried
108
130
  # once.
109
131
  #
110
- # Only do this when you know that retrying a POST is safe for your
111
- # application and will not create duplicate resources.
132
+ # Only do this when you know that retrying a POST or other non-idempotent
133
+ # request is safe for your application and will not create duplicate
134
+ # resources.
112
135
  #
113
136
  # The recommended way to handle non-idempotent requests is the following:
114
137
  #
@@ -149,7 +172,7 @@ class Net::HTTP::Persistent
149
172
  ##
150
173
  # The version of Net::HTTP::Persistent you are using
151
174
 
152
- VERSION = '2.4.1'
175
+ VERSION = '2.5'
153
176
 
154
177
  ##
155
178
  # Error class for errors raised by Net::HTTP::Persistent. Various
@@ -175,11 +198,6 @@ class Net::HTTP::Persistent
175
198
 
176
199
  attr_reader :cert_store
177
200
 
178
- ##
179
- # Where this instance's connections live in the thread local variables
180
-
181
- attr_reader :connection_key # :nodoc:
182
-
183
201
  ##
184
202
  # Sends debug_output to this IO via Net::HTTP#set_debug_output.
185
203
  #
@@ -189,7 +207,17 @@ class Net::HTTP::Persistent
189
207
  attr_accessor :debug_output
190
208
 
191
209
  ##
192
- # Headers that are added to every request
210
+ # Current connection generation
211
+
212
+ attr_reader :generation # :nodoc:
213
+
214
+ ##
215
+ # Where this instance's connections live in the thread local variables
216
+
217
+ attr_reader :generation_key # :nodoc:
218
+
219
+ ##
220
+ # Headers that are added to every request using Net::HTTP#add_field
193
221
 
194
222
  attr_reader :headers
195
223
 
@@ -227,6 +255,11 @@ class Net::HTTP::Persistent
227
255
 
228
256
  attr_accessor :open_timeout
229
257
 
258
+ ##
259
+ # Headers that are added to every request using Net::HTTP#[]=
260
+
261
+ attr_reader :override_headers
262
+
230
263
  ##
231
264
  # This client's SSL private key
232
265
 
@@ -267,11 +300,24 @@ class Net::HTTP::Persistent
267
300
 
268
301
  attr_reader :socket_options
269
302
 
303
+ ##
304
+ # Current SSL connection generation
305
+
306
+ attr_reader :ssl_generation # :nodoc:
307
+
270
308
  ##
271
309
  # Where this instance's SSL connections live in the thread local variables
272
310
 
273
311
  attr_reader :ssl_generation_key # :nodoc:
274
312
 
313
+ ##
314
+ # SSL version to use.
315
+ #
316
+ # By default, the version will be negotiated automatically between client
317
+ # and server. Ruby 1.9 and newer only.
318
+
319
+ attr_reader :ssl_version if RUBY_VERSION > '1.9'
320
+
275
321
  ##
276
322
  # Where this instance's last-use times live in the thread local variables
277
323
 
@@ -304,8 +350,6 @@ class Net::HTTP::Persistent
304
350
 
305
351
  attr_accessor :retry_change_requests
306
352
 
307
- attr_reader :ssl_generation # :nodoc:
308
-
309
353
  ##
310
354
  # Creates a new Net::HTTP::Persistent.
311
355
  #
@@ -326,38 +370,22 @@ class Net::HTTP::Persistent
326
370
  def initialize name = nil, proxy = nil
327
371
  @name = name
328
372
 
329
- @proxy_uri = case proxy
330
- when :ENV then proxy_from_env
331
- when URI::HTTP then proxy
332
- when nil then # ignore
333
- else raise ArgumentError, 'proxy must be :ENV or a URI::HTTP'
334
- end
335
-
336
- if @proxy_uri then
337
- @proxy_args = [
338
- @proxy_uri.host,
339
- @proxy_uri.port,
340
- @proxy_uri.user,
341
- @proxy_uri.password,
342
- ]
343
-
344
- @proxy_connection_id = [nil, *@proxy_args].join ':'
345
- end
346
-
347
- @debug_output = nil
348
- @headers = {}
349
- @http_versions = {}
350
- @keep_alive = 30
351
- @open_timeout = nil
352
- @read_timeout = nil
353
- @idle_timeout = 5
354
- @socket_options = []
373
+ @debug_output = nil
374
+ @proxy_uri = nil
375
+ @headers = {}
376
+ @override_headers = {}
377
+ @http_versions = {}
378
+ @keep_alive = 30
379
+ @open_timeout = nil
380
+ @read_timeout = nil
381
+ @idle_timeout = 5
382
+ @socket_options = []
355
383
 
356
384
  @socket_options << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1] if
357
385
  Socket.const_defined? :TCP_NODELAY
358
386
 
359
387
  key = ['net_http_persistent', name].compact
360
- @connection_key = [key, 'connections' ].join('_').intern
388
+ @generation_key = [key, 'generations' ].join('_').intern
361
389
  @ssl_generation_key = [key, 'ssl_generations'].join('_').intern
362
390
  @request_key = [key, 'requests' ].join('_').intern
363
391
  @timeout_key = [key, 'timeouts' ].join('_').intern
@@ -365,14 +393,18 @@ class Net::HTTP::Persistent
365
393
  @certificate = nil
366
394
  @ca_file = nil
367
395
  @private_key = nil
396
+ @ssl_version = nil
368
397
  @verify_callback = nil
369
398
  @verify_mode = OpenSSL::SSL::VERIFY_PEER
370
399
  @cert_store = nil
371
400
 
401
+ @generation = 0 # incremented when proxy URI changes
372
402
  @ssl_generation = 0 # incremented when SSL session variables change
373
403
  @reuse_ssl_sessions = true
374
404
 
375
405
  @retry_change_requests = false
406
+
407
+ self.proxy = proxy if proxy
376
408
  end
377
409
 
378
410
  ##
@@ -403,12 +435,33 @@ class Net::HTTP::Persistent
403
435
  reconnect_ssl
404
436
  end
405
437
 
438
+ ##
439
+ # Finishes all connections on the given +thread+ that were created before
440
+ # the given +generation+ in the threads +generation_key+ list.
441
+ #
442
+ # See #shutdown for a bunch of scary warning about misusing this method.
443
+
444
+ def cleanup(generation, thread = Thread.current,
445
+ generation_key = @generation_key) # :nodoc:
446
+ timeouts = thread[@timeout_key]
447
+
448
+ (0...generation).each do |generation|
449
+ conns = thread[generation_key].delete generation
450
+
451
+ conns.each_value do |conn|
452
+ finish conn, thread
453
+
454
+ timeouts.delete conn.object_id if timeouts
455
+ end if conns
456
+ end
457
+ end
458
+
406
459
  ##
407
460
  # Creates a new connection for +uri+
408
461
 
409
462
  def connection_for uri
463
+ Thread.current[@generation_key] ||= Hash.new { |h,k| h[k] = {} }
410
464
  Thread.current[@ssl_generation_key] ||= Hash.new { |h,k| h[k] = {} }
411
- Thread.current[@connection_key] ||= {}
412
465
  Thread.current[@request_key] ||= Hash.new 0
413
466
  Thread.current[@timeout_key] ||= Hash.new EPOCH
414
467
 
@@ -421,7 +474,11 @@ class Net::HTTP::Persistent
421
474
 
422
475
  connections = Thread.current[@ssl_generation_key][ssl_generation]
423
476
  else
424
- connections = Thread.current[@connection_key]
477
+ generation = @generation
478
+
479
+ cleanup generation
480
+
481
+ connections = Thread.current[@generation_key][generation]
425
482
  end
426
483
 
427
484
  net_http_args = [uri.host, uri.port]
@@ -490,8 +547,10 @@ class Net::HTTP::Persistent
490
547
  ##
491
548
  # Finishes the Net::HTTP +connection+
492
549
 
493
- def finish connection
494
- Thread.current[@request_key].delete connection.object_id
550
+ def finish connection, thread = Thread.current
551
+ if requests = thread[@request_key] then
552
+ requests.delete connection.object_id
553
+ end
495
554
 
496
555
  connection.finish
497
556
  rescue IOError
@@ -603,6 +662,41 @@ class Net::HTTP::Persistent
603
662
  reconnect_ssl
604
663
  end
605
664
 
665
+ ##
666
+ # Sets the proxy server. The +proxy+ may be the URI of the proxy server,
667
+ # the symbol +:ENV+ which will read the proxy from the environment or nil to
668
+ # disable use of a proxy. See #proxy_from_env for details on setting the
669
+ # proxy from the environment.
670
+ #
671
+ # If the proxy URI is set after requests have been made, the next request
672
+ # will shut-down and re-open all connections.
673
+ #
674
+ # If you are making some requests through a proxy and others without a proxy
675
+ # use separate Net::Http::Persistent instances.
676
+
677
+ def proxy= proxy
678
+ @proxy_uri = case proxy
679
+ when :ENV then proxy_from_env
680
+ when URI::HTTP then proxy
681
+ when nil then # ignore
682
+ else raise ArgumentError, 'proxy must be :ENV or a URI::HTTP'
683
+ end
684
+
685
+ if @proxy_uri then
686
+ @proxy_args = [
687
+ @proxy_uri.host,
688
+ @proxy_uri.port,
689
+ @proxy_uri.user,
690
+ @proxy_uri.password,
691
+ ]
692
+
693
+ @proxy_connection_id = [nil, *@proxy_args].join ':'
694
+ end
695
+
696
+ reconnect
697
+ reconnect_ssl
698
+ end
699
+
606
700
  ##
607
701
  # Creates a URI for an HTTP proxy server from ENV variables.
608
702
  #
@@ -612,7 +706,7 @@ class Net::HTTP::Persistent
612
706
  # indicated user and password unless HTTP_PROXY contains either of these in
613
707
  # the URI.
614
708
  #
615
- # For Windows users lowercase ENV variables are preferred over uppercase ENV
709
+ # For Windows users, lowercase ENV variables are preferred over uppercase ENV
616
710
  # variables.
617
711
 
618
712
  def proxy_from_env
@@ -630,6 +724,13 @@ class Net::HTTP::Persistent
630
724
  uri
631
725
  end
632
726
 
727
+ ##
728
+ # Forces reconnection of HTTP connections.
729
+
730
+ def reconnect
731
+ @generation += 1
732
+ end
733
+
633
734
  ##
634
735
  # Forces reconnection of SSL connections.
635
736
 
@@ -671,10 +772,14 @@ class Net::HTTP::Persistent
671
772
 
672
773
  req = Net::HTTP::Get.new uri.request_uri unless req
673
774
 
674
- headers.each do |pair|
775
+ @headers.each do |pair|
675
776
  req.add_field(*pair)
676
777
  end
677
778
 
779
+ @override_headers.each do |name, value|
780
+ req[name] = value
781
+ end
782
+
678
783
  unless req['Connection'] then
679
784
  req.add_field 'Connection', 'keep-alive'
680
785
  req.add_field 'Keep-Alive', @keep_alive
@@ -743,22 +848,14 @@ class Net::HTTP::Persistent
743
848
  # best to call #shutdown in the thread at the appropriate time instead!
744
849
 
745
850
  def shutdown thread = Thread.current
746
- connections = thread[@connection_key]
747
-
748
- connections.each do |_, connection|
749
- begin
750
- connection.finish
751
- rescue IOError
752
- end
753
- end if connections
851
+ generation = reconnect
852
+ cleanup generation, thread, @generation_key
754
853
 
755
854
  ssl_generation = reconnect_ssl
855
+ cleanup ssl_generation, thread, @ssl_generation_key
756
856
 
757
- ssl_cleanup ssl_generation
758
-
759
- thread[@connection_key] = nil
760
- thread[@request_key] = nil
761
- thread[@timeout_key] = nil
857
+ thread[@request_key] = nil
858
+ thread[@timeout_key] = nil
762
859
  end
763
860
 
764
861
  ##
@@ -785,6 +882,8 @@ class Net::HTTP::Persistent
785
882
  def ssl connection
786
883
  connection.use_ssl = true
787
884
 
885
+ connection.ssl_version = @ssl_version if @ssl_version
886
+
788
887
  connection.verify_mode = @verify_mode
789
888
 
790
889
  if OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE and
@@ -838,17 +937,18 @@ application:
838
937
  # Finishes all connections that existed before the given SSL parameter
839
938
  # +generation+.
840
939
 
841
- def ssl_cleanup generation
842
- (0...ssl_generation).each do |generation|
843
- ssl_conns = Thread.current[@ssl_generation_key].delete generation
940
+ def ssl_cleanup generation # :nodoc:
941
+ cleanup generation, Thread.current, @ssl_generation_key
942
+ end
844
943
 
845
- ssl_conns.each_value do |ssl_conn|
846
- finish ssl_conn
944
+ ##
945
+ # SSL version to use
847
946
 
848
- Thread.current[@timeout_key].delete ssl_conn.object_id
849
- end if ssl_conns
850
- end
851
- end
947
+ def ssl_version= ssl_version
948
+ @ssl_version = ssl_version
949
+
950
+ reconnect_ssl
951
+ end if RUBY_VERSION > '1.9'
852
952
 
853
953
  ##
854
954
  # Sets the HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER.
@@ -95,7 +95,7 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
95
95
  raise "#{@uri} is not HTTP" unless @uri.scheme.downcase == 'http'
96
96
 
97
97
  c = BasicConnection.new
98
- conns["#{@uri.host}:#{@uri.port}"] = c
98
+ conns[0]["#{@uri.host}:#{@uri.port}"] = c
99
99
  c
100
100
  end
101
101
 
@@ -117,7 +117,7 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
117
117
  end
118
118
 
119
119
  def conns
120
- Thread.current[@http.connection_key] ||= {}
120
+ Thread.current[@http.generation_key] ||= Hash.new { |h,k| h[k] = {} }
121
121
  end
122
122
 
123
123
  def reqs
@@ -148,14 +148,7 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
148
148
  assert_equal 'name', http.name
149
149
  end
150
150
 
151
- def test_initialize_env
152
- ENV['HTTP_PROXY'] = 'proxy.example'
153
- http = Net::HTTP::Persistent.new nil, :ENV
154
-
155
- assert_equal URI.parse('http://proxy.example'), http.proxy_uri
156
- end
157
-
158
- def test_initialize_uri
151
+ def test_initialize_proxy
159
152
  proxy_uri = URI.parse 'http://proxy.example'
160
153
 
161
154
  http = Net::HTTP::Persistent.new nil, proxy_uri
@@ -197,8 +190,8 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
197
190
  assert_equal 123, c.open_timeout
198
191
  assert_equal 321, c.read_timeout
199
192
 
200
- assert_includes conns.keys, 'example.com:80'
201
- assert_same c, conns['example.com:80']
193
+ assert_includes conns[0].keys, 'example.com:80'
194
+ assert_same c, conns[0]['example.com:80']
202
195
 
203
196
  socket = c.instance_variable_get :@socket
204
197
  expected = if Socket.const_defined? :TCP_NODELAY then
@@ -213,7 +206,7 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
213
206
  def test_connection_for_cached
214
207
  cached = basic_connection
215
208
  cached.start
216
- conns['example.com:80'] = cached
209
+ conns[0]['example.com:80'] = cached
217
210
 
218
211
  c = @http.connection_for @uri
219
212
 
@@ -237,7 +230,7 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
237
230
  assert c.started?
238
231
 
239
232
  assert_includes conns.keys, 'example.com:80'
240
- assert_same c, conns['example.com:80']
233
+ assert_same c, conns[0]['example.com:80']
241
234
 
242
235
  socket = c.instance_variable_get :@socket
243
236
 
@@ -254,8 +247,8 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
254
247
  assert c.started?
255
248
  assert_equal io, c.instance_variable_get(:@debug_output)
256
249
 
257
- assert_includes conns.keys, 'example.com:80'
258
- assert_same c, conns['example.com:80']
250
+ assert_includes conns[0].keys, 'example.com:80'
251
+ assert_same c, conns[0]['example.com:80']
259
252
  end
260
253
 
261
254
  def test_connection_for_finished_ssl
@@ -278,7 +271,7 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
278
271
  cached = basic_connection
279
272
  def cached.start; raise Errno::EHOSTDOWN end
280
273
  def cached.started?; false end
281
- conns['example.com:80'] = cached
274
+ conns[0]['example.com:80'] = cached
282
275
 
283
276
  e = assert_raises Net::HTTP::Persistent::Error do
284
277
  @http.connection_for @uri
@@ -339,16 +332,16 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
339
332
  assert c.started?
340
333
  assert c.proxy?
341
334
 
342
- assert_includes conns.keys,
335
+ assert_includes conns[1].keys,
343
336
  'example.com:80:proxy.example:80:johndoe:muffins'
344
- assert_same c, conns['example.com:80:proxy.example:80:johndoe:muffins']
337
+ assert_same c, conns[1]['example.com:80:proxy.example:80:johndoe:muffins']
345
338
  end
346
339
 
347
340
  def test_connection_for_refused
348
341
  cached = basic_connection
349
342
  def cached.start; raise Errno::ECONNREFUSED end
350
343
  def cached.started?; false end
351
- conns['example.com:80'] = cached
344
+ conns[0]['example.com:80'] = cached
352
345
 
353
346
  e = assert_raises Net::HTTP::Persistent::Error do
354
347
  @http.connection_for @uri
@@ -414,7 +407,7 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
414
407
  cached.start
415
408
  reqs[cached.object_id] = 10
416
409
  touts[cached.object_id] = Time.now - 6
417
- conns['example.com:80'] = cached
410
+ conns[0]['example.com:80'] = cached
418
411
 
419
412
  c = @http.connection_for @uri
420
413
 
@@ -493,6 +486,16 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
493
486
  assert_equal 'https://example', @http.normalize_uri('https://example')
494
487
  end
495
488
 
489
+ def test_override_haeders
490
+ assert_empty @http.override_headers
491
+
492
+ @http.override_headers['User-Agent'] = 'MyCustomAgent'
493
+
494
+ expected = { 'User-Agent' => 'MyCustomAgent' }
495
+
496
+ assert_equal expected, @http.override_headers
497
+ end
498
+
496
499
  def test_pipeline
497
500
  skip 'net-http-pipeline not installed' unless defined?(Net::HTTP::Pipeline)
498
501
 
@@ -519,6 +522,25 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
519
522
  assert_equal 1, @http.ssl_generation
520
523
  end
521
524
 
525
+ def test_proxy_equals_env
526
+ ENV['HTTP_PROXY'] = 'proxy.example'
527
+
528
+ @http.proxy = :ENV
529
+
530
+ assert_equal URI.parse('http://proxy.example'), @http.proxy_uri
531
+
532
+ assert_equal 1, @http.generation, 'generation'
533
+ assert_equal 1, @http.ssl_generation, 'ssl_generation'
534
+ end
535
+
536
+ def test_proxy_equals_uri
537
+ proxy_uri = URI.parse 'http://proxy.example'
538
+
539
+ @http.proxy = proxy_uri
540
+
541
+ assert_equal proxy_uri, @http.proxy_uri
542
+ end
543
+
522
544
  def test_proxy_from_env
523
545
  ENV['HTTP_PROXY'] = 'proxy.example'
524
546
  ENV['HTTP_PROXY_USER'] = 'johndoe'
@@ -559,6 +581,12 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
559
581
  assert_nil uri
560
582
  end
561
583
 
584
+ def test_reconnect
585
+ result = @http.reconnect
586
+
587
+ assert_equal 1, result
588
+ end
589
+
562
590
  def test_reconnect_ssl
563
591
  result = @http.reconnect_ssl
564
592
 
@@ -566,7 +594,8 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
566
594
  end
567
595
 
568
596
  def test_request
569
- @http.headers['user-agent'] = 'test ua'
597
+ @http.override_headers['user-agent'] = 'test ua'
598
+ @http.headers['accept'] = 'text/*'
570
599
  c = connection
571
600
 
572
601
  res = @http.request @uri
@@ -576,9 +605,12 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
576
605
 
577
606
  assert_kind_of Net::HTTP::Get, req
578
607
  assert_equal '/path', req.path
608
+
609
+ assert_equal 'test ua', req['user-agent']
610
+ assert_match %r%text/\*%, req['accept']
611
+
579
612
  assert_equal 'keep-alive', req['connection']
580
613
  assert_equal '30', req['keep-alive']
581
- assert_match %r%test ua%, req['user-agent']
582
614
 
583
615
  assert_in_delta Time.now, touts[c.object_id]
584
616
 
@@ -928,18 +960,21 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
928
960
 
929
961
  @http = orig
930
962
 
931
- assert c.finished?
932
- refute c2.finished?
963
+ assert c.finished?, 'last-generation connection must be finished'
964
+ refute c2.finished?, 'present generation connection must not be finished'
933
965
 
934
- refute_same cs, conns
935
966
  refute_same rs, reqs
936
967
  refute_same ts, touts
937
968
 
969
+ assert_empty conns
970
+ assert_empty ssl_conns
971
+
938
972
  assert_empty reqs
939
973
  assert_empty touts
940
974
  end
941
975
 
942
976
  def test_shutdown_in_all_threads
977
+ conns
943
978
  ssl_conns
944
979
 
945
980
  t = Thread.new do
@@ -959,23 +994,31 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
959
994
 
960
995
  assert_nil @http.shutdown_in_all_threads
961
996
 
962
- assert c.finished?
963
- assert_nil Thread.current[@http.connection_key]
997
+ assert c.finished?, 'connection in same thread must be finished'
998
+
999
+ assert_empty Thread.current[@http.generation_key]
1000
+
964
1001
  assert_nil Thread.current[@http.request_key]
965
1002
 
966
1003
  t.run
967
- assert t.value.finished?
968
- assert_nil t[@http.connection_key]
1004
+ assert t.value.finished?, 'connection in other thread must be finished'
1005
+
1006
+ assert_empty t[@http.generation_key]
1007
+
969
1008
  assert_nil t[@http.request_key]
970
1009
  end
971
1010
 
972
1011
  def test_shutdown_no_connections
1012
+ conns
973
1013
  ssl_conns
974
1014
 
975
1015
  @http.shutdown
976
1016
 
977
- assert_nil Thread.current[@http.connection_key]
1017
+ assert_empty Thread.current[@http.generation_key]
1018
+ assert_empty Thread.current[@http.ssl_generation_key]
1019
+
978
1020
  assert_nil Thread.current[@http.request_key]
1021
+ assert_nil Thread.current[@http.timeout_key]
979
1022
  end
980
1023
 
981
1024
  def test_shutdown_not_started
@@ -984,12 +1027,15 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
984
1027
  c = basic_connection
985
1028
  def c.finish() raise IOError end
986
1029
 
987
- conns["#{@uri.host}:#{@uri.port}"] = c
1030
+ conns[0]["#{@uri.host}:#{@uri.port}"] = c
988
1031
 
989
1032
  @http.shutdown
990
1033
 
991
- assert_nil Thread.current[@http.connection_key]
1034
+ assert_empty Thread.current[@http.generation_key]
1035
+ assert_empty Thread.current[@http.ssl_generation_key]
1036
+
992
1037
  assert_nil Thread.current[@http.request_key]
1038
+ assert_nil Thread.current[@http.timeout_key]
993
1039
  end
994
1040
 
995
1041
  def test_shutdown_ssl
@@ -1003,11 +1049,11 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
1003
1049
  end
1004
1050
 
1005
1051
  def test_shutdown_thread
1006
- ssl_conns
1007
-
1008
1052
  t = Thread.new do
1009
1053
  c = connection
1010
1054
  conns
1055
+ ssl_conns
1056
+
1011
1057
  reqs
1012
1058
 
1013
1059
  Thread.stop
@@ -1025,8 +1071,10 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
1025
1071
 
1026
1072
  t.run
1027
1073
  assert t.value.finished?
1028
- assert_nil t[@http.connection_key]
1074
+ assert_empty t[@http.generation_key]
1075
+ assert_empty t[@http.ssl_generation_key]
1029
1076
  assert_nil t[@http.request_key]
1077
+ assert_nil t[@http.timeout_key]
1030
1078
  end
1031
1079
 
1032
1080
  def test_ssl
@@ -1145,6 +1193,13 @@ class TestNetHttpPersistent < MiniTest::Unit::TestCase
1145
1193
  assert_empty reqs # sanity check, performed by #finish
1146
1194
  end
1147
1195
 
1196
+ def test_ssl_version_equals
1197
+ @http.ssl_version = :ssl_version
1198
+
1199
+ assert_equal :ssl_version, @http.ssl_version
1200
+ assert_equal 1, @http.ssl_generation
1201
+ end if RUBY_VERSION > '1.9'
1202
+
1148
1203
  def test_verify_callback_equals
1149
1204
  @http.verify_callback = :verify_callback
1150
1205
 
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-http-persistent
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 4
9
- - 1
10
- version: 2.4.1
8
+ - 5
9
+ version: "2.5"
11
10
  platform: ruby
12
11
  authors:
13
12
  - Eric Hodel
@@ -36,7 +35,7 @@ cert_chain:
36
35
  YJY7T/W2n+eWy8WuPhzVUkyzguj0bQe27NDeabgCh2mHd4Hynk2AkYh8MQ==
37
36
  -----END CERTIFICATE-----
38
37
 
39
- date: 2012-02-04 00:00:00 Z
38
+ date: 2012-02-07 00:00:00 Z
40
39
  dependencies:
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: minitest
metadata.gz.sig CHANGED
Binary file