Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

In send_file: Do not override Content-Type if already set and no expl…

…icit type

is given.

Fixes #130.
  • Loading branch information...
commit ce2e61c81f10df340d350acd748d7b550783d151 1 parent 9d5a718
@rkh rkh authored
Showing with 27 additions and 2 deletions.
  1. +3 −2 lib/sinatra/base.rb
  2. +24 −0 test/helpers_test.rb
View
5 lib/sinatra/base.rb
@@ -166,8 +166,9 @@ def send_file(path, opts={})
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
24 test/helpers_test.rb
@@ -454,6 +454,30 @@ def send_file_app(opts={})
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
Please sign in to comment.
Something went wrong with that request. Please try again.