Class Gem::FakeFetcher
In: lib/rubygems/test_utilities.rb
Parent: Object

A fake Gem::RemoteFetcher for use in tests or to avoid real live HTTP requests when testing code that uses RubyGems.

Example:

  @fetcher = Gem::FakeFetcher.new
  @fetcher.data['http://gems.example.com/yaml'] = source_index.to_yaml
  Gem::RemoteFetcher.fetcher = @fetcher

  # invoke RubyGems code

  paths = @fetcher.paths
  assert_equal 'http://gems.example.com/yaml', paths.shift
  assert paths.empty?, paths.join(', ')

See RubyGems’ tests for more examples of FakeFetcher.

Methods

Attributes

data  [R] 
last_request  [R] 
paths  [RW] 

Public Class methods

[Source]

    # File lib/rubygems/test_utilities.rb, line 29
29:   def initialize
30:     @data = {}
31:     @paths = []
32:   end

Public Instance methods

[Source]

     # File lib/rubygems/test_utilities.rb, line 99
 99:   def download spec, source_uri, install_dir = Gem.dir
100:     name = spec.file_name
101:     path = Gem.cache_gem(name, install_dir)
102: 
103:     Gem.ensure_gem_subdirectories install_dir
104: 
105:     if source_uri =~ /^http/ then
106:       File.open(path, "wb") do |f|
107:         f.write fetch_path(File.join(source_uri, "gems", name))
108:       end
109:     else
110:       FileUtils.cp source_uri, path
111:     end
112: 
113:     path
114:   end

[Source]

     # File lib/rubygems/test_utilities.rb, line 116
116:   def download_to_cache dependency
117:     found = Gem::SpecFetcher.fetcher.fetch dependency
118: 
119:     return if found.empty?
120: 
121:     spec, source_uri = found.first
122: 
123:     download spec, source_uri
124:   end

[Source]

    # File lib/rubygems/test_utilities.rb, line 46
46:   def fetch_path path, mtime = nil
47:     data = find_data(path)
48: 
49:     if data.respond_to?(:call) then
50:       data.call
51:     else
52:       if path.to_s =~ /gz$/ and not data.nil? and not data.empty? then
53:         data = Gem.gunzip data
54:       end
55: 
56:       data
57:     end
58:   end

[Source]

    # File lib/rubygems/test_utilities.rb, line 84
84:   def fetch_size(path)
85:     path = path.to_s
86:     @paths << path
87: 
88:     raise ArgumentError, 'need full URI' unless path =~ %r'^http://'
89: 
90:     unless @data.key? path then
91:       raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path)
92:     end
93: 
94:     data = @data[path]
95: 
96:     data.respond_to?(:call) ? data.call : data.length
97:   end

[Source]

    # File lib/rubygems/test_utilities.rb, line 34
34:   def find_data(path)
35:     path = path.to_s
36:     @paths << path
37:     raise ArgumentError, 'need full URI' unless path =~ %r'^https?://'
38: 
39:     unless @data.key? path then
40:       raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path)
41:     end
42: 
43:     @data[path]
44:   end

Thanks, FakeWeb!

[Source]

    # File lib/rubygems/test_utilities.rb, line 61
61:   def open_uri_or_path(path)
62:     data = find_data(path)
63:     body, code, msg = data
64: 
65:     response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg)
66:     response.instance_variable_set(:@body, body)
67:     response.instance_variable_set(:@read, true)
68:     response
69:   end

[Source]

    # File lib/rubygems/test_utilities.rb, line 71
71:   def request(uri, request_class, last_modified = nil)
72:     data = find_data(uri)
73:     body, code, msg = data
74: 
75:     @last_request = request_class.new uri.request_uri
76:     yield @last_request if block_given?
77: 
78:     response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg)
79:     response.instance_variable_set(:@body, body)
80:     response.instance_variable_set(:@read, true)
81:     response
82:   end

[Validate]