Skip to content

Commit

Permalink
Raise DownloadError when no content is returned
Browse files Browse the repository at this point in the history
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 carrierwaveuploader#2632
  • Loading branch information
BrianHawley committed Sep 28, 2022
1 parent b104d0f commit ba50008
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/carrierwave/downloader/remote_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ def initialize(file)
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
Expand Down
17 changes: 17 additions & 0 deletions spec/downloader/remote_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@
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|
Expand Down

0 comments on commit ba50008

Please sign in to comment.