2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-26 14:24:53 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::HTTP do
|
2019-06-05 12:25:43 +05:30
|
|
|
include StubRequests
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:default_options) { described_class::DEFAULT_TIMEOUT_OPTIONS }
|
|
|
|
|
2019-06-05 12:25:43 +05:30
|
|
|
context 'when allow_local_requests' do
|
|
|
|
it 'sends the request to the correct URI' do
|
|
|
|
stub_full_request('https://example.org:8080', ip_address: '8.8.8.8').to_return(status: 200)
|
|
|
|
|
|
|
|
described_class.get('https://example.org:8080', allow_local_requests: false)
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:get, 'https://8.8.8.8:8080').once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not allow_local_requests' do
|
|
|
|
it 'sends the request to the correct URI' do
|
|
|
|
stub_full_request('https://example.org:8080')
|
|
|
|
|
|
|
|
described_class.get('https://example.org:8080', allow_local_requests: true)
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:get, 'https://8.8.8.9:8080').once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-02 01:05:55 +05:30
|
|
|
context 'when reading the response is too slow' do
|
2022-04-04 11:22:00 +05:30
|
|
|
before(:all) do
|
2022-03-02 08:16:31 +05:30
|
|
|
# Override Net::HTTP to add a delay between sending each response chunk
|
|
|
|
mocked_http = Class.new(Net::HTTP) do
|
|
|
|
def request(*)
|
|
|
|
super do |response|
|
|
|
|
response.instance_eval do
|
|
|
|
def read_body(*)
|
|
|
|
@body.each do |fragment|
|
|
|
|
sleep 0.002.seconds
|
|
|
|
|
|
|
|
yield fragment if block_given?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
yield response if block_given?
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@original_net_http = Net.send(:remove_const, :HTTP)
|
2022-04-04 11:22:00 +05:30
|
|
|
@webmock_net_http = WebMock::HttpLibAdapters::NetHttpAdapter.instance_variable_get('@webMockNetHTTP')
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
Net.send(:const_set, :HTTP, mocked_http)
|
2022-04-04 11:22:00 +05:30
|
|
|
WebMock::HttpLibAdapters::NetHttpAdapter.instance_variable_set('@webMockNetHTTP', mocked_http)
|
|
|
|
|
|
|
|
# Reload Gitlab::NetHttpAdapter
|
|
|
|
Gitlab.send(:remove_const, :NetHttpAdapter)
|
|
|
|
load "#{Rails.root}/lib/gitlab/net_http_adapter.rb"
|
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
before do
|
2021-07-02 01:05:55 +05:30
|
|
|
stub_const("#{described_class}::DEFAULT_READ_TOTAL_TIMEOUT", 0.001.seconds)
|
|
|
|
|
|
|
|
WebMock.stub_request(:post, /.*/).to_return do |request|
|
2022-03-02 08:16:31 +05:30
|
|
|
{ body: %w(a b), status: 200 }
|
2021-07-02 01:05:55 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
after(:all) do
|
2022-03-02 08:16:31 +05:30
|
|
|
Net.send(:remove_const, :HTTP)
|
|
|
|
Net.send(:const_set, :HTTP, @original_net_http)
|
2022-04-04 11:22:00 +05:30
|
|
|
WebMock::HttpLibAdapters::NetHttpAdapter.instance_variable_set('@webMockNetHTTP', @webmock_net_http)
|
|
|
|
|
|
|
|
# Reload Gitlab::NetHttpAdapter
|
|
|
|
Gitlab.send(:remove_const, :NetHttpAdapter)
|
|
|
|
load "#{Rails.root}/lib/gitlab/net_http_adapter.rb"
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
2021-07-02 01:05:55 +05:30
|
|
|
let(:options) { {} }
|
|
|
|
|
|
|
|
subject(:request_slow_responder) { described_class.post('http://example.org', **options) }
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
shared_examples 'tracks the timeout but does not raise an error' do
|
|
|
|
specify :aggregate_failures do
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(
|
|
|
|
an_instance_of(Gitlab::HTTP::ReadTotalTimeout)
|
|
|
|
).once
|
|
|
|
|
|
|
|
expect { request_slow_responder }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'still calls the block' do
|
2022-03-02 08:16:31 +05:30
|
|
|
expect { |b| described_class.post('http://example.org', **options, &b) }.to yield_successive_args('a', 'b')
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
2021-07-02 01:05:55 +05:30
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
shared_examples 'does not track or raise timeout error' do
|
|
|
|
specify :aggregate_failures do
|
|
|
|
expect(Gitlab::ErrorTracking).not_to receive(:track_exception)
|
|
|
|
|
|
|
|
expect { request_slow_responder }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'tracks the timeout but does not raise an error'
|
|
|
|
|
|
|
|
context 'and use_read_total_timeout option is truthy' do
|
2021-07-02 01:05:55 +05:30
|
|
|
let(:options) { { use_read_total_timeout: true } }
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'raises an error' do
|
2021-07-02 01:05:55 +05:30
|
|
|
expect { request_slow_responder }.to raise_error(Gitlab::HTTP::ReadTotalTimeout, /Request timed out after ?([0-9]*[.])?[0-9]+ seconds/)
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
2021-07-02 01:05:55 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'and timeout option is greater than DEFAULT_READ_TOTAL_TIMEOUT' do
|
|
|
|
let(:options) { { timeout: 10.seconds } }
|
2021-07-02 01:05:55 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it_behaves_like 'does not track or raise timeout error'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and stream_body option is truthy' do
|
|
|
|
let(:options) { { stream_body: true } }
|
|
|
|
|
|
|
|
it_behaves_like 'does not track or raise timeout error'
|
|
|
|
|
|
|
|
context 'but skip_read_total_timeout option is falsey' do
|
|
|
|
let(:options) { { stream_body: true, skip_read_total_timeout: false } }
|
|
|
|
|
|
|
|
it_behaves_like 'tracks the timeout but does not raise an error'
|
2021-07-02 01:05:55 +05:30
|
|
|
end
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
context 'and skip_read_total_timeout option is truthy' do
|
|
|
|
let(:options) { { skip_read_total_timeout: true } }
|
|
|
|
|
|
|
|
it_behaves_like 'does not track or raise timeout error'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and skip_read_total_timeout option is falsely' do
|
|
|
|
let(:options) { { skip_read_total_timeout: false } }
|
|
|
|
|
|
|
|
it_behaves_like 'tracks the timeout but does not raise an error'
|
|
|
|
end
|
2021-07-02 01:05:55 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls a block' do
|
|
|
|
WebMock.stub_request(:post, /.*/)
|
|
|
|
|
|
|
|
expect { |b| described_class.post('http://example.org', &b) }.to yield_with_args
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
describe 'allow_local_requests_from_web_hooks_and_services is' do
|
2018-03-26 14:24:53 +05:30
|
|
|
before do
|
|
|
|
WebMock.stub_request(:get, /.*/).to_return(status: 200, body: 'Success')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'disabled' do
|
|
|
|
before do
|
2019-10-12 21:52:04 +05:30
|
|
|
allow(Gitlab::CurrentSettings).to receive(:allow_local_requests_from_web_hooks_and_services?).and_return(false)
|
2018-03-26 14:24:53 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'deny requests to localhost' do
|
2018-05-09 12:01:36 +05:30
|
|
|
expect { described_class.get('http://localhost:3003') }.to raise_error(Gitlab::HTTP::BlockedUrlError)
|
2018-03-26 14:24:53 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'deny requests to private network' do
|
2018-05-09 12:01:36 +05:30
|
|
|
expect { described_class.get('http://192.168.1.2:3003') }.to raise_error(Gitlab::HTTP::BlockedUrlError)
|
2018-03-26 14:24:53 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'if allow_local_requests set to true' do
|
|
|
|
it 'override the global value and allow requests to localhost or private network' do
|
2019-06-05 12:25:43 +05:30
|
|
|
stub_full_request('http://localhost:3003')
|
|
|
|
|
2018-03-26 14:24:53 +05:30
|
|
|
expect { described_class.get('http://localhost:3003', allow_local_requests: true) }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'enabled' do
|
|
|
|
before do
|
2019-10-12 21:52:04 +05:30
|
|
|
allow(Gitlab::CurrentSettings).to receive(:allow_local_requests_from_web_hooks_and_services?).and_return(true)
|
2018-03-26 14:24:53 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'allow requests to localhost' do
|
2019-06-05 12:25:43 +05:30
|
|
|
stub_full_request('http://localhost:3003')
|
|
|
|
|
2018-03-26 14:24:53 +05:30
|
|
|
expect { described_class.get('http://localhost:3003') }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allow requests to private network' do
|
|
|
|
expect { described_class.get('http://192.168.1.2:3003') }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if allow_local_requests set to false' do
|
|
|
|
it 'override the global value and ban requests to localhost or private network' do
|
2018-05-09 12:01:36 +05:30
|
|
|
expect { described_class.get('http://localhost:3003', allow_local_requests: false) }.to raise_error(Gitlab::HTTP::BlockedUrlError)
|
2018-03-26 14:24:53 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
describe 'handle redirect loops' do
|
|
|
|
before do
|
2019-06-05 12:25:43 +05:30
|
|
|
stub_full_request("http://example.org", method: :any).to_raise(HTTParty::RedirectionTooDeep.new("Redirection Too Deep"))
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles GET requests' do
|
|
|
|
expect { described_class.get('http://example.org') }.to raise_error(Gitlab::HTTP::RedirectionTooDeep)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles POST requests' do
|
|
|
|
expect { described_class.post('http://example.org') }.to raise_error(Gitlab::HTTP::RedirectionTooDeep)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles PUT requests' do
|
|
|
|
expect { described_class.put('http://example.org') }.to raise_error(Gitlab::HTTP::RedirectionTooDeep)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles DELETE requests' do
|
|
|
|
expect { described_class.delete('http://example.org') }.to raise_error(Gitlab::HTTP::RedirectionTooDeep)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles HEAD requests' do
|
|
|
|
expect { described_class.head('http://example.org') }.to raise_error(Gitlab::HTTP::RedirectionTooDeep)
|
|
|
|
end
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
describe 'setting default timeouts' do
|
|
|
|
before do
|
|
|
|
stub_full_request('http://example.org', method: :any)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no timeouts are set' do
|
|
|
|
it 'sets default open and read and write timeouts' do
|
|
|
|
expect(described_class).to receive(:httparty_perform_request).with(
|
|
|
|
Net::HTTP::Get, 'http://example.org', default_options
|
|
|
|
).and_call_original
|
|
|
|
|
|
|
|
described_class.get('http://example.org')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when :timeout is set' do
|
|
|
|
it 'does not set any default timeouts' do
|
|
|
|
expect(described_class).to receive(:httparty_perform_request).with(
|
|
|
|
Net::HTTP::Get, 'http://example.org', timeout: 1
|
|
|
|
).and_call_original
|
|
|
|
|
|
|
|
described_class.get('http://example.org', timeout: 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when :open_timeout is set' do
|
|
|
|
it 'only sets default read and write timeout' do
|
|
|
|
expect(described_class).to receive(:httparty_perform_request).with(
|
|
|
|
Net::HTTP::Get, 'http://example.org', default_options.merge(open_timeout: 1)
|
|
|
|
).and_call_original
|
|
|
|
|
|
|
|
described_class.get('http://example.org', open_timeout: 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when :read_timeout is set' do
|
|
|
|
it 'only sets default open and write timeout' do
|
|
|
|
expect(described_class).to receive(:httparty_perform_request).with(
|
|
|
|
Net::HTTP::Get, 'http://example.org', default_options.merge(read_timeout: 1)
|
|
|
|
).and_call_original
|
|
|
|
|
|
|
|
described_class.get('http://example.org', read_timeout: 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when :write_timeout is set' do
|
|
|
|
it 'only sets default open and read timeout' do
|
|
|
|
expect(described_class).to receive(:httparty_perform_request).with(
|
|
|
|
Net::HTTP::Put, 'http://example.org', default_options.merge(write_timeout: 1)
|
|
|
|
).and_call_original
|
|
|
|
|
|
|
|
described_class.put('http://example.org', write_timeout: 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
describe '.try_get' do
|
|
|
|
let(:path) { 'http://example.org' }
|
|
|
|
|
|
|
|
let(:extra_log_info_proc) do
|
|
|
|
proc do |error, url, options|
|
|
|
|
{ klass: error.class, url: url, options: options }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:request_options) do
|
2020-10-24 23:57:45 +05:30
|
|
|
default_options.merge({
|
2020-04-22 19:07:51 +05:30
|
|
|
verify: false,
|
|
|
|
basic_auth: { username: 'user', password: 'pass' }
|
2020-10-24 23:57:45 +05:30
|
|
|
})
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
described_class::HTTP_ERRORS.each do |exception_class|
|
|
|
|
context "with #{exception_class}" do
|
|
|
|
let(:klass) { exception_class }
|
|
|
|
|
|
|
|
context 'with path' do
|
|
|
|
before do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(described_class).to receive(:httparty_perform_request)
|
|
|
|
.with(Net::HTTP::Get, path, default_options)
|
2020-04-22 19:07:51 +05:30
|
|
|
.and_raise(klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles requests without extra_log_info' do
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.with(instance_of(klass), {})
|
|
|
|
|
|
|
|
expect(described_class.try_get(path)).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles requests with extra_log_info as hash' do
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.with(instance_of(klass), { a: :b })
|
|
|
|
|
|
|
|
expect(described_class.try_get(path, extra_log_info: { a: :b })).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles requests with extra_log_info as proc' do
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.with(instance_of(klass), { url: path, klass: klass, options: {} })
|
|
|
|
|
|
|
|
expect(described_class.try_get(path, extra_log_info: extra_log_info_proc)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with path and options' do
|
|
|
|
before do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(described_class).to receive(:httparty_perform_request)
|
|
|
|
.with(Net::HTTP::Get, path, request_options)
|
2020-04-22 19:07:51 +05:30
|
|
|
.and_raise(klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles requests without extra_log_info' do
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.with(instance_of(klass), {})
|
|
|
|
|
|
|
|
expect(described_class.try_get(path, request_options)).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles requests with extra_log_info as hash' do
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.with(instance_of(klass), { a: :b })
|
|
|
|
|
|
|
|
expect(described_class.try_get(path, **request_options, extra_log_info: { a: :b })).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles requests with extra_log_info as proc' do
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.with(instance_of(klass), { klass: klass, url: path, options: request_options })
|
|
|
|
|
|
|
|
expect(described_class.try_get(path, **request_options, extra_log_info: extra_log_info_proc)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with path, options, and block' do
|
|
|
|
let(:block) do
|
|
|
|
proc {}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(described_class).to receive(:httparty_perform_request)
|
|
|
|
.with(Net::HTTP::Get, path, request_options, &block)
|
2020-04-22 19:07:51 +05:30
|
|
|
.and_raise(klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles requests without extra_log_info' do
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.with(instance_of(klass), {})
|
|
|
|
|
|
|
|
expect(described_class.try_get(path, request_options, &block)).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles requests with extra_log_info as hash' do
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.with(instance_of(klass), { a: :b })
|
|
|
|
|
|
|
|
expect(described_class.try_get(path, **request_options, extra_log_info: { a: :b }, &block)).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles requests with extra_log_info as proc' do
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.with(instance_of(klass), { klass: klass, url: path, options: request_options })
|
|
|
|
|
|
|
|
expect(described_class.try_get(path, **request_options, extra_log_info: extra_log_info_proc, &block)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-26 14:24:53 +05:30
|
|
|
end
|