In send_file: Do not override Content-Type if already set and no explicit type

is given.

Fixes #130.
This commit is contained in:
Konstantin Haase 2010-12-08 12:01:24 +01:00
parent b30d664ea6
commit 88d378ab94
2 changed files with 27 additions and 2 deletions

View File

@ -166,8 +166,9 @@ module Sinatra
stat = File.stat(path)
last_modified stat.mtime
content_type opts[:type] || File.extname(path),
:default => response['Content-Type'] || 'application/octet-stream'
if opts[:type] or not response['Content-Type']
content_type opts[:type] || File.extname(path), :default => 'application/octet-stream'
end
if opts[:disposition] == 'attachment' || opts[:filename]
attachment opts[:filename] || path

View File

@ -454,6 +454,30 @@ class HelpersTest < Test::Unit::TestCase
get '/file.txt'
assert_equal 'application/octet-stream', response['Content-Type']
end
it "does not override Content-Type if already set and no explicit type is given" do
path = @file
mock_app do
get '/' do
content_type :png
send_file path
end
end
get '/'
assert_equal 'image/png', response['Content-Type']
end
it "does override Content-Type even if already set, if explicit type is given" do
path = @file
mock_app do
get '/' do
content_type :png
send_file path, :type => :gif
end
end
get '/'
assert_equal 'image/gif', response['Content-Type']
end
end
describe 'cache_control' do