Skip to content

Commit

Permalink
Adding a unit test for Upfile and updating a few content types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Morrison committed Oct 9, 2009
1 parent d6ebd32 commit c7b84ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/paperclip/upfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ module Upfile
def content_type
type = (self.path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
case type
when %r"jpe?g" then "image/jpeg"
when %r"jp(e|g|eg)" then "image/jpeg"
when %r"tiff?" then "image/tiff"
when %r"png", "gif", "bmp" then "image/#{type}"
when "txt" then "text/plain"
when %r"html?" then "text/html"
when "csv", "xml", "css", "js" then "text/#{type}"
when "js" then "application/js"
when "csv", "xml", "css" then "text/#{type}"
else "application/x-#{type}"
end
end
Expand Down
28 changes: 28 additions & 0 deletions test/upfile_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'test/helper'

class UpfileTest < Test::Unit::TestCase
{ %w(jpg jpe jpeg) => 'image/jpeg',
%w(tif tiff) => 'image/tiff',
%w(png) => 'image/png',
%w(gif) => 'image/gif',
%w(bmp) => 'image/bmp',
%w(txt) => 'text/plain',
%w(htm html) => 'text/html',
%w(csv) => 'text/csv',
%w(xml) => 'text/xml',
%w(css) => 'text/css',
%w(js) => 'application/js',
%w(foo) => 'application/x-foo'
}.each do |extensions, content_type|
extensions.each do |extension|
should "return a content_type of #{content_type} for a file with extension .#{extension}" do
file = stub('file', :path => "basename.#{extension}")
class << file
include Paperclip::Upfile
end

assert_equal content_type, file.content_type
end
end
end
end

0 comments on commit c7b84ad

Please sign in to comment.