Only set default charset for specific mime type. Fixes #111.
This commit is contained in:
parent
e6cf5b30d7
commit
aa06ed5e6d
|
@ -130,8 +130,12 @@ module Sinatra
|
|||
def content_type(type, params={})
|
||||
mime_type = mime_type(type)
|
||||
fail "Unknown media type: %p" % type if mime_type.nil?
|
||||
params[:charset] ||= params.delete('charset') || settings.default_encoding
|
||||
response['Content-Type'] = "#{mime_type};#{params.map { |kv| kv.join('=') }.join(', ')}"
|
||||
mime_type = mime_type.dup
|
||||
unless params.include? :charset or settings.add_charset.all? { |p| not p === mime_type }
|
||||
params[:charset] = params.delete('charset') || settings.default_encoding
|
||||
end
|
||||
mime_type << ";#{params.map { |kv| kv.join('=') }.join(', ')}" unless params.empty?
|
||||
response['Content-Type'] = mime_type
|
||||
end
|
||||
|
||||
# Set the Content-Disposition to "attachment" with the specified filename,
|
||||
|
@ -1238,6 +1242,7 @@ module Sinatra
|
|||
set :logging, false
|
||||
set :method_override, false
|
||||
set :default_encoding, "utf-8"
|
||||
set :add_charset, [/^text\//, 'application/javascript', 'application/xml', 'application/xhtml+xml']
|
||||
|
||||
class << self
|
||||
alias_method :methodoverride?, :method_override?
|
||||
|
|
|
@ -320,7 +320,7 @@ class HelpersTest < Test::Unit::TestCase
|
|||
|
||||
get '/foo.xml'
|
||||
assert ok?
|
||||
assert_equal 'application/foo;charset=utf-8', response['Content-Type']
|
||||
assert_equal 'application/foo', response['Content-Type']
|
||||
assert_equal 'I AM FOO', body
|
||||
end
|
||||
|
||||
|
@ -334,6 +334,32 @@ class HelpersTest < Test::Unit::TestCase
|
|||
|
||||
assert_raise(RuntimeError) { get '/foo.xml' }
|
||||
end
|
||||
|
||||
it 'only sets default charset for specific mime types' do
|
||||
tests_ran = false
|
||||
mock_app do
|
||||
mime_type :foo, 'text/foo'
|
||||
mime_type :bar, 'application/bar'
|
||||
mime_type :baz, 'application/baz'
|
||||
add_charset << mime_type(:baz)
|
||||
get '/' do
|
||||
assert_equal content_type(:txt), 'text/plain;charset=utf-8'
|
||||
assert_equal content_type(:css), 'text/css;charset=utf-8'
|
||||
assert_equal content_type(:html), 'text/html;charset=utf-8'
|
||||
assert_equal content_type(:foo), 'text/foo;charset=utf-8'
|
||||
assert_equal content_type(:xml), 'application/xml;charset=utf-8'
|
||||
assert_equal content_type(:xhtml), 'application/xhtml+xml;charset=utf-8'
|
||||
assert_equal content_type(:js), 'application/javascript;charset=utf-8'
|
||||
assert_equal content_type(:bar), 'application/bar'
|
||||
assert_equal content_type(:png), 'image/png'
|
||||
assert_equal content_type(:baz), 'application/baz;charset=utf-8'
|
||||
tests_ran = true
|
||||
"done"
|
||||
end
|
||||
end
|
||||
get '/'
|
||||
assert tests_ran
|
||||
end
|
||||
end
|
||||
|
||||
describe 'send_file' do
|
||||
|
@ -378,7 +404,7 @@ class HelpersTest < Test::Unit::TestCase
|
|||
it 'sets the Content-Type response header if type option is set to a mime type' do
|
||||
send_file_app :type => 'application/octet-stream'
|
||||
get '/file.txt'
|
||||
assert_equal 'application/octet-stream;charset=utf-8', response['Content-Type']
|
||||
assert_equal 'application/octet-stream', response['Content-Type']
|
||||
end
|
||||
|
||||
it 'sets the Content-Length response header' do
|
||||
|
|
Loading…
Reference in New Issue