Actions that send files to browser smartly determine content-type HTTP header by the file name extnsion

This commit is contained in:
Alexey Verkhovsky 2005-01-22 14:58:43 +00:00
parent e9a419c40f
commit c30989c7eb
6 changed files with 62 additions and 16 deletions

View file

@ -2,12 +2,16 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'file_controller'
require 'fileutils'
# Raise errors beyond the default web-based presentation
class FileController; def rescue_action(e) logger.error(e); raise e end; end
class FileControllerTest < Test::Unit::TestCase
FILE_AREA = RAILS_ROOT + '/storage/test/wiki'
FileUtils.mkdir_p(FILE_AREA) unless File.directory?(FILE_AREA)
def setup
setup_test_wiki
setup_controller_test
@ -19,6 +23,29 @@ class FileControllerTest < Test::Unit::TestCase
def test_file
process 'file', 'web' => 'wiki', 'id' => 'foo.tgz'
assert_success
assert_rendered_file 'file/file'
end
def test_file_download_text_file
File.open(FILE_AREA + '/foo.txt', 'wb') { |f| f.write "aaa\nbbb\n" }
r = process 'file', 'web' => 'wiki', 'id' => 'foo.txt'
assert_success
assert_equal "aaa\nbbb\n", r.binary_content
assert_equal 'text/plain', r.headers['Content-Type']
end
def test_file_download_pdf_file
File.open(FILE_AREA + '/foo.pdf', 'wb') { |f| f.write "aaa\nbbb\n" }
r = process 'file', 'web' => 'wiki', 'id' => 'foo.pdf'
assert_success
assert_equal "aaa\nbbb\n", r.binary_content
assert_equal 'application/pdf', r.headers['Content-Type']
end
end