Commit ca14b70d authored by Sean McGivern's avatar Sean McGivern

Merge branch 'fj-55781-fix-api-blob-content-disposition' into 'master'

Fixed content-disposition in blob and files API endpoint

Closes #55781

See merge request gitlab-org/gitlab-ce!24078
parents a352a95e 2cd47bba
---
title: Fix content-disposition in blobs and files API endpoint
merge_request: 24078
author:
type: fixed
...@@ -496,7 +496,7 @@ module API ...@@ -496,7 +496,7 @@ module API
def send_git_blob(repository, blob) def send_git_blob(repository, blob)
env['api.format'] = :txt env['api.format'] = :txt
content_type 'text/plain' content_type 'text/plain'
header['Content-Disposition'] = "attachment; filename=#{blob.name.inspect}" header['Content-Disposition'] = content_disposition('attachment', blob.name)
header(*Gitlab::Workhorse.send_git_blob(repository, blob)) header(*Gitlab::Workhorse.send_git_blob(repository, blob))
end end
...@@ -529,5 +529,11 @@ module API ...@@ -529,5 +529,11 @@ module API
params[:archived] params[:archived]
end end
def content_disposition(disposition, filename)
disposition += %(; filename=#{filename.inspect}) if filename.present?
disposition
end
end end
end end
...@@ -148,4 +148,36 @@ describe API::Helpers do ...@@ -148,4 +148,36 @@ describe API::Helpers do
it_behaves_like 'user namespace finder' it_behaves_like 'user namespace finder'
end end
describe '#send_git_blob' do
context 'content disposition' do
let(:repository) { double }
let(:blob) { double(name: 'foobar') }
let(:send_git_blob) do
subject.send(:send_git_blob, repository, blob)
end
before do
allow(subject).to receive(:env).and_return({})
allow(subject).to receive(:content_type)
allow(subject).to receive(:header).and_return({})
allow(Gitlab::Workhorse).to receive(:send_git_blob)
end
context 'when blob name is null' do
let(:blob) { double(name: nil) }
it 'returns only the disposition' do
expect(send_git_blob['Content-Disposition']).to eq 'attachment'
end
end
context 'when blob name is not null' do
it 'returns disposition with the blob name' do
expect(send_git_blob['Content-Disposition']).to eq 'attachment; filename="foobar"'
end
end
end
end
end end
...@@ -190,7 +190,7 @@ describe API::Files do ...@@ -190,7 +190,7 @@ describe API::Files do
get api(url, current_user), params: params get api(url, current_user), params: params
expect(headers['Content-Disposition']).to match(/^attachment/) expect(headers['Content-Disposition']).to eq('attachment; filename="popen.rb"')
end end
context 'when mandatory params are not given' do context 'when mandatory params are not given' do
......
...@@ -171,7 +171,7 @@ describe API::Repositories do ...@@ -171,7 +171,7 @@ describe API::Repositories do
it 'forces attachment content disposition' do it 'forces attachment content disposition' do
get api(route, current_user) get api(route, current_user)
expect(headers['Content-Disposition']).to match(/^attachment/) expect(headers['Content-Disposition']).to eq 'attachment'
end end
context 'when sha does not exist' do context 'when sha does not exist' do
......
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