summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHomu <homu@barosl.com>2016-01-21 20:17:57 -0800
committerHomu <homu@barosl.com>2016-01-21 20:17:57 -0800
commita2d40cdde33186ab3bdd42c5080321ae89218a7b (patch)
treec99d6f17fb8444dcbc0b94cd0bda8fa9d012bbd8
parentdd07c9d35d1c8354bb43d82cd2cbca998cdadda1 (diff)
parent53d56c21b1118802c63769eaead844b26d253f85 (diff)
downloadbundler-a2d40cdde33186ab3bdd42c5080321ae89218a7b.tar.gz
Auto merge of #4223 - RochesterinNYC:expand-unit-test-coverage-for-fetcher-index, r=segiddins
Complete unit test coverage for `Bundler::Fetcher::Index`
-rw-r--r--spec/bundler/fetcher/index_spec.rb109
1 files changed, 97 insertions, 12 deletions
diff --git a/spec/bundler/fetcher/index_spec.rb b/spec/bundler/fetcher/index_spec.rb
index 92cad4b6c6..52acd9cebf 100644
--- a/spec/bundler/fetcher/index_spec.rb
+++ b/spec/bundler/fetcher/index_spec.rb
@@ -1,16 +1,101 @@
-require "bundler" # The error lives here
+require "spec_helper"
describe Bundler::Fetcher::Index do
- it "handles Net::HTTPFatalErrors" do
- rubygems = double(:sources => [], "sources=" => [])
- expect(rubygems).to receive(:fetch_all_remote_specs) {
- raise Net::HTTPFatalError.new("nooo", 404)
- }
- allow(Bundler).to receive(:rubygems).and_return(rubygems)
- allow(Bundler).to receive(:ui).and_return(double(:trace => nil))
-
- expect do
- Bundler::Fetcher::Index.new(nil, nil, nil).specs(%w(foo bar))
- end.to raise_error(Bundler::HTTPError)
+ let(:downloader) { nil }
+ let(:remote) { nil }
+ let(:display_uri) { "http://sample_uri.com" }
+ let(:rubygems) { double(:rubygems) }
+ let(:gem_names) { %w(foo bar) }
+
+ subject { described_class.new(downloader, remote, display_uri) }
+
+ before { allow(Bundler).to receive(:rubygems).and_return(rubygems) }
+
+ it "fetches and returns the list of remote specs" do
+ expect(rubygems).to receive(:fetch_all_remote_specs) { nil }
+ subject.specs(gem_names)
+ end
+
+ context "error handling" do
+ shared_examples_for "the error is properly handled" do
+ context "when certificate verify failed" do
+ let(:error_message) { "certificate verify failed" }
+
+ it "should raise a Bundler::Fetcher::CertificateFailureError" do
+ expect { subject.specs(gem_names) }.to raise_error(Bundler::Fetcher::CertificateFailureError,
+ %r{Could not verify the SSL certificate for http://sample_uri.com})
+ end
+ end
+
+ context "when a 401 response occurs" do
+ let(:error_message) { "401" }
+ let(:remote_uri) { double(:remote_uri, :to_s => "http://remote_uri.org") }
+
+ before { allow(subject).to receive(:remote_uri).and_return(remote_uri) }
+
+ it "should raise a Bundler::Fetcher::AuthenticationRequiredError" do
+ expect { subject.specs(gem_names) }.to raise_error(Bundler::Fetcher::AuthenticationRequiredError,
+ %r{Authentication is required for http://remote_uri.org})
+ end
+ end
+
+ context "when a 403 response occurs" do
+ let(:error_message) { "403" }
+ let(:remote_uri) { double(:remote_uri) }
+ let(:remote_uri_string) { "http://remote_uri.org" }
+
+ before do
+ allow(subject).to receive(:remote_uri).and_return(remote_uri)
+ allow(remote_uri).to receive(:userinfo).and_return(userinfo)
+ allow(remote_uri).to receive(:to_s).and_return(remote_uri_string)
+ end
+
+ context "and there was userinfo" do
+ let(:userinfo) { double(:userinfo) }
+
+ it "should raise a Bundler::Fetcher::BadAuthenticationError" do
+ expect { subject.specs(gem_names) }.to raise_error(Bundler::Fetcher::BadAuthenticationError,
+ %r{Bad username or password for http://remote_uri.org})
+ end
+ end
+
+ context "and there was no userinfo" do
+ let(:userinfo) { nil }
+
+ it "should raise a Bundler::Fetcher::AuthenticationRequiredError" do
+ expect { subject.specs(gem_names) }.to raise_error(Bundler::Fetcher::AuthenticationRequiredError,
+ %r{Authentication is required for http://remote_uri.org})
+ end
+ end
+ end
+
+ context "any other message is returned" do
+ let(:error_message) { "You get an error, you get an error!" }
+
+ before { allow(Bundler).to receive(:ui).and_return(double(:trace => nil)) }
+
+ it "should raise a Bundler::HTTPError" do
+ expect { subject.specs(gem_names) }.to raise_error(Bundler::HTTPError, "Could not fetch specs from http://sample_uri.com")
+ end
+ end
+ end
+
+ context "when a Gem::RemoteFetcher::FetchError occurs" do
+ before { allow(rubygems).to receive(:fetch_all_remote_specs) { raise Gem::RemoteFetcher::FetchError.new(error_message, nil) } }
+
+ it_behaves_like "the error is properly handled"
+ end
+
+ context "when a OpenSSL::SSL::SSLError occurs" do
+ before { allow(rubygems).to receive(:fetch_all_remote_specs) { raise OpenSSL::SSL::SSLError.new(error_message) } }
+
+ it_behaves_like "the error is properly handled"
+ end
+
+ context "when a Net::HTTPFatalError occurs" do
+ before { allow(rubygems).to receive(:fetch_all_remote_specs) { raise Net::HTTPFatalError.new(error_message, 404) } }
+
+ it_behaves_like "the error is properly handled"
+ end
end
end