1
0
Fork 0
mirror of https://github.com/carrierwaveuploader/carrierwave.git synced 2022-11-09 12:03:54 -05:00

Raise DownloadError when no content is returned

Have RemoteFile raise CarrierWave::DownloadError when the HTTP response
is of a type that has no content, according to the standards.
In those cases, response.body returns nil.

There are 8 response types that have no body, 4 of which we can get, so
just check whether the body was nil.

Fixes #2632
This commit is contained in:
Brian Hawley 2022-09-28 11:54:57 -07:00
parent b104d0f0e0
commit ba50008f81
2 changed files with 21 additions and 1 deletions

View file

@ -8,7 +8,10 @@ module CarrierWave
when String
@file = StringIO.new(file)
when Net::HTTPResponse
@file = StringIO.new(file.body)
body = file.body
raise CarrierWave::DownloadError, 'could not download file: No Content' if body.nil?
@file = StringIO.new(body)
@content_type = file.content_type
@headers = file
@uri = file.uri

View file

@ -26,6 +26,23 @@ describe CarrierWave::Downloader::RemoteFile do
end
end
{
'204' => Net::HTTPNoContent,
'205' => Net::HTTPResetContent
}.each do |response_code, response_class|
context "with a #{response_class} instance" do
let!(:file) do
response_class.new('1.0', response_code, '').tap do |response|
response.reading_body(StringIO.new, true) {}
end
end
it 'raises CarrierWave::DownloadError' do
expect { subject }.to raise_error(CarrierWave::DownloadError, 'could not download file: No Content')
end
end
end
context 'with OpenURI::Meta instance' do
let(:file) do
File.open(file_path("test.jpg")).tap { |f| OpenURI::Meta.init(f) }.tap do |file|