From 5f7845118f649b60862a9bc01f6b2a0f932e89d7 Mon Sep 17 00:00:00 2001 From: Raf Szalanski Date: Thu, 2 Jul 2015 22:31:40 +0000 Subject: [PATCH] Fix creation of tempfiles in Rack::Test::UploadedFile 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. --- lib/rack/test/uploaded_file.rb | 2 +- spec/rack/test/uploaded_file_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/rack/test/uploaded_file.rb b/lib/rack/test/uploaded_file.rb index d0d3e2a8..33394127 100644 --- a/lib/rack/test/uploaded_file.rb +++ b/lib/rack/test/uploaded_file.rb @@ -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 diff --git a/spec/rack/test/uploaded_file_spec.rb b/spec/rack/test/uploaded_file_spec.rb index cde48567..8ffafc66 100644 --- a/spec/rack/test/uploaded_file_spec.rb +++ b/spec/rack/test/uploaded_file_spec.rb @@ -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