Permalink
Browse files

files w/o an extension return fallback mime-type, unknown extensions …

…always return "application/octet-stream"
  • Loading branch information...
1 parent e9a9103 commit 2618862e1124c3754f30d080f9c11b875924cf30 @montague committed Mar 16, 2012
Showing with 26 additions and 1 deletion.
  1. +3 −1 lib/rack/mime.rb
  2. +23 −0 test/spec_mime.rb
View
@@ -12,7 +12,9 @@ module Mime
#
def mime_type(ext, fallback='application/octet-stream')
- MIME_TYPES.fetch(ext.to_s.downcase, fallback)
+ ext = ext.to_s.downcase
+ return fallback if ext == '' || ext.nil?
+ return MIME_TYPES.fetch(ext, 'application/octet-stream')
end
module_function :mime_type
View
@@ -0,0 +1,23 @@
+require 'rack/mime'
+
+describe "Rack::Mime#mime_type" do
+ it "should return the fallback mime-type for files with no extension" do
+ fallback = 'image/jpg'
+ Rack::Mime.mime_type(File.extname('no_ext'), fallback).should == fallback
+ end
+
+ it "should always return 'application/octet-stream' for unknown file extensions" do
+ unknown_ext = File.extname('unknown_ext.abcdefg')
+ Rack::Mime.mime_type(unknown_ext).should == 'application/octet-stream'
+ end
+
+ it "should ignore explicitly set fallback for unknown file extensions" do
+ unknown_ext = File.extname('unknown_ext.abcdefg')
+ Rack::Mime.mime_type(unknown_ext, 'explicit_fallback').should == 'application/octet-stream'
+ end
+
+ it "should return the mime-type for a given extension" do
+ # sanity check. it would be infeasible test every single mime-type.
+ Rack::Mime.mime_type(File.extname('image.jpg')).should == 'image/jpeg'
+ end
+end

0 comments on commit 2618862

Please sign in to comment.