Fix bug setting http headers in Files API

This commit is contained in:
Francisco Javier López 🌴 On vacation; back on August 22th! 2018-08-01 16:48:33 +00:00 committed by Nick Thomas
parent dae5c2665d
commit 4b36f74bfe
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