Commit 7d845690 by Prem Sichanugrist

Fix a problem with `Rack::Test::UploadedFile`

In Rails, `ActionDispatch::Http::UploadedFile` has an accessor to access
a `Tempfile` object. However, `Rack::Test::UploadedFile` does not
provide that, but provinding the `#path` method instead. To be able to
support both, we have to check for existance for `#tempfile` method,
then fallback to `#path` method.

Fixes #807
parent 237675da
...@@ -2,7 +2,12 @@ module Paperclip ...@@ -2,7 +2,12 @@ module Paperclip
class UploadedFileAdapter class UploadedFileAdapter
def initialize(target) def initialize(target)
@target = target @target = target
@tempfile = copy_to_tempfile(@target.tempfile)
if @target.respond_to?(:tempfile)
@tempfile = copy_to_tempfile(@target.tempfile)
else
@tempfile = copy_to_tempfile(@target)
end
end end
def original_filename def original_filename
......
...@@ -13,8 +13,7 @@ require 'active_support' ...@@ -13,8 +13,7 @@ require 'active_support'
require 'active_support/core_ext' require 'active_support/core_ext'
require 'mime/types' require 'mime/types'
require 'pathname' require 'pathname'
require 'ostruct'
require 'pathname'
puts "Testing against version #{ActiveRecord::VERSION::STRING}" puts "Testing against version #{ActiveRecord::VERSION::STRING}"
......
...@@ -2,44 +2,84 @@ require './test/helper' ...@@ -2,44 +2,84 @@ require './test/helper'
class UploadedFileAdapterTest < Test::Unit::TestCase class UploadedFileAdapterTest < Test::Unit::TestCase
context "a new instance" do context "a new instance" do
setup do context "with UploadedFile responding to #tempfile" do
class UploadedFile < OpenStruct; end setup do
@file = UploadedFile.new( class UploadedFile < OpenStruct; end
:original_filename => "5k.png", @file = UploadedFile.new(
:content_type => "image/png", :original_filename => "5k.png",
:head => "", :content_type => "image/png",
:tempfile => File.new(fixture_file("5k.png")) :head => "",
) :tempfile => File.new(fixture_file("5k.png"))
@subject = Paperclip.io_adapters.for(@file) )
end @subject = Paperclip.io_adapters.for(@file)
end
should "get the right filename" do should "get the right filename" do
assert_equal "5k.png", @subject.original_filename assert_equal "5k.png", @subject.original_filename
end end
should "get the content type" do should "get the content type" do
assert_equal "image/png", @subject.content_type assert_equal "image/png", @subject.content_type
end end
should "get the file's size" do should "get the file's size" do
assert_equal 4456, @subject.size assert_equal 4456, @subject.size
end end
should "return false for a call to nil?" do should "return false for a call to nil?" do
assert ! @subject.nil? assert ! @subject.nil?
end end
should "generate a MD5 hash of the contents" do should "generate a MD5 hash of the contents" do
expected = Digest::MD5.file(@file.tempfile.path).to_s expected = Digest::MD5.file(@file.tempfile.path).to_s
assert_equal expected, @subject.fingerprint assert_equal expected, @subject.fingerprint
end end
should "read the contents of the file" do should "read the contents of the file" do
expected = @file.tempfile.read expected = @file.tempfile.read
assert expected.length > 0 assert expected.length > 0
assert_equal expected, @subject.read assert_equal expected, @subject.read
end
end end
end context "with UploadFile responding to #path" do
setup do
class UploadedFile < OpenStruct; end
@file = UploadedFile.new(
:original_filename => "5k.png",
:content_type => "image/png",
:head => "",
:path => fixture_file("5k.png")
)
@subject = Paperclip.io_adapters.for(@file)
end
should "get the right filename" do
assert_equal "5k.png", @subject.original_filename
end
should "get the content type" do
assert_equal "image/png", @subject.content_type
end
should "get the file's size" do
assert_equal 4456, @subject.size
end
should "return false for a call to nil?" do
assert ! @subject.nil?
end
should "generate a MD5 hash of the contents" do
expected = Digest::MD5.file(@file.path).to_s
assert_equal expected, @subject.fingerprint
end
should "read the contents of the file" do
expected = File.new(@file.path).read
assert expected.length > 0
assert_equal expected, @subject.read
end
end
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment