Allow to register a mime type with mime_type

This commit is contained in:
Simon Rozet 2009-10-04 16:41:03 +02:00 committed by Ryan Tomayko
parent 31bf026087
commit 5da3460f90
2 changed files with 17 additions and 3 deletions

View File

@ -730,11 +730,12 @@ module Sinatra
end
end
# Look up a media type by file extension in Rack's mime registry.
def mime_type(type)
# Lookup or register a mime type in Rack's mime registry.
def mime_type(type, value=nil)
return type if type.nil? || type.to_s.include?('/')
type = ".#{type}" unless type.to_s[0] == ?.
Rack::Mime.mime_type(type, nil)
return Rack::Mime.mime_type(type, nil) unless value
Rack::Mime::MIME_TYPES[type] = value
end
# Define a before filter. Filters are run before all requests

View File

@ -230,6 +230,19 @@ class HelpersTest < Test::Unit::TestCase
end
end
test 'Base.mime_type registers mime type' do
mock_app {
mime_type :foo, 'application/foo'
get '/' do
"foo is #{mime_type(:foo)}"
end
}
get '/'
assert_equal 'foo is application/foo', body
end
describe 'content_type' do
it 'sets the Content-Type header' do
mock_app {