Commit 76da2102 authored by David Fernandez's avatar David Fernandez

Update the Container Registry gitlab API client

* add an ending forward slash on the import url
* configure the connection to accept JSON responses
* cleanup the related spec
parent 07749ddd
......@@ -57,7 +57,7 @@ module ContainerRegistry
def faraday(timeout_enabled: true)
@faraday ||= faraday_base(timeout_enabled: timeout_enabled) do |conn|
initialize_connection(conn, @options, &method(:accept_manifest))
initialize_connection(conn, @options, &method(:configure_connection))
end
end
......@@ -105,7 +105,7 @@ module ContainerRegistry
faraday_redirect.get(uri)
end
def accept_manifest(conn)
def configure_connection(conn)
conn.headers['Accept'] = ACCEPTED_TYPES
conn.response :json, content_type: 'application/json'
......
......@@ -4,6 +4,8 @@ module ContainerRegistry
class GitlabApiClient < BaseClient
include Gitlab::Utils::StrongMemoize
JSON_TYPE = 'application/json'
IMPORT_RESPONSES = {
200 => :already_imported,
202 => :ok,
......@@ -46,9 +48,20 @@ module ContainerRegistry
private
def start_import_for(path, pre:)
faraday.put("/gitlab/v1/import/#{path}") do |req|
faraday.put(import_url_for(path)) do |req|
req.params['pre'] = pre.to_s
end
end
def import_url_for(path)
"/gitlab/v1/import/#{path}/"
end
# overrides the default configuration
def configure_connection(conn)
conn.headers['Accept'] = [JSON_TYPE]
conn.response :json, content_type: JSON_TYPE
end
end
end
......@@ -7,6 +7,8 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
include_context 'container registry client'
let(:path) { 'namespace/path/to/repository' }
describe '#supports_gitlab_api?' do
subject { client.supports_gitlab_api? }
......@@ -30,9 +32,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
it 'returns the expected result' do
if expect_registry_to_be_pinged
expect_next_instance_of(Faraday::Connection) do |connection|
expect(connection).to receive(:run_request).and_call_original
end
expect(Faraday::Connection).to receive(:new).and_call_original
else
expect(Faraday::Connection).not_to receive(:new)
end
......@@ -54,9 +54,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
end
describe '#pre_import_repository' do
let(:path) { 'namespace/path/to/repository' }
subject { client.pre_import_repository('namespace/path/to/repository') }
subject { client.pre_import_repository(path) }
where(:status_code, :expected_result) do
200 | :already_imported
......@@ -80,9 +78,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
end
describe '#pre_import_repository' do
let(:path) { 'namespace/path/to/repository' }
subject { client.import_repository('namespace/path/to/repository') }
subject { client.import_repository(path) }
where(:status_code, :expected_result) do
200 | :already_imported
......@@ -129,9 +125,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
it 'returns the expected result' do
if expect_registry_to_be_pinged
expect_next_instance_of(Faraday::Connection) do |connection|
expect(connection).to receive(:run_request).and_call_original
end
expect(Faraday::Connection).to receive(:new).and_call_original
else
expect(Faraday::Connection).not_to receive(:new)
end
......@@ -166,13 +160,15 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
end
def stub_pre_import(path, status_code, pre:)
stub_request(:put, "#{registry_api_url}/gitlab/v1/import/#{path}?pre=#{pre}")
stub_request(:put, "#{registry_api_url}/gitlab/v1/import/#{path}/?pre=#{pre}")
.with(headers: { 'Accept' => described_class::JSON_TYPE })
.to_return(status: status_code, body: '')
end
def stub_registry_gitlab_api_support(supported = true)
status_code = supported ? 200 : 404
stub_request(:get, "#{registry_api_url}/gitlab/v1/")
.with(headers: { 'Accept' => described_class::JSON_TYPE })
.to_return(status: status_code, body: '')
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment