Skip to content

unknown file extensions return binary mime-type. no file extension returns fallback mime-type #366

Closed
wants to merge 3 commits into from
View
8 lib/rack/mime.rb
@@ -10,11 +10,11 @@ module Mime
# Usage:
# Rack::Mime.mime_type('.foo')
#
- # This is a shortcut for:
- # Rack::Mime::MIME_TYPES.fetch('.foo', 'application/octet-stream')
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
@@ -598,7 +598,7 @@ def mime_type(ext, fallback='application/octet-stream')
".wmv" => "video/x-ms-wmv",
".wmx" => "video/x-ms-wmx",
".wmz" => "application/x-ms-wmz",
- ".woff" => "application/octet-stream",
+ ".woff" => "application/x-font-woff",
".wpd" => "application/vnd.wordperfect",
".wpl" => "application/vnd.ms-wpl",
".wps" => "application/vnd.ms-works",
View
23 test/spec_mime.rb
@@ -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
Something went wrong with that request. Please try again.