diff options
Diffstat (limited to 'test/rubygems/utilities.rb')
-rw-r--r-- | test/rubygems/utilities.rb | 141 |
1 files changed, 103 insertions, 38 deletions
diff --git a/test/rubygems/utilities.rb b/test/rubygems/utilities.rb index 613cf6c0ee..357379f88d 100644 --- a/test/rubygems/utilities.rb +++ b/test/rubygems/utilities.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true -require 'tempfile' -require 'rubygems' -require 'rubygems/remote_fetcher' + +require "tempfile" +require "rubygems" +require "rubygems/remote_fetcher" ## # A fake Gem::RemoteFetcher for use in tests or to avoid real live HTTP @@ -39,35 +40,43 @@ class Gem::FakeFetcher end def find_data(path) - return Gem.read_binary path.path if URI === path and 'file' == path.scheme + return Gem.read_binary path.path if Gem::URI === path && path.scheme == "file" - if URI === path and "URI::#{path.scheme.upcase}" != path.class.name + if Gem::URI === path && "Gem::URI::#{path.scheme.upcase}" != path.class.name raise ArgumentError, "mismatch for scheme #{path.scheme} and class #{path.class}" end path = path.to_s @paths << path - raise ArgumentError, 'need full URI' unless path.start_with?("https://", "http://") + raise ArgumentError, "need full Gem::URI" unless path.start_with?("https://", "http://") unless @data.key? path raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path) end - if @data[path].kind_of?(Array) && @data[path].first.kind_of?(Array) + if @data[path].is_a?(Array) @data[path].shift else @data[path] end end + def create_response(uri) + data = find_data(uri) + response = data.respond_to?(:call) ? data.call : data + raise TypeError, "#{response.class} is not a type of Gem::Net::HTTPResponse" unless response.is_a?(Gem::Net::HTTPResponse) + + response + end + def fetch_path(path, mtime = nil, head = false) data = find_data(path) if data.respond_to?(:call) data.call else - if path.to_s.end_with?(".gz") and not data.nil? and not data.empty? + if path.to_s.end_with?(".gz") && !data.nil? && !data.empty? data = Gem::Util.gunzip data end data @@ -76,7 +85,7 @@ class Gem::FakeFetcher def cache_update_path(uri, path = nil, update = true) if data = fetch_path(uri) - File.open(path, 'wb') {|io| io.write data } if path and update + File.open(path, "wb") {|io| io.write data } if path && update data else Gem.read_binary(path) if path @@ -85,32 +94,22 @@ class Gem::FakeFetcher # Thanks, FakeWeb! def open_uri_or_path(path) - data = find_data(path) - body, code, msg = data + find_data(path) - response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg) - response.instance_variable_set(:@body, body) - response.instance_variable_set(:@read, true) - response + create_response(uri) end def request(uri, request_class, last_modified = nil) - data = find_data(uri) - body, code, msg = (data.respond_to?(:call) ? data.call : data) - @last_request = request_class.new uri.request_uri yield @last_request if block_given? - response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg) - response.instance_variable_set(:@body, body) - response.instance_variable_set(:@read, true) - response + create_response(uri) end def pretty_print(q) # :nodoc: - q.group 2, '[FakeFetcher', ']' do + q.group 2, "[FakeFetcher", "]" do q.breakable - q.text 'URIs:' + q.text "URIs:" q.breakable q.pp @data.keys @@ -121,7 +120,7 @@ class Gem::FakeFetcher path = path.to_s @paths << path - raise ArgumentError, 'need full URI' unless path =~ %r{^http://} + raise ArgumentError, "need full URI" unless %r{^http://}.match?(path) unless @data.key? path raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path) @@ -142,7 +141,7 @@ class Gem::FakeFetcher path = File.join path, name - if source_uri =~ /^http/ + if /^http/.match?(source_uri) File.open(path, "wb") do |f| f.write fetch_path(File.join(source_uri, "gems", name)) end @@ -164,6 +163,66 @@ class Gem::FakeFetcher end end +## +# The HTTPResponseFactory allows easy creation of Gem::Net::HTTPResponse instances in RubyGems tests: +# +# Example: +# +# Gem::HTTPResponseFactory.create( +# body: "", +# code: 301, +# msg: "Moved Permanently", +# headers: { "location" => "http://example.com" } +# ) +# + +class Gem::HTTPResponseFactory + def self.create(body:, code:, msg:, headers: {}) + response = Gem::Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg) + response.instance_variable_set(:@body, body) + response.instance_variable_set(:@read, true) + headers.each {|name, value| response[name] = value } + + response + end +end + +## +# A Gem::MockBrowser is used in tests to mock a browser in that it can +# send HTTP requests to the defined URI. +# +# Example: +# +# # Sends a get request to http://localhost:5678 +# Gem::MockBrowser.get Gem::URI("http://localhost:5678") +# +# See RubyGems' tests for more examples of MockBrowser. +# + +class Gem::MockBrowser + def self.options(uri) + options = Gem::Net::HTTP::Options.new(uri) + Gem::Net::HTTP.start(uri.hostname, uri.port) do |http| + http.request(options) + end + end + + def self.get(uri) + get = Gem::Net::HTTP::Get.new(uri) + Gem::Net::HTTP.start(uri.hostname, uri.port) do |http| + http.request(get) + end + end + + def self.post(uri, content_type: "application/x-www-form-urlencoded") + headers = { "content-type" => content_type } if content_type + post = Gem::Net::HTTP::Post.new(uri, headers) + Gem::Net::HTTP.start(uri.hostname, uri.port) do |http| + http.request(post) + end + end +end + # :stopdoc: class Gem::RemoteFetcher def self.fetcher=(fetcher) @@ -288,16 +347,16 @@ class Gem::TestCase::SpecFetcherSetup # Creates a legacy platform spec with the name 'pl' and version 1 def legacy_platform - spec 'pl', 1 do |s| - s.platform = Gem::Platform.new 'i386-linux' - s.instance_variable_set :@original_platform, 'i386-linux' + spec "pl", 1 do |s| + s.platform = Gem::Platform.new "i386-linux" + s.instance_variable_set :@original_platform, "i386-linux" end end def setup_fetcher # :nodoc: - require 'zlib' - require 'socket' - require 'rubygems/remote_fetcher' + require "zlib" + require "socket" + require "rubygems/remote_fetcher" unless @test.fetcher @test.fetcher = Gem::FakeFetcher.new @@ -307,13 +366,14 @@ class Gem::TestCase::SpecFetcherSetup Gem::Specification.reset begin - gem_repo, @test.gem_repo = @test.gem_repo, @repository - @test.uri = URI @repository + gem_repo = @test.gem_repo + @test.gem_repo = @repository + @test.uri = Gem::URI @repository @test.util_setup_spec_fetcher(*@downloaded) ensure @test.gem_repo = gem_repo - @test.uri = URI gem_repo + @test.uri = Gem::URI gem_repo end @gems.each do |spec, gem| @@ -338,7 +398,7 @@ class Gem::TestCase::SpecFetcherSetup end def write_spec(spec) # :nodoc: - File.open spec.spec_file, 'w' do |io| + File.open spec.spec_file, "w" do |io| io.write spec.to_ruby_for_cache end end @@ -350,11 +410,11 @@ end # # This class was added to flush out problems in Rubinius' IO implementation. -class TempIO < Tempfile +class Gem::TempIO < Tempfile ## # Creates a new TempIO that will be initialized to contain +string+. - def initialize(string = '') + def initialize(string = "") super "TempIO" binmode write string @@ -369,3 +429,8 @@ class TempIO < Tempfile Gem.read_binary path end end + +class Gem::TestCase + TempIO = Gem::TempIO + HTTPResponseFactory = Gem::HTTPResponseFactory +end |