Skip to content

Commit

Permalink
Fix creation of tempfiles in Rack::Test::UploadedFile
Browse files Browse the repository at this point in the history
Tempfile's initializer is not very intuitive. If you want your tempfile
to have an extension you have to pass an array with two elements:
  - basename
  - extension (with a dot at the beginning)

This change makes it behave more like original Rack::Multipart::UploadedFile.
  • Loading branch information
szalansky committed Jul 2, 2015
1 parent 56ddeda commit 5f78451
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rack/test/uploaded_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(path, content_type = "text/plain", binary = false)
@content_type = content_type
@original_filename = ::File.basename(path)

@tempfile = Tempfile.new(@original_filename)
@tempfile = Tempfile.new([@original_filename, ::File.extname(path)])
@tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)
@tempfile.binmode if binary

Expand Down
5 changes: 5 additions & 0 deletions spec/rack/test/uploaded_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ def test_file_path
uploaded_file.should respond_to(:tempfile) # Allows calls to params[:file].tempfile
end

it "creates Tempfiles with original file's extension" do
uploaded_file = Rack::Test::UploadedFile.new(test_file_path)

File.extname(uploaded_file.path).should eq(".txt")
end
end

0 comments on commit 5f78451

Please sign in to comment.