Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 914db7eefe6cfe14bd3432ca0f222895ccfc0155 1 parent ec5f606
@rkh rkh authored
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
4 CHANGES
@@ -36,6 +36,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
6 lib/sinatra/base.rb
@@ -181,7 +181,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
22 test/helpers_test.rb
@@ -442,6 +442,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
Please sign in to comment.
Something went wrong with that request. Please try again.