Skip to content

Commit

Permalink
Fix UploadedFile#new regression (rack#215)
Browse files Browse the repository at this point in the history
In rack#210, we tried to fix this but the fix was perhaps not really good enough... So let's relax this check a bit and hopefully unbreak these use cases.
  • Loading branch information
perlun authored and Alex Damian Negru committed Apr 5, 2021
1 parent 9554567 commit a70b015
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
14 changes: 7 additions & 7 deletions lib/rack/test/uploaded_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class UploadedFile
# file.
# @param content_type [String]
# @param binary [Boolean] an optional flag that indicates whether the file should be open in binary mode or not.
# @param original_filename [String] an optional parameter that provides the original filename if `content` is an IO
# object.
# @param original_filename [String] an optional parameter that provides the original filename if `content` is a StringIO
# object. Not used for other kind of `content` objects.
def initialize(content, content_type = 'text/plain', binary = false, original_filename: nil)
if content.respond_to?(:read) && (content.is_a?(IO) || content.is_a?(StringIO))
initialize_from_io(content, original_filename)
if original_filename
initialize_from_stringio(content, original_filename)
else
initialize_from_file_path(content)
end
Expand Down Expand Up @@ -62,9 +62,9 @@ def self.actually_finalize(file)

private

def initialize_from_io(io, original_filename)
@tempfile = io
@original_filename = original_filename || raise(ArgumentError, 'Missing `original_filename` for IO')
def initialize_from_stringio(stringio, original_filename)
@tempfile = stringio
@original_filename = original_filename || raise(ArgumentError, 'Missing `original_filename` for StringIO object')
end

def initialize_from_file_path(path)
Expand Down
12 changes: 3 additions & 9 deletions spec/rack/test/uploaded_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ def test_file_path
end

describe '#initialize' do
subject { -> { uploaded_file } }
let(:uploaded_file) { described_class.new(io, original_filename: original_filename) }

context 'with an IO object' do
let(:io) { StringIO.new('I am content') }
let(:stringio) { StringIO.new('I am content') }
let(:uploaded_file) { described_class.new(stringio, original_filename: original_filename) }
subject { -> { uploaded_file } }

context 'with an original filename' do
let(:original_filename) { 'content.txt' }
Expand All @@ -59,11 +58,6 @@ def test_file_path
expect(uploaded_file.original_filename).to eq(original_filename)
end
end

context 'without an original filename' do
let(:original_filename) { nil }
it { should raise_error(ArgumentError) }
end
end
end
end

0 comments on commit a70b015

Please sign in to comment.