mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Rename media_type to mime_type for symmetry with Rack::Mime.mime_type
This commit is contained in:
parent
df210273d4
commit
31bf026087
2 changed files with 18 additions and 18 deletions
|
@ -109,20 +109,20 @@ module Sinatra
|
|||
end
|
||||
|
||||
# Look up a media type by file extension in Rack's mime registry.
|
||||
def media_type(type)
|
||||
Base.media_type(type)
|
||||
def mime_type(type)
|
||||
Base.mime_type(type)
|
||||
end
|
||||
|
||||
# Set the Content-Type of the response body given a media type or file
|
||||
# extension.
|
||||
def content_type(type, params={})
|
||||
media_type = self.media_type(type)
|
||||
fail "Unknown media type: %p" % type if media_type.nil?
|
||||
mime_type = self.mime_type(type)
|
||||
fail "Unknown media type: %p" % type if mime_type.nil?
|
||||
if params.any?
|
||||
params = params.collect { |kv| "%s=%s" % kv }.join(', ')
|
||||
response['Content-Type'] = [media_type, params].join(";")
|
||||
response['Content-Type'] = [mime_type, params].join(";")
|
||||
else
|
||||
response['Content-Type'] = media_type
|
||||
response['Content-Type'] = mime_type
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -141,8 +141,8 @@ module Sinatra
|
|||
stat = File.stat(path)
|
||||
last_modified stat.mtime
|
||||
|
||||
content_type media_type(opts[:type]) ||
|
||||
media_type(File.extname(path)) ||
|
||||
content_type mime_type(opts[:type]) ||
|
||||
mime_type(File.extname(path)) ||
|
||||
response['Content-Type'] ||
|
||||
'application/octet-stream'
|
||||
|
||||
|
@ -731,7 +731,7 @@ module Sinatra
|
|||
end
|
||||
|
||||
# Look up a media type by file extension in Rack's mime registry.
|
||||
def media_type(type)
|
||||
def mime_type(type)
|
||||
return type if type.nil? || type.to_s.include?('/')
|
||||
type = ".#{type}" unless type.to_s[0] == ?.
|
||||
Rack::Mime.mime_type(type, nil)
|
||||
|
@ -769,7 +769,7 @@ module Sinatra
|
|||
|
||||
def provides(*types)
|
||||
types = [types] unless types.kind_of? Array
|
||||
types.map!{|t| media_type(t)}
|
||||
types.map!{|t| mime_type(t)}
|
||||
|
||||
condition {
|
||||
matching_types = (request.accept & types)
|
||||
|
|
|
@ -207,26 +207,26 @@ class HelpersTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
describe 'media_type' do
|
||||
describe 'mime_type' do
|
||||
include Sinatra::Helpers
|
||||
|
||||
it "looks up media types in Rack's MIME registry" do
|
||||
it "looks up mime types in Rack's MIME registry" do
|
||||
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
|
||||
assert_equal 'application/foo', media_type('foo')
|
||||
assert_equal 'application/foo', media_type('.foo')
|
||||
assert_equal 'application/foo', media_type(:foo)
|
||||
assert_equal 'application/foo', mime_type('foo')
|
||||
assert_equal 'application/foo', mime_type('.foo')
|
||||
assert_equal 'application/foo', mime_type(:foo)
|
||||
end
|
||||
|
||||
it 'returns nil when given nil' do
|
||||
assert media_type(nil).nil?
|
||||
assert mime_type(nil).nil?
|
||||
end
|
||||
|
||||
it 'returns nil when media type not registered' do
|
||||
assert media_type(:bizzle).nil?
|
||||
assert mime_type(:bizzle).nil?
|
||||
end
|
||||
|
||||
it 'returns the argument when given a media type string' do
|
||||
assert_equal 'text/plain', media_type('text/plain')
|
||||
assert_equal 'text/plain', mime_type('text/plain')
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue