diff options
Diffstat (limited to 'spec/lib/bulk_imports/clients/http_spec.rb')
-rw-r--r-- | spec/lib/bulk_imports/clients/http_spec.rb | 102 |
1 files changed, 50 insertions, 52 deletions
diff --git a/spec/lib/bulk_imports/clients/http_spec.rb b/spec/lib/bulk_imports/clients/http_spec.rb index 2d841b7fac2..213fa23675e 100644 --- a/spec/lib/bulk_imports/clients/http_spec.rb +++ b/spec/lib/bulk_imports/clients/http_spec.rb @@ -8,66 +8,23 @@ RSpec.describe BulkImports::Clients::Http do let(:uri) { 'http://gitlab.example' } let(:token) { 'token' } let(:resource) { 'resource' } + let(:response_double) { double(code: 200, success?: true, parsed_response: {}) } subject { described_class.new(uri: uri, token: token) } - describe '#get' do - let(:response_double) { double(code: 200, success?: true, parsed_response: {}) } - - shared_examples 'performs network request' do - it 'performs network request' do - expect(Gitlab::HTTP).to receive(:get).with(*expected_args).and_return(response_double) - - subject.get(resource) - end - end - - describe 'request query' do - include_examples 'performs network request' do - let(:expected_args) do - [ - anything, - hash_including( - query: { - page: described_class::DEFAULT_PAGE, - per_page: described_class::DEFAULT_PER_PAGE - } - ) - ] - end - end - end - - describe 'request headers' do - include_examples 'performs network request' do - let(:expected_args) do - [ - anything, - hash_including( - headers: { - 'Content-Type' => 'application/json', - 'Authorization' => "Bearer #{token}" - } - ) - ] - end - end - end + shared_examples 'performs network request' do + it 'performs network request' do + expect(Gitlab::HTTP).to receive(method).with(*expected_args).and_return(response_double) - describe 'request uri' do - include_examples 'performs network request' do - let(:expected_args) do - ['http://gitlab.example:80/api/v4/resource', anything] - end - end + subject.public_send(method, resource) end context 'error handling' do context 'when error occurred' do it 'raises ConnectionError' do - allow(Gitlab::HTTP).to receive(:get).and_raise(Errno::ECONNREFUSED) + allow(Gitlab::HTTP).to receive(method).and_raise(Errno::ECONNREFUSED) - expect { subject.get(resource) }.to raise_exception(described_class::ConnectionError) + expect { subject.public_send(method, resource) }.to raise_exception(described_class::ConnectionError) end end @@ -75,12 +32,34 @@ RSpec.describe BulkImports::Clients::Http do it 'raises ConnectionError' do response_double = double(code: 503, success?: false) - allow(Gitlab::HTTP).to receive(:get).and_return(response_double) + allow(Gitlab::HTTP).to receive(method).and_return(response_double) - expect { subject.get(resource) }.to raise_exception(described_class::ConnectionError) + expect { subject.public_send(method, resource) }.to raise_exception(described_class::ConnectionError) end end end + end + + describe '#get' do + let(:method) { :get } + + include_examples 'performs network request' do + let(:expected_args) do + [ + 'http://gitlab.example:80/api/v4/resource', + hash_including( + query: { + page: described_class::DEFAULT_PAGE, + per_page: described_class::DEFAULT_PER_PAGE + }, + headers: { + 'Content-Type' => 'application/json', + 'Authorization' => "Bearer #{token}" + } + ) + ] + end + end describe '#each_page' do let(:objects1) { [{ object: 1 }, { object: 2 }] } @@ -129,4 +108,23 @@ RSpec.describe BulkImports::Clients::Http do end end end + + describe '#post' do + let(:method) { :post } + + include_examples 'performs network request' do + let(:expected_args) do + [ + 'http://gitlab.example:80/api/v4/resource', + hash_including( + body: {}, + headers: { + 'Content-Type' => 'application/json', + 'Authorization' => "Bearer #{token}" + } + ) + ] + end + end + end end |