Skip to content

Commit

Permalink
Cleaned up tests a bit, since we use the stubs in such a similar way …
Browse files Browse the repository at this point in the history
…now.

Removed test_zip and test_no_zip since they weren't actually testing anything.
util_stub_connection_for now takes a straight hash.
Conn class now takes a straight payload, since it was never used more than once.
fetch_path now takes an optional head argument. Returns headers if true.
fetch_size is now a beautiful 1 line long.
open_uri_or_path also takes an optional head argument.


git-svn-id: http://rubygems.rubyforge.org/svn/trunk@1819 3d4018f9-ac1a-0410-99e9-8a154d859a19
  • Loading branch information
zenspider committed Jun 24, 2008
1 parent dc68bb5 commit bdc20cd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 84 deletions.
50 changes: 15 additions & 35 deletions lib/rubygems/remote_fetcher.rb
Expand Up @@ -138,49 +138,25 @@ def download(spec, source_uri, install_dir = Gem.dir)
##
# Downloads +uri+ and returns it as a String.

def fetch_path(uri, mtime = nil)
open_uri_or_path(uri, mtime) do |input|
data = input.read
data = Gem.gunzip data if uri.to_s =~ /gz$/
data
end
def fetch_path(uri, mtime = nil, head = false)
data = open_uri_or_path(uri, mtime, head)
data = Gem.gunzip data if uri.to_s =~ /gz$/ and not head
data
rescue FetchError
raise
rescue Timeout::Error
raise FetchError.new('timed out', uri)
rescue IOError, SocketError, SystemCallError => e
raise FetchError.new("#{e.class}: #{e}", uri)
rescue => e
raise FetchError.new("#{e.class}: #{e}", uri)
end

##
# Returns the size of +uri+ in bytes.

def fetch_size(uri)
return File.size(get_file_uri_path(uri)) if file_uri? uri

uri = URI.parse uri unless URI::Generic === uri

raise ArgumentError, 'uri is not an HTTP URI' unless URI::HTTP === uri

response = request uri, Net::HTTP::Head

case response
when Net::HTTPOK then
else
raise FetchError.new("bad response #{response.message} #{response.code}", uri)
end

if response['content-length'] then
return response['content-length'].to_i
else
response = http.get uri.request_uri
return response.body.size
end
def fetch_size(uri) # TODO: phase this out
response = fetch_path(uri, nil, true)

rescue SocketError, SystemCallError, Timeout::Error => e
raise FetchError.new("#{e.message} (#{e.class})\n\tfetching size", uri)
response['content-length'].to_i
end

def escape(str)
Expand Down Expand Up @@ -253,20 +229,24 @@ def connection_for(uri)
# Read the data from the (source based) URI, but if it is a file:// URI,
# read from the filesystem instead.

def open_uri_or_path(uri, last_modified = nil, depth = 0)
def open_uri_or_path(uri, last_modified = nil, head = false, depth = 0)
raise "block is dead" if block_given?

return open(get_file_uri_path(uri)) if file_uri? uri

uri = URI.parse uri unless URI::Generic === uri
raise ArgumentError, 'uri is not an HTTP URI' unless URI::HTTP === uri

response = request uri, Net::HTTP::Get, last_modified
fetch_type = head ? Net::HTTP::Head : Net::HTTP::Get
response = request uri, fetch_type, last_modified

case response
when Net::HTTPOK then
response.body
head ? response : response.body
when Net::HTTPRedirection then
raise FetchError.new('too many redirects', uri) if depth > 10

open_uri_or_path(response['Location'], last_modified, depth + 1)
open_uri_or_path(response['Location'], last_modified, head, depth + 1)
else
raise FetchError.new("bad response #{response.message} #{response.code}", uri)
end
Expand Down
76 changes: 27 additions & 49 deletions test/test_gem_remote_fetcher.rb
Expand Up @@ -12,10 +12,6 @@
require 'rubygems/remote_fetcher'
require 'ostruct'

def o hash
OpenStruct.new(hash)
end

# = Testing Proxy Settings
#
# These tests check the proper proxy server settings by running two
Expand Down Expand Up @@ -155,23 +151,21 @@ def test_fetch_size_bad_uri
def test_fetch_size_socket_error
fetcher = Gem::RemoteFetcher.new nil
def fetcher.connection_for(uri)
raise SocketError
raise SocketError, "tarded"
end

uri = 'http://gems.example.com/yaml'
e = assert_raise Gem::RemoteFetcher::FetchError do
fetcher.fetch_size uri
end

assert_equal "SocketError (SocketError)\n\tfetching size (#{uri})",
e.message
assert_equal "SocketError: tarded (#{uri})", e.message
end

def test_no_proxy
use_ui @ui do
fetcher = Gem::RemoteFetcher.new nil
assert_data_from_server fetcher.fetch_path(@server_uri)
assert_equal SERVER_DATA.size, fetcher.fetch_size(@server_uri)
assert_data_from_server @fetcher.fetch_path(@server_uri)
assert_equal SERVER_DATA.size, @fetcher.fetch_size(@server_uri)
end
end

