Project

General

Profile

Feature #9556 ยป add-block-to-http-get.patch

srawlins (Sam Rawlins), 02/23/2014 05:52 AM

View differences:

lib/net/http.rb
451 451
    #
452 452
    #    print Net::HTTP.get('www.example.com', '/index.html')
453 453
    #
454
    def HTTP.get(uri_or_host, path = nil, port = nil)
455
      get_response(uri_or_host, path, port).body
454
    # If called with a block, yields each fragment of the
455
    # entity body in turn as a String as it is read from
456
    # the socket.  Note that in this case, the returned response
457
    # object will *not* contain a (meaningful) body.
458
    #
459
    #   File.open('result.zip', 'wb') {|f|
460
    #     Net::HTTP.get('www.example.com', '/file.zip') do |chunk|
461
    #       f.write chunk
462
    #     end
463
    #   }
464
    def HTTP.get(uri_or_host, path = nil, port = nil, &block)
465
      if block_given?
466
        get_response(uri_or_host, path, port) {|res|
467
          res.read_body &block
468
        }
469
      else
470
        get_response(uri_or_host, path, port).body
471
      end
456 472
    end
457 473

  
458 474
    # Sends a GET request to the target and returns the HTTP response
test/net/http/test_http.rb
196 196
  def test_s_get
197 197
    assert_equal $test_net_http_data,
198 198
        Net::HTTP.get(config('host'), '/', config('port'))
199

  
200
    f = StringIO.new("".force_encoding("ASCII-8BIT"))
201
    Net::HTTP.get(config('host'), '/', config('port')) do |chunk|
202
      f.write chunk
203
    end
204

  
205
    assert_equal $test_net_http_data, f.string
199 206
  end
200 207

  
201 208
  def test_head
202
-