summaryrefslogtreecommitdiff
path: root/spec/unit/core/http_spec.rb
blob: b0158cea462f70830e556341aa1b50bedf6f0f96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true

RSpec.describe ::Spandx::Core::Http do
  subject { described_class.new(retries: 1) }

  describe '#get' do
    context 'when a connection to a host failed previously' do
      let(:down_host) { 'nexus.example.com' }
      let(:up_host) { 'git.example.com' }

      before do
        down_url = "https://#{down_host}/#{SecureRandom.uuid}"
        stub_request(:get, down_url).to_timeout
        subject.get(down_url)
      end

      it 'stops attempting to connect to an unreachable host after `n` failures' do
        another_down_url = "https://#{down_host}/#{SecureRandom.uuid}"
        default_value = SecureRandom.uuid

        stub_request(:get, another_down_url).to_raise(StandardError)

        expect(subject.get(another_down_url, default: default_value)).to eql(default_value)
      end

      it 'continues to connect to hosts that are still up' do
        up_url = "https://#{up_host}/#{SecureRandom.uuid}"
        stub_request(:get, up_url).to_return(status: 200)

        expect(subject.get(up_url)).to be_a(Net::HTTPSuccess)
      end
    end
  end
end