Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Only set default charset for specific mime type. Fixes #111.

  • Loading branch information...
commit aa06ed5e6d7e7a042dd1df2380dd07441819584c 1 parent e6cf5b3
@rkh rkh authored
Showing with 35 additions and 4 deletions.
  1. +7 −2 lib/sinatra/base.rb
  2. +28 −2 test/helpers_test.rb
View
9 lib/sinatra/base.rb
@@ -130,8 +130,12 @@ def mime_type(type)
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 @@ def force_encoding(*) end
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?
View
30 test/helpers_test.rb
@@ -320,7 +320,7 @@ def test_default
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 @@ def test_default
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 @@ def send_file_app(opts={})
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
Please sign in to comment.
Something went wrong with that request. Please try again.