ruby-changes:73789
From: Jenny <ko1@a...>
Date: Thu, 29 Sep 2022 17:56:51 +0900 (JST)
Subject: [ruby-changes:73789] 17b783ad9e (master): [rubygems/rubygems] Surface entire redirect uri in permanent redirections
https://git.ruby-lang.org/ruby.git/commit/?id=17b783ad9e From 17b783ad9e62070e8636800fe3aa9c5570a65bda Mon Sep 17 00:00:00 2001 From: Jenny Shen <jenny.shen@s...> Date: Fri, 16 Sep 2022 11:37:55 -0400 Subject: [rubygems/rubygems] Surface entire redirect uri in permanent redirections https://github.com/rubygems/rubygems/commit/da7837630b --- lib/rubygems/gemcutter_utilities.rb | 3 +- test/rubygems/test_gem_commands_owner_command.rb | 64 +++++++++++++++++------ test/rubygems/test_gem_commands_push_command.rb | 10 ++-- test/rubygems/test_gem_commands_signin_command.rb | 20 ++++--- test/rubygems/utilities.rb | 51 +++++++++++++----- 5 files changed, 105 insertions(+), 43 deletions(-) diff --git a/lib/rubygems/gemcutter_utilities.rb b/lib/rubygems/gemcutter_utilities.rb index 1112498357..fb7b9149ba 100644 --- a/lib/rubygems/gemcutter_utilities.rb +++ b/lib/rubygems/gemcutter_utilities.rb @@ -213,8 +213,7 @@ module Gem::GemcutterUtilities https://github.com/ruby/ruby/blob/trunk/lib/rubygems/gemcutter_utilities.rb#L213 say clean_text(response.body) end when Net::HTTPPermanentRedirect, Net::HTTPRedirection then - message = "The request has redirected permanently to #{Gem::Uri.parse(response['location']).origin}. " \ - "Please check your defined push host." + message = "The request has redirected permanently to #{response['location']}. Please check your defined push host." message = "#{error_prefix}: #{message}" if error_prefix say clean_text(message) diff --git a/test/rubygems/test_gem_commands_owner_command.rb b/test/rubygems/test_gem_commands_owner_command.rb index 3d0a265eb1..1f03838614 100644 --- a/test/rubygems/test_gem_commands_owner_command.rb +++ b/test/rubygems/test_gem_commands_owner_command.rb @@ -121,17 +121,23 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_owner_command.rb#L121 def test_show_owners_permanent_redirect host = "http://rubygems.example" ENV["RUBYGEMS_HOST"] = host - @stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = ["", 301, "Moved Permanently"] + path = "/api/v1/gems/freewill/owners.yaml" + redirected_uri = "https://rubygems.example#{path}" + + @stub_fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create( + body: "", + code: "301", + msg: "Moved Permanently", + headers: { "location" => redirected_uri } + ) assert_raise Gem::MockGemUi::TermError do use_ui @stub_ui do - Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do - @cmd.show_owners("freewill") - end + @cmd.show_owners("freewill") end end - response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host." + response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host." assert_match response, @stub_ui.output end @@ -178,15 +184,21 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_owner_command.rb#L184 def test_add_owners_permanent_redirect host = "http://rubygems.example" ENV["RUBYGEMS_HOST"] = host - @stub_fetcher.data["#{host}/api/v1/gems/freewill/owners"] = ["", 308, "Permanent Redirect"] + path = "/api/v1/gems/freewill/owners" + redirected_uri = "https://rubygems.example#{path}" + + @stub_fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create( + body: "", + code: "308", + msg: "Permanent Redirect", + headers: { "location" => redirected_uri } + ) use_ui @stub_ui do - Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do - @cmd.add_owners("freewill", ["user-new1@e..."]) - end + @cmd.add_owners("freewill", ["user-new1@e..."]) end - response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host." + response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host." assert_match response, @stub_ui.output end @@ -251,15 +263,37 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_owner_command.rb#L263 def test_remove_owners_permanent_redirect host = "http://rubygems.example" ENV["RUBYGEMS_HOST"] = host - @stub_fetcher.data["#{host}/api/v1/gems/freewill/owners"] = ["", 308, "Permanent Redirect"] + path = "/api/v1/gems/freewill/owners" + redirected_uri = "https://rubygems.example#{path}" + @stub_fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create( + body: "", + code: "308", + msg: "Permanent Redirect", + headers: { "location" => redirected_uri } + ) use_ui @stub_ui do - Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do - @cmd.remove_owners("freewill", ["user-remove1@e..."]) - end + @cmd.remove_owners("freewill", ["user-remove1@e..."]) + end + + response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host." + assert_match response, @stub_ui.output + + path = "/api/v1/gems/freewill/owners" + redirected_uri = "https://rubygems.example#{path}" + + @stub_fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create( + body: "", + code: "308", + msg: "Permanent Redirect", + headers: { "location" => redirected_uri } + ) + + use_ui @stub_ui do + @cmd.add_owners("freewill", ["user-new1@e..."]) end - response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host." + response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host." assert_match response, @stub_ui.output end diff --git a/test/rubygems/test_gem_commands_push_command.rb b/test/rubygems/test_gem_commands_push_command.rb index c58d3f3692..770ba25681 100644 --- a/test/rubygems/test_gem_commands_push_command.rb +++ b/test/rubygems/test_gem_commands_push_command.rb @@ -327,19 +327,17 @@ class TestGemCommandsPushCommand < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_push_command.rb#L327 def test_sending_gem_to_permanent_redirect_host @host = "http://rubygems.example" - @fetcher.data["#{@host}/api/v1/gems"] = ["", 308, "Permanent Redirect"] + redirected_uri = "https://rubygems.example/api/v1/gems" + @fetcher.data["#{@host}/api/v1/gems"] = HTTPResponseFactory.create(body: "", code: 308, msg: "Permanent Redirect", headers: { "Location" => redirected_uri }) assert_raise Gem::MockGemUi::TermError do use_ui @ui do @cmd.instance_variable_set :@host, @host - - Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do - @cmd.send_gem(@path) - end + @cmd.send_gem(@path) end end - response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host." + response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host." assert_match response, @ui.output end diff --git a/test/rubygems/test_gem_commands_signin_command.rb b/test/rubygems/test_gem_commands_signin_command.rb index 940e68b3da..78a5f92d18 100644 --- a/test/rubygems/test_gem_commands_signin_command.rb +++ b/test/rubygems/test_gem_commands_signin_command.rb @@ -73,22 +73,26 @@ class TestGemCommandsSigninCommand < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_signin_command.rb#L73 def test_execute_with_host_permanent_redirect host = "http://rubygems.example/" - ENV["RUBYGEMS_HOST"] = host - data_key = "#{host}/api/v1/api_key" - fetcher = Gem::FakeFetcher.new - fetcher.data[data_key] = ["", 308, "Moved Permanently"] + ENV["RUBYGEMS_HOST"] = host + path = "/api/v1/api_key" + redirected_uri = "http://rubygems.example#{path}" + fetcher = Gem::FakeFetcher.new + fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create( + body: "", + code: "308", + msg: "Permanent Redirect", + headers: { "location" => redirected_uri } + ) Gem::RemoteFetcher.fetcher = fetcher ui = Gem::MockGemUi.new("you@e...\nsecret\n\n\n\n\n\n\n\n\n") assert_raise Gem::MockGemUi::TermError do use_ui ui do - Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do - @cmd.execute - end + @cmd.execute end end - response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host." + response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host." assert_match response, ui.output end 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 https://github.com/ruby/ruby/blob/trunk/test/rubygems/utilities.rb#L61 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 https://github.com/ruby/ruby/blob/trunk/test/rubygems/utilities.rb#L99 # Thanks, FakeWeb! def open_uri_or_path(path) data = find_data(path) - body, code, msg = data - response = Net::HTTPResponse.send(:respo (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/