Permalink
Browse files

Handles `Content-Type` strings that already contain parameters correc…

…tly in

`content_type` (example: `content_type "text/plain; charset=utf-16"`).
  • Loading branch information...
1 parent cf47e56 commit abb5f3a78cd58390f8ec53e03fdb9c2c230646a2 @rkh rkh committed Mar 25, 2011
Showing with 31 additions and 1 deletion.
  1. +4 −0 CHANGES
  2. +5 −1 lib/sinatra/base.rb
  3. +22 −0 test/helpers_test.rb
View
@@ -16,6 +16,10 @@
* URIs passed to the `url` helper or `redirect` may now use any schema to be
identified as absolute URIs, not only `http` or `https`. (Konstantin Haase)
+ * Handles `Content-Type` strings that already contain parameters correctly in
+ `content_type` (example: `content_type "text/plain; charset=utf-16"`).
+ (Konstantin Haase)
+
= 1.2.1 / 2011-03-17
* Use a generated session secret when using `enable :sessions`. (Konstantin
View
@@ -186,7 +186,11 @@ def content_type(type = nil, params={})
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?
+ params.delete :charset if mime_type.include? 'charset'
+ unless params.empty?
+ mime_type << (mime_type.include?(';') ? ', ' : ';')
+ mime_type << params.map { |kv| kv.join('=') }.join(', ')
+ end
response['Content-Type'] = mime_type
end
View
@@ -429,6 +429,28 @@ def test_default
get '/'
assert tests_ran
end
+
+ it 'handles already present params' do
+ mock_app do
+ get '/' do
+ content_type 'foo/bar;level=1', :charset => 'utf-8'
+ 'ok'
+ end
+ end
+ get '/'
+ assert_equal 'foo/bar;level=1, charset=utf-8', response['Content-Type']
+ end
+
+ it 'does not add charset if present' do
+ mock_app do
+ get '/' do
+ content_type 'text/plain;charset=utf-16'
+ 'ok'
+ end
+ end
+ get '/'
+ assert_equal 'text/plain;charset=utf-16', response['Content-Type']
+ end
end
describe 'send_file' do

0 comments on commit abb5f3a

Please sign in to comment.