diff options
author | Jenny Shen <jenny.shen@shopify.com> | 2022-09-16 11:37:55 -0400 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2022-09-29 17:56:35 +0900 |
commit | 17b783ad9e62070e8636800fe3aa9c5570a65bda (patch) | |
tree | af0c225b7a18bb9a0013922821a457a2f7ba884e /test/rubygems/utilities.rb | |
parent | 1cbf0fd86356ccbac5556ab0f63ea8a4b08fd24d (diff) | |
download | ruby-17b783ad9e62070e8636800fe3aa9c5570a65bda.tar.gz |
[rubygems/rubygems] Surface entire redirect uri in permanent redirections
https://github.com/rubygems/rubygems/commit/da7837630b
Diffstat (limited to 'test/rubygems/utilities.rb')
-rw-r--r-- | test/rubygems/utilities.rb | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/test/rubygems/utilities.rb b/test/rubygems/utilities.rb index c01f7acd48..96b197c096 100644 --- a/test/rubygems/utilities.rb +++ b/test/rubygems/utilities.rb @@ -61,6 +61,19 @@ class Gem::FakeFetcher end end + def create_response(uri) + data = find_data(uri) + if data.kind_of?(Array) + body, code, msg = data + HTTPResponseFactory.create(body: body, code: code, msg: msg) + elsif data.respond_to?(:call) + body, code, msg = data.call + HTTPResponseFactory.create(body: body, code: code, msg: msg) + else + data + end + end + def fetch_path(path, mtime = nil, head = false) data = find_data(path) @@ -86,25 +99,15 @@ class Gem::FakeFetcher # Thanks, FakeWeb! def open_uri_or_path(path) data = find_data(path) - body, code, msg = data - 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: @@ -164,6 +167,30 @@ class Gem::FakeFetcher end end +## +# The HTTPResponseFactory allows easy creation of Net::HTTPResponse instances in RubyGems tests: +# +# Example: +# +# HTTPResponseFactory.create( +# body: "", +# code: 301, +# msg: "Moved Permanently", +# headers: { "location" => "http://example.com" } +# ) +# + +class HTTPResponseFactory + def self.create(body:, code:, msg:, headers: {}) + 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) + headers.each {|name, value| response[name] = value } + + response + end +end + # :stopdoc: class Gem::RemoteFetcher def self.fetcher=(fetcher) |