summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_remote_fetcher.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/rubygems/test_gem_remote_fetcher.rb')
-rw-r--r--test/rubygems/test_gem_remote_fetcher.rb194
1 files changed, 149 insertions, 45 deletions
diff --git a/test/rubygems/test_gem_remote_fetcher.rb b/test/rubygems/test_gem_remote_fetcher.rb
index 66fb682c6c..e039bb76bd 100644
--- a/test/rubygems/test_gem_remote_fetcher.rb
+++ b/test/rubygems/test_gem_remote_fetcher.rb
@@ -99,7 +99,7 @@ gems:
# REFACTOR: copied from test_gem_dependency_installer.rb
@gems_dir = File.join @tempdir, 'gems'
- @cache_dir = Gem.cache_dir(@gemhome)
+ @cache_dir = File.join @gemhome, "cache"
FileUtils.mkdir @gems_dir
# TODO: why does the remote fetcher need it written to disk?
@@ -152,7 +152,7 @@ gems:
fetcher.fetch_size 'gems.example.com/yaml'
end
- assert_equal 'uri scheme is invalid', e.message
+ assert_equal 'uri scheme is invalid: nil', e.message
end
def test_fetch_size_socket_error
@@ -209,7 +209,7 @@ gems:
fetcher = util_fuck_with_fetcher a1_data
- a1_cache_gem = Gem.cache_gem(@a1.file_name, @gemhome)
+ a1_cache_gem = @a1.cache_file
assert_equal a1_cache_gem, fetcher.download(@a1, 'http://gems.example.com')
assert_equal("http://gems.example.com/gems/a-1.gem",
fetcher.instance_variable_get(:@test_arg).to_s)
@@ -221,8 +221,7 @@ gems:
inst = Gem::RemoteFetcher.fetcher
- assert_equal Gem.cache_gem(@a1.file_name, @gemhome),
- inst.download(@a1, 'http://gems.example.com')
+ assert_equal @a1.cache_file, inst.download(@a1, 'http://gems.example.com')
end
def test_download_local
@@ -234,8 +233,7 @@ gems:
inst = Gem::RemoteFetcher.fetcher
end
- assert_equal Gem.cache_gem(@a1.file_name, @gemhome),
- inst.download(@a1, local_path)
+ assert_equal @a1.cache_file, inst.download(@a1, local_path)
end
def test_download_local_space
@@ -249,21 +247,19 @@ gems:
inst = Gem::RemoteFetcher.fetcher
end
- assert_equal Gem.cache_gem(@a1.file_name, @gemhome),
- inst.download(@a1, local_path)
+ assert_equal @a1.cache_file, inst.download(@a1, local_path)
end
def test_download_install_dir
- a1_data = nil
- File.open @a1_gem, 'rb' do |fp|
- a1_data = fp.read
+ a1_data = File.open @a1_gem, 'rb' do |fp|
+ fp.read
end
fetcher = util_fuck_with_fetcher a1_data
install_dir = File.join @tempdir, 'more_gems'
- a1_cache_gem = Gem.cache_gem(@a1.file_name, install_dir)
+ a1_cache_gem = File.join install_dir, "cache", @a1.file_name
FileUtils.mkdir_p(File.dirname(a1_cache_gem))
actual = fetcher.download(@a1, 'http://gems.example.com', install_dir)
@@ -279,7 +275,7 @@ gems:
FileUtils.mv @a1_gem, @tempdir
local_path = File.join @tempdir, @a1.file_name
inst = nil
- File.chmod 0555, Gem.cache_dir(@gemhome)
+ FileUtils.chmod 0555, @a1.cache_dir
Dir.chdir @tempdir do
inst = Gem::RemoteFetcher.fetcher
@@ -288,19 +284,20 @@ gems:
assert_equal File.join(@tempdir, @a1.file_name),
inst.download(@a1, local_path)
ensure
- File.chmod 0755, Gem.cache_dir(@gemhome)
+ FileUtils.chmod 0755, @a1.cache_dir
end
def test_download_read_only
- File.chmod 0555, Gem.cache_dir(@gemhome)
- File.chmod 0555, File.join(@gemhome)
+ FileUtils.chmod 0555, @a1.cache_dir
+ FileUtils.chmod 0555, @gemhome
fetcher = util_fuck_with_fetcher File.read(@a1_gem)
fetcher.download(@a1, 'http://gems.example.com')
- assert File.exist?(Gem.cache_gem(@a1.file_name, Gem.user_dir))
+ a1_cache_gem = File.join Gem.user_dir, "cache", @a1.file_name
+ assert File.exist? a1_cache_gem
ensure
- File.chmod 0755, @gemhome
- File.chmod 0755, Gem.cache_dir(@gemhome)
+ FileUtils.chmod 0755, @gemhome
+ FileUtils.chmod 0755, @a1.cache_dir
end
end
@@ -319,7 +316,7 @@ gems:
fetcher = util_fuck_with_fetcher e1_data, :blow_chunks
- e1_cache_gem = Gem.cache_gem(e1.file_name, @gemhome)
+ e1_cache_gem = e1.cache_file
assert_equal e1_cache_gem, fetcher.download(e1, 'http://gems.example.com')
@@ -337,7 +334,7 @@ gems:
inst = Gem::RemoteFetcher.fetcher
end
- cache_path = Gem.cache_gem(@a1.file_name, @gemhome)
+ cache_path = @a1.cache_file
FileUtils.mv local_path, cache_path
gem = Gem::Format.from_file_by_path cache_path
@@ -422,7 +419,7 @@ gems:
def test_fetch_path_gzip
fetcher = Gem::RemoteFetcher.new nil
- def fetcher.open_uri_or_path(uri, mtime, head = nil)
+ def fetcher.fetch_http(uri, mtime, head = nil)
Gem.gzip 'foo'
end
@@ -432,7 +429,7 @@ gems:
def test_fetch_path_gzip_unmodified
fetcher = Gem::RemoteFetcher.new nil
- def fetcher.open_uri_or_path(uri, mtime, head = nil)
+ def fetcher.fetch_http(uri, mtime, head = nil)
nil
end
@@ -442,53 +439,59 @@ gems:
def test_fetch_path_io_error
fetcher = Gem::RemoteFetcher.new nil
- def fetcher.open_uri_or_path(uri, mtime, head = nil)
+ def fetcher.fetch_http(*)
raise EOFError
end
+ url = 'http://example.com/uri'
+
e = assert_raises Gem::RemoteFetcher::FetchError do
- fetcher.fetch_path 'uri'
+ fetcher.fetch_path url
end
- assert_equal 'EOFError: EOFError (uri)', e.message
- assert_equal 'uri', e.uri
+ assert_equal "EOFError: EOFError (#{url})", e.message
+ assert_equal url, e.uri
end
def test_fetch_path_socket_error
fetcher = Gem::RemoteFetcher.new nil
- def fetcher.open_uri_or_path(uri, mtime, head = nil)
+ def fetcher.fetch_http(uri, mtime, head = nil)
raise SocketError
end
+ url = 'http://example.com/uri'
+
e = assert_raises Gem::RemoteFetcher::FetchError do
- fetcher.fetch_path 'uri'
+ fetcher.fetch_path url
end
- assert_equal 'SocketError: SocketError (uri)', e.message
- assert_equal 'uri', e.uri
+ assert_equal "SocketError: SocketError (#{url})", e.message
+ assert_equal url, e.uri
end
def test_fetch_path_system_call_error
fetcher = Gem::RemoteFetcher.new nil
- def fetcher.open_uri_or_path(uri, mtime = nil, head = nil)
+ def fetcher.fetch_http(uri, mtime = nil, head = nil)
raise Errno::ECONNREFUSED, 'connect(2)'
end
+ url = 'http://example.com/uri'
+
e = assert_raises Gem::RemoteFetcher::FetchError do
- fetcher.fetch_path 'uri'
+ fetcher.fetch_path url
end
- assert_match %r|ECONNREFUSED:.*connect\(2\) \(uri\)\z|,
+ assert_match %r|ECONNREFUSED:.*connect\(2\) \(#{Regexp.escape url}\)\z|,
e.message
- assert_equal 'uri', e.uri
+ assert_equal url, e.uri
end
def test_fetch_path_unmodified
fetcher = Gem::RemoteFetcher.new nil
- def fetcher.open_uri_or_path(uri, mtime, head = nil)
+ def fetcher.fetch_http(uri, mtime, head = nil)
nil
end
@@ -551,16 +554,18 @@ gems:
end
end
- def test_open_uri_or_path
+ def test_fetch_http
fetcher = Gem::RemoteFetcher.new nil
+ url = 'http://gems.example.com/redirect'
conn = Object.new
def conn.started?() true end
def conn.request(req)
+ url = 'http://gems.example.com/redirect'
unless defined? @requested then
@requested = true
res = Net::HTTPMovedPermanently.new nil, 301, nil
- res.add_field 'Location', 'http://gems.example.com/real_path'
+ res.add_field 'Location', url
res
else
res = Net::HTTPOK.new nil, 200, nil
@@ -572,19 +577,21 @@ gems:
conn = { "#{Thread.current.object_id}:gems.example.com:80" => conn }
fetcher.instance_variable_set :@connections, conn
- data = fetcher.open_uri_or_path 'http://gems.example.com/redirect'
+ data = fetcher.fetch_http URI.parse(url)
assert_equal 'real_path', data
end
- def test_open_uri_or_path_limited_redirects
+ def test_fetch_http_redirects
fetcher = Gem::RemoteFetcher.new nil
+ url = 'http://gems.example.com/redirect'
conn = Object.new
def conn.started?() true end
def conn.request(req)
+ url = 'http://gems.example.com/redirect'
res = Net::HTTPMovedPermanently.new nil, 301, nil
- res.add_field 'Location', 'http://gems.example.com/redirect'
+ res.add_field 'Location', url
res
end
@@ -592,11 +599,10 @@ gems:
fetcher.instance_variable_set :@connections, conn
e = assert_raises Gem::RemoteFetcher::FetchError do
- fetcher.open_uri_or_path 'http://gems.example.com/redirect'
+ fetcher.fetch_http URI.parse(url)
end
- assert_equal 'too many redirects (http://gems.example.com/redirect)',
- e.message
+ assert_equal "too many redirects (#{url})", e.message
end
def test_request
@@ -631,6 +637,85 @@ gems:
assert_equal t.rfc2822, conn.payload['if-modified-since']
end
+ def test_user_agent
+ ua = @fetcher.user_agent
+
+ assert_match %r%^RubyGems/\S+ \S+ Ruby/\S+ \(.*?\)%, ua
+ assert_match %r%RubyGems/#{Regexp.escape Gem::VERSION}%, ua
+ assert_match %r% #{Regexp.escape Gem::Platform.local.to_s} %, ua
+ assert_match %r%Ruby/#{Regexp.escape RUBY_VERSION}%, ua
+ assert_match %r%\(#{Regexp.escape RUBY_RELEASE_DATE} %, ua
+ end
+
+ def test_user_agent_engine
+ util_save_version
+
+ Object.send :remove_const, :RUBY_ENGINE if defined?(RUBY_ENGINE)
+ Object.send :const_set, :RUBY_ENGINE, 'vroom'
+
+ ua = @fetcher.user_agent
+
+ assert_match %r%\) vroom%, ua
+ ensure
+ util_restore_version
+ end
+
+ def test_user_agent_engine_ruby
+ util_save_version
+
+ Object.send :remove_const, :RUBY_ENGINE if defined?(RUBY_ENGINE)
+ Object.send :const_set, :RUBY_ENGINE, 'ruby'
+
+ ua = @fetcher.user_agent
+
+ assert_match %r%\)%, ua
+ ensure
+ util_restore_version
+ end
+
+ def test_user_agent_patchlevel
+ util_save_version
+
+ Object.send :remove_const, :RUBY_PATCHLEVEL
+ Object.send :const_set, :RUBY_PATCHLEVEL, 5
+
+ ua = @fetcher.user_agent
+
+ assert_match %r% patchlevel 5\)%, ua
+ ensure
+ util_restore_version
+ end
+
+ def test_user_agent_revision
+ util_save_version
+
+ Object.send :remove_const, :RUBY_PATCHLEVEL
+ Object.send :const_set, :RUBY_PATCHLEVEL, -1
+ Object.send :remove_const, :RUBY_REVISION if defined?(RUBY_REVISION)
+ Object.send :const_set, :RUBY_REVISION, 6
+
+ ua = @fetcher.user_agent
+
+ assert_match %r% revision 6\)%, ua
+ assert_match %r%Ruby/#{Regexp.escape RUBY_VERSION}dev%, ua
+ ensure
+ util_restore_version
+ end
+
+ def test_user_agent_revision_missing
+ util_save_version
+
+ Object.send :remove_const, :RUBY_PATCHLEVEL
+ Object.send :const_set, :RUBY_PATCHLEVEL, -1
+ Object.send :remove_const, :RUBY_REVISION if defined?(RUBY_REVISION)
+
+ ua = @fetcher.user_agent
+
+ assert_match %r%\(#{Regexp.escape RUBY_RELEASE_DATE}\)%, ua
+ ensure
+ util_restore_version
+ end
+
def test_yaml_error_on_size
use_ui @ui do
self.class.enable_yaml = false
@@ -753,5 +838,24 @@ gems:
assert_equal "/home/skillet", @fetcher.correct_for_windows_path(path)
end
+ def util_save_version
+ @orig_RUBY_ENGINE = RUBY_ENGINE if defined? RUBY_ENGINE
+ @orig_RUBY_PATCHLEVEL = RUBY_PATCHLEVEL
+ @orig_RUBY_REVISION = RUBY_REVISION if defined? RUBY_REVISION
+ end
+
+ def util_restore_version
+ Object.send :remove_const, :RUBY_ENGINE if defined?(RUBY_ENGINE)
+ Object.send :const_set, :RUBY_ENGINE, @orig_RUBY_ENGINE if
+ defined?(@orig_RUBY_ENGINE)
+
+ Object.send :remove_const, :RUBY_PATCHLEVEL
+ Object.send :const_set, :RUBY_PATCHLEVEL, @orig_RUBY_PATCHLEVEL
+
+ Object.send :remove_const, :RUBY_REVISION if defined?(RUBY_REVISION)
+ Object.send :const_set, :RUBY_REVISION, @orig_RUBY_REVISION if
+ defined?(@orig_RUBY_REVISION)
+ end
+
end