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
class UploadedFileAdapter
def initialize(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
def original_filename
......
......@@ -13,8 +13,7 @@ require 'active_support'
require 'active_support/core_ext'
require 'mime/types'
require 'pathname'
require 'pathname'
require 'ostruct'
puts "Testing against version #{ActiveRecord::VERSION::STRING}"
......
......@@ -2,44 +2,84 @@ require './test/helper'
class UploadedFileAdapterTest < Test::Unit::TestCase
context "a new instance" do
setup do
class UploadedFile < OpenStruct; end
@file = UploadedFile.new(
:original_filename => "5k.png",
:content_type => "image/png",
:head => "",
:tempfile => File.new(fixture_file("5k.png"))
)
@subject = Paperclip.io_adapters.for(@file)
end
context "with UploadedFile responding to #tempfile" do
setup do
class UploadedFile < OpenStruct; end
@file = UploadedFile.new(
:original_filename => "5k.png",
:content_type => "image/png",
:head => "",
:tempfile => File.new(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 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 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 "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 "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.tempfile.path).to_s
assert_equal expected, @subject.fingerprint
end
should "generate a MD5 hash of the contents" do
expected = Digest::MD5.file(@file.tempfile.path).to_s
assert_equal expected, @subject.fingerprint
end
should "read the contents of the file" do
expected = @file.tempfile.read
assert expected.length > 0
assert_equal expected, @subject.read
should "read the contents of the file" do
expected = @file.tempfile.read
assert expected.length > 0
assert_equal expected, @subject.read
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
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