Welcome to mirror list, hosted at ThFree Co, Russian Federation.

http_spec.rb « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c23f4ea8ffaa14163dde993f303d5290bcc686cb (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::HTTP, feature_category: :shared do
  let(:default_options) do
    {
      allow_local_requests: false,
      deny_all_requests_except_allowed: false,
      dns_rebinding_protection_enabled: true,
      outbound_local_requests_allowlist: [],
      silent_mode_enabled: false
    }
  end

  describe '.get' do
    it 'calls Gitlab::HTTP_V2.get with default options' do
      expect(Gitlab::HTTP_V2).to receive(:get).with('/path', default_options)

      described_class.get('/path')
    end

    context 'when passing allow_object_storage:true' do
      before do
        allow(ObjectStoreSettings).to receive(:enabled_endpoint_uris).and_return([URI('http://example.com')])
      end

      it 'calls Gitlab::HTTP_V2.get with default options and extra_allowed_uris' do
        expect(Gitlab::HTTP_V2).to receive(:get)
          .with('/path', default_options.merge(extra_allowed_uris: [URI('http://example.com')]))

        described_class.get('/path', allow_object_storage: true)
      end
    end

    context 'when passing async:true' do
      it 'calls Gitlab::HTTP_V2.get with default options and async:true' do
        expect(Gitlab::HTTP_V2).to receive(:get)
          .with('/path', default_options.merge(async: true))

        described_class.get('/path', async: true)
      end

      it 'returns a Gitlab::HTTP_V2::LazyResponse object' do
        stub_request(:get, 'http://example.org').to_return(status: 200, body: 'hello world')
        result = described_class.get('http://example.org', async: true)

        expect(result).to be_a(Gitlab::HTTP_V2::LazyResponse)

        result.execute
        result.wait

        expect(result.value).to be_a(HTTParty::Response)
        expect(result.value.body).to eq('hello world')
      end

      context 'when there is a DB call in the concurrent thread' do
        before do
          # Simulating Sentry is active and configured.
          # More info: https://gitlab.com/gitlab-org/gitlab/-/issues/432145#note_1671305713
          stub_sentry_settings
          allow(Gitlab::ErrorTracking).to receive(:sentry_configurable?).and_return(true)
          Gitlab::ErrorTracking.configure
        end

        after do
          clear_sentry_settings
        end

        it 'raises Gitlab::Utils::ConcurrentRubyThreadIsUsedError error' do
          stub_request(:get, 'http://example.org').to_return(status: 200, body: 'hello world')

          result = described_class.get('http://example.org', async: true) do |_fragment|
            User.first
          end

          result.execute
          result.wait

          expect { result.value }.to raise_error(Gitlab::Utils::ConcurrentRubyThreadIsUsedError,
            "Cannot run 'db' if running from `Concurrent::Promise`.")
        end
      end
    end
  end

  describe '.try_get' do
    it 'calls .get' do
      expect(described_class).to receive(:get).with('/path', {})

      described_class.try_get('/path')
    end

    it 'returns nil when .get raises an error' do
      expect(described_class).to receive(:get).and_raise(SocketError)

      expect(described_class.try_get('/path')).to be_nil
    end
  end

  describe '.perform_request' do
    context 'when sending a GET request' do
      it 'calls Gitlab::HTTP_V2.get with default options' do
        expect(Gitlab::HTTP_V2).to receive(:get).with('/path', default_options)

        described_class.perform_request(Net::HTTP::Get, '/path', {})
      end
    end

    context 'when sending a LOCK request' do
      it 'raises ArgumentError' do
        expect do
          described_class.perform_request(Net::HTTP::Lock, '/path', {})
        end.to raise_error(ArgumentError, "Unsupported HTTP method: 'lock'.")
      end
    end
  end
end