Expand Down Expand Up @@ -390,7 +384,9 @@ def test_explicit_proxy_with_user_auth_in_env
def test_fetch_path_io_error
fetcher = Gem::RemoteFetcher.new nil

def fetcher.open_uri_or_path(uri, mtime) raise EOFError; end
def fetcher.open_uri_or_path(uri, mtime, i = 1)
raise EOFError
end

e = assert_raise Gem::RemoteFetcher::FetchError do
fetcher.fetch_path 'uri'
Expand All @@ -403,7 +399,9 @@ def fetcher.open_uri_or_path(uri, mtime) raise EOFError; end
def test_fetch_path_socket_error
fetcher = Gem::RemoteFetcher.new nil

def fetcher.open_uri_or_path(uri, mtime) raise SocketError; end
def fetcher.open_uri_or_path(uri, mtime, hate = 2)
raise SocketError
end

e = assert_raise Gem::RemoteFetcher::FetchError do
fetcher.fetch_path 'uri'
Expand All @@ -416,7 +414,7 @@ def fetcher.open_uri_or_path(uri, mtime) raise SocketError; end
def test_fetch_path_system_call_error
fetcher = Gem::RemoteFetcher.new nil

def fetcher.open_uri_or_path(uri, mtime = nil)
def fetcher.open_uri_or_path(uri, mtime = nil, drbrain = 3)
raise Errno::ECONNREFUSED, 'connect(2)'
end

Expand All @@ -432,13 +430,10 @@ def fetcher.open_uri_or_path(uri, mtime = nil)
def test_fetch_path_unmodified
fetcher = Gem::RemoteFetcher.new nil

def fetcher.open_uri_or_path(uri, mtime)
yield StringIO.new('')
def fetcher.open_uri_or_path(uri, mtime, tons = 4)
''
end

util_stub_connection_for o(:request => o(:body => '', :code => 304,
:date => Time.at(0).to_s))

assert_equal '', fetcher.fetch_path(URI.parse(@gem_repo), Time.at(0))
end

Expand Down Expand Up @@ -541,7 +536,7 @@ def conn.request(req)

def test_request
uri = URI.parse "#{@gem_repo}/specs.#{Gem.marshal_version}"
util_stub_connection_for o(:body => :junk, :code => 200)
util_stub_connection_for :body => :junk, :code => 200

response = @fetcher.request uri, Net::HTTP::Get

Expand All @@ -551,7 +546,7 @@ def test_request

def test_request_head
uri = URI.parse "#{@gem_repo}/specs.#{Gem.marshal_version}"
util_stub_connection_for o(:body => '', :code => 200)
util_stub_connection_for :body => '', :code => 200
response = @fetcher.request uri, Net::HTTP::Head

assert_equal 200, response.code
Expand All @@ -560,32 +555,15 @@ def test_request_head

def test_request_unmodifed
uri = URI.parse "#{@gem_repo}/specs.#{Gem.marshal_version}"
conn = util_stub_connection_for o(:body => '', :code => 304)
conn = util_stub_connection_for :body => '', :code => 304

response = @fetcher.request uri, Net::HTTP::Head, Time.now
t = Time.now
response = @fetcher.request uri, Net::HTTP::Head, t

assert_equal 304, response.code
assert_equal '', response.body

assert conn.requests.first['if-modified-since']
end

def test_zip
use_ui @ui do
self.class.enable_zip = true
fetcher = Gem::RemoteFetcher.new nil
assert_equal SERVER_DATA.size, fetcher.fetch_size(@server_uri), "probably not from proxy"
zip_data = fetcher.fetch_path(@server_z_uri)
assert zip_data.size < SERVER_DATA.size, "Zipped data should be smaller"
end
end

def test_no_zip
use_ui @ui do
self.class.enable_zip = false
fetcher = Gem::RemoteFetcher.new nil
assert_error { fetcher.fetch_path(@server_z_uri) }
end
assert_equal t.rfc2822, conn.payload['if-modified-since']
end

def test_yaml_error_on_size
Expand All @@ -596,7 +574,7 @@ def test_yaml_error_on_size
end
end

def util_stub_connection_for *data
def util_stub_connection_for hash
def @fetcher.connection= conn
@conn = conn
end
Expand All @@ -605,7 +583,7 @@ def @fetcher.connection_for uri
@conn
end

@fetcher.connection = Conn.new data
@fetcher.connection = Conn.new OpenStruct.new(hash)
end

def assert_error(exception_class=Exception)
Expand All @@ -627,16 +605,16 @@ def assert_data_from_proxy(data)
end

class Conn
attr_reader :requests
attr_accessor :payload

def initialize(responses)
@responses = responses
@requests = []
def initialize(response)
@response = response
self.payload = nil
end

def request(req)
@requests << req
@responses.shift
self.payload = req
@response
end
end

Expand Down

0 comments on commit bdc20cd

Please sign in to comment.