Merge branch 'fj-49802-bug-api-set-http-headers' into 'master'

Fix bug setting http headers in Files API

See merge request gitlab-org/gitlab-ce!20938
This commit is contained in:
Nick Thomas 2018-08-01 16:48:33 +00:00
commit f175014067
3 changed files with 40 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
title: Fix bug setting http headers in Files API
merge_request: 20938
author:
type: fixed

View file

@ -3,7 +3,11 @@ module API
module HeadersHelpers
def set_http_headers(header_data)
header_data.each do |key, value|
header "X-Gitlab-#{key.to_s.split('_').collect(&:capitalize).join('-')}", value
if value.is_a?(Enumerable)
raise ArgumentError.new("Header value should be a string")
end
header "X-Gitlab-#{key.to_s.split('_').collect(&:capitalize).join('-')}", value.to_s
end
end
end

View file

@ -13,6 +13,24 @@ describe API::Files do
let(:author_email) { 'user@example.org' }
let(:author_name) { 'John Doe' }
let(:helper) do
fake_class = Class.new do
include ::API::Helpers::HeadersHelpers
attr_reader :headers
def initialize
@headers = {}
end
def header(key, value)
@headers[key] = value
end
end
fake_class.new
end
before do
project.add_developer(user)
end
@ -21,6 +39,18 @@ describe API::Files do
"/projects/#{project.id}/repository/files/#{file_path}"
end
context 'http headers' do
it 'converts value into string' do
helper.set_http_headers(test: 1)
expect(helper.headers).to eq({ 'X-Gitlab-Test' => '1' })
end
it 'raises exception if value is an Enumerable' do
expect { helper.set_http_headers(test: [1]) }.to raise_error(ArgumentError)
end
end
describe "HEAD /projects/:id/repository/files/:file_path" do
shared_examples_for 'repository files' do
it 'returns file attributes in headers' do