summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/rubygems/gemcutter_utilities.rb10
-rw-r--r--test/rubygems/test_gem_commands_owner_command.rb47
-rw-r--r--test/rubygems/test_gem_commands_push_command.rb18
-rw-r--r--test/rubygems/test_gem_commands_signin_command.rb21
4 files changed, 95 insertions, 1 deletions
diff --git a/lib/rubygems/gemcutter_utilities.rb b/lib/rubygems/gemcutter_utilities.rb
index a785159196..1112498357 100644
--- a/lib/rubygems/gemcutter_utilities.rb
+++ b/lib/rubygems/gemcutter_utilities.rb
@@ -201,7 +201,8 @@ module Gem::GemcutterUtilities
# block was given or shows the response body to the user.
#
# If the response was not successful, shows an error to the user including
- # the +error_prefix+ and the response body.
+ # the +error_prefix+ and the response body. If the response was a permanent redirect,
+ # shows an error to the user including the redirect location.
def with_response(response, error_prefix = nil)
case response
@@ -211,6 +212,13 @@ module Gem::GemcutterUtilities
else
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 = "#{error_prefix}: #{message}" if error_prefix
+
+ say clean_text(message)
+ terminate_interaction(ERROR_CODE)
else
message = response.body
message = "#{error_prefix}: #{message}" if error_prefix
diff --git a/test/rubygems/test_gem_commands_owner_command.rb b/test/rubygems/test_gem_commands_owner_command.rb
index ca77041000..3d0a265eb1 100644
--- a/test/rubygems/test_gem_commands_owner_command.rb
+++ b/test/rubygems/test_gem_commands_owner_command.rb
@@ -118,6 +118,23 @@ EOF
assert_match response, @stub_ui.output
end
+ 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"]
+
+ 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
+ end
+ end
+
+ response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host."
+ assert_match response, @stub_ui.output
+ end
+
def test_show_owners_key
response = "- email: user1@example.com\n"
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, "OK"]
@@ -158,6 +175,21 @@ EOF
assert_match response, @stub_ui.output
end
+ 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"]
+
+ use_ui @stub_ui do
+ Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do
+ @cmd.add_owners("freewill", ["user-new1@example.com"])
+ end
+ end
+
+ response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host."
+ assert_match response, @stub_ui.output
+ end
+
def test_add_owner_with_host_option_through_execute
host = "http://rubygems.example"
add_owner_response = "Owner added successfully."
@@ -216,6 +248,21 @@ EOF
assert_match response, @stub_ui.output
end
+ 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"]
+
+ use_ui @stub_ui do
+ Gem::Uri.stub("parse", URI.parse("https://rubygems.example/")) do
+ @cmd.remove_owners("freewill", ["user-remove1@example.com"])
+ end
+ end
+
+ response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host."
+ assert_match response, @stub_ui.output
+ end
+
def test_remove_owners_key
response = "Owner removed successfully."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, "OK"]
diff --git a/test/rubygems/test_gem_commands_push_command.rb b/test/rubygems/test_gem_commands_push_command.rb
index f38a2ae7a1..c58d3f3692 100644
--- a/test/rubygems/test_gem_commands_push_command.rb
+++ b/test/rubygems/test_gem_commands_push_command.rb
@@ -325,6 +325,24 @@ class TestGemCommandsPushCommand < Gem::TestCase
assert_match @response, @ui.output
end
+ def test_sending_gem_to_permanent_redirect_host
+ @host = "http://rubygems.example"
+ @fetcher.data["#{@host}/api/v1/gems"] = ["", 308, "Permanent Redirect"]
+
+ 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
+ end
+ end
+
+ response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host."
+ assert_match response, @ui.output
+ end
+
def test_raises_error_with_no_arguments
def @cmd.sign_in(*); end
assert_raise Gem::CommandLineError do
diff --git a/test/rubygems/test_gem_commands_signin_command.rb b/test/rubygems/test_gem_commands_signin_command.rb
index ce745bff20..940e68b3da 100644
--- a/test/rubygems/test_gem_commands_signin_command.rb
+++ b/test/rubygems/test_gem_commands_signin_command.rb
@@ -71,6 +71,27 @@ class TestGemCommandsSigninCommand < Gem::TestCase
assert_equal api_key, credentials[host]
end
+ 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"]
+ Gem::RemoteFetcher.fetcher = fetcher
+ ui = Gem::MockGemUi.new("you@example.com\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
+ end
+ end
+
+ response = "The request has redirected permanently to https://rubygems.example. Please check your defined push host."
+ assert_match response, ui.output
+ end
+
def test_execute_with_valid_creds_set_for_default_host
util_capture { @cmd.execute }