diff --git a/lib/rubygems/commands/install_command.rb b/lib/rubygems/commands/install_command.rb index 70d32013ba6a..c615f6329e94 100644 --- a/lib/rubygems/commands/install_command.rb +++ b/lib/rubygems/commands/install_command.rb @@ -159,6 +159,10 @@ def execute load_hooks + if ENV["GEM_HOST_API_KEY"] + Gem::RemoteFetcher.fetcher.headers[:authorization] = ENV["GEM_HOST_API_KEY"] + end + exit_code = install_gems show_installed diff --git a/test/rubygems/test_gem_commands_install_command.rb b/test/rubygems/test_gem_commands_install_command.rb index d2ca933a632c..5d0b1c6541d8 100644 --- a/test/rubygems/test_gem_commands_install_command.rb +++ b/test/rubygems/test_gem_commands_install_command.rb @@ -26,6 +26,10 @@ def teardown Gem::Command.build_args = @orig_args File.unlink @gemdeps if File.file? @gemdeps File.unlink "#{@gemdeps}.lock" if File.file? "#{@gemdeps}.lock" + + if defined? Gem::RemoteFetcher + Gem::RemoteFetcher.fetcher = nil + end end def test_execute_exclude_prerelease @@ -737,6 +741,22 @@ def test_execute_remote assert_match "1 gem installed", @ui.output end + def test_execute_remote_gem_host_api_key + Gem::RemoteFetcher.fetcher = @fetcher = Gem::FakeFetcher.new + @a1, @a1_gem = util_gem "a", "1" + util_setup_spec_fetcher @a1 + @fetcher.data["http://gems.example.com/gems/a-1.gem"] = Gem.read_binary(@a1_gem) + + ENV["GEM_HOST_API_KEY"] = "temporary_secret_key" + @cmd.options[:args] = %w[a] + use_ui @stub_ui do + @cmd.execute + end + + assert_match "1 gem installed", @ui.output + assert_equal "temporary_secret_key", @fetcher.headers[:authorization] + end + def test_execute_with_invalid_gem_file FileUtils.touch("a.gem") diff --git a/test/rubygems/utilities.rb b/test/rubygems/utilities.rb index bf601f6fac2d..28b02346a56d 100644 --- a/test/rubygems/utilities.rb +++ b/test/rubygems/utilities.rb @@ -30,11 +30,12 @@ # See RubyGems' tests for more examples of FakeFetcher. class Gem::FakeFetcher - attr_reader :data, :requests + attr_reader :data, :headers, :requests attr_accessor :paths def initialize @data = {} + @headers = {} @paths = [] @requests = [] end