2009-08-01 22:38:05 -04:00
|
|
|
# encoding: utf-8
|
2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
module TestFileUtils
|
|
|
|
def file_name() File.basename(__FILE__) end
|
|
|
|
def file_path() File.expand_path(__FILE__) end
|
2009-08-01 23:07:28 -04:00
|
|
|
def file_data() @data ||= File.open(file_path, 'rb') { |f| f.read } end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class SendFileController < ActionController::Base
|
|
|
|
include TestFileUtils
|
2005-05-30 03:51:02 -04:00
|
|
|
layout "layouts/standard" # to make sure layouts don't interfere
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
attr_writer :options
|
2009-05-21 18:26:58 -04:00
|
|
|
def options
|
|
|
|
@options ||= {}
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2009-05-21 18:26:58 -04:00
|
|
|
def file
|
|
|
|
send_file(file_path, options)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2009-05-21 18:26:58 -04:00
|
|
|
def data
|
|
|
|
send_data(file_data, options)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2009-01-07 16:23:10 -05:00
|
|
|
class SendFileTest < ActionController::TestCase
|
|
|
|
tests SendFileController
|
2004-11-23 20:04:44 -05:00
|
|
|
include TestFileUtils
|
|
|
|
|
2007-02-25 15:16:58 -05:00
|
|
|
Mime::Type.register "image/png", :png unless defined? Mime::PNG
|
2008-01-11 17:07:04 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def setup
|
|
|
|
@controller = SendFileController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_nostream
|
|
|
|
@controller.options = { :stream => false }
|
|
|
|
response = nil
|
|
|
|
assert_nothing_raised { response = process('file') }
|
|
|
|
assert_not_nil response
|
2010-02-23 18:37:17 -05:00
|
|
|
body = response.body
|
|
|
|
assert_kind_of String, body
|
|
|
|
assert_equal file_data, body
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2009-06-17 19:51:51 -04:00
|
|
|
def test_file_stream
|
2010-02-19 10:31:10 -05:00
|
|
|
response = nil
|
|
|
|
assert_nothing_raised { response = process('file') }
|
|
|
|
assert_not_nil response
|
2010-05-17 14:40:56 -04:00
|
|
|
assert_respond_to response.body_parts, :each
|
|
|
|
assert_respond_to response.body_parts, :to_path
|
2009-05-21 15:48:26 -04:00
|
|
|
|
2010-02-19 10:31:10 -05:00
|
|
|
require 'stringio'
|
|
|
|
output = StringIO.new
|
|
|
|
output.binmode
|
2011-12-25 06:34:58 -05:00
|
|
|
output.string.force_encoding(file_data.encoding)
|
2010-02-19 10:31:10 -05:00
|
|
|
assert_nothing_raised { response.body_parts.each { |part| output << part.to_s } }
|
|
|
|
assert_equal file_data, output.string
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-11 17:07:04 -05:00
|
|
|
|
2007-02-09 06:25:37 -05:00
|
|
|
def test_file_url_based_filename
|
|
|
|
@controller.options = { :url_based_filename => true }
|
|
|
|
response = nil
|
|
|
|
assert_nothing_raised { response = process('file') }
|
|
|
|
assert_not_nil response
|
|
|
|
assert_equal "attachment", response.headers["Content-Disposition"]
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def test_data
|
|
|
|
response = nil
|
|
|
|
assert_nothing_raised { response = process('data') }
|
|
|
|
assert_not_nil response
|
|
|
|
|
|
|
|
assert_kind_of String, response.body
|
|
|
|
assert_equal file_data, response.body
|
|
|
|
end
|
2005-01-13 08:29:49 -05:00
|
|
|
|
2006-11-25 14:29:10 -05:00
|
|
|
def test_headers_after_send_shouldnt_include_charset
|
|
|
|
response = process('data')
|
2009-06-15 14:21:08 -04:00
|
|
|
assert_equal "application/octet-stream", response.headers["Content-Type"]
|
2006-11-25 14:29:10 -05:00
|
|
|
|
|
|
|
response = process('file')
|
2009-06-15 14:21:08 -04:00
|
|
|
assert_equal "application/octet-stream", response.headers["Content-Type"]
|
2006-11-25 14:29:10 -05:00
|
|
|
end
|
|
|
|
|
2005-01-13 08:29:49 -05:00
|
|
|
# Test that send_file_headers! is setting the correct HTTP headers.
|
2010-02-19 22:19:20 -05:00
|
|
|
def test_send_file_headers_bang
|
2005-01-13 08:29:49 -05:00
|
|
|
options = {
|
2007-02-25 15:16:58 -05:00
|
|
|
:type => Mime::PNG,
|
2005-01-13 08:29:49 -05:00
|
|
|
:disposition => 'disposition',
|
|
|
|
:filename => 'filename'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Do it a few times: the resulting headers should be identical
|
|
|
|
# no matter how many times you send with the same options.
|
|
|
|
# Test resolving Ticket #458.
|
|
|
|
@controller.headers = {}
|
|
|
|
@controller.send(:send_file_headers!, options)
|
|
|
|
@controller.send(:send_file_headers!, options)
|
|
|
|
@controller.send(:send_file_headers!, options)
|
|
|
|
|
|
|
|
h = @controller.headers
|
2009-06-15 14:21:08 -04:00
|
|
|
assert_equal 'image/png', @controller.content_type
|
2005-01-13 08:29:49 -05:00
|
|
|
assert_equal 'disposition; filename="filename"', h['Content-Disposition']
|
|
|
|
assert_equal 'binary', h['Content-Transfer-Encoding']
|
2006-06-01 20:51:56 -04:00
|
|
|
|
2005-09-25 02:53:42 -04:00
|
|
|
# test overriding Cache-Control: no-cache header to fix IE open/save dialog
|
|
|
|
@controller.send(:send_file_headers!, options)
|
2009-07-31 00:00:39 -04:00
|
|
|
@controller.response.prepare!
|
2005-09-25 02:53:42 -04:00
|
|
|
assert_equal 'private', h['Cache-Control']
|
2005-01-13 08:29:49 -05:00
|
|
|
end
|
2006-06-01 20:51:56 -04:00
|
|
|
|
2008-12-21 13:58:55 -05:00
|
|
|
def test_send_file_headers_with_mime_lookup_with_symbol
|
|
|
|
options = {
|
|
|
|
:type => :png
|
|
|
|
}
|
|
|
|
|
|
|
|
@controller.headers = {}
|
|
|
|
@controller.send(:send_file_headers!, options)
|
|
|
|
|
2009-06-15 14:21:08 -04:00
|
|
|
assert_equal 'image/png', @controller.content_type
|
2008-12-21 13:58:55 -05:00
|
|
|
end
|
2010-04-25 13:48:40 -04:00
|
|
|
|
2008-12-21 13:58:55 -05:00
|
|
|
|
|
|
|
def test_send_file_headers_with_bad_symbol
|
|
|
|
options = {
|
|
|
|
:type => :this_type_is_not_registered
|
|
|
|
}
|
|
|
|
|
|
|
|
@controller.headers = {}
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ArgumentError){ @controller.send(:send_file_headers!, options) }
|
2008-12-21 13:58:55 -05:00
|
|
|
end
|
2011-06-28 00:57:41 -04:00
|
|
|
|
|
|
|
def test_send_file_headers_guess_type_from_extension
|
|
|
|
{
|
|
|
|
'image.png' => 'image/png',
|
|
|
|
'image.jpeg' => 'image/jpeg',
|
|
|
|
'image.jpg' => 'image/jpeg',
|
|
|
|
'image.tif' => 'image/tiff',
|
|
|
|
'image.gif' => 'image/gif',
|
|
|
|
'movie.mpg' => 'video/mpeg',
|
|
|
|
'file.zip' => 'application/zip',
|
|
|
|
'file.unk' => 'application/octet-stream',
|
|
|
|
'zip' => 'application/octet-stream'
|
|
|
|
}.each do |filename,expected_type|
|
|
|
|
options = { :filename => filename }
|
|
|
|
@controller.headers = {}
|
|
|
|
@controller.send(:send_file_headers!, options)
|
|
|
|
assert_equal expected_type, @controller.content_type
|
|
|
|
end
|
|
|
|
end
|
2008-12-21 13:58:55 -05:00
|
|
|
|
2012-04-30 03:12:55 -04:00
|
|
|
def test_send_file_with_default_content_disposition_header
|
|
|
|
process('data')
|
|
|
|
assert_equal 'attachment', @controller.headers['Content-Disposition']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_without_content_disposition_header
|
|
|
|
@controller.options = {:disposition => nil}
|
|
|
|
process('data')
|
|
|
|
assert_nil @controller.headers['Content-Disposition']
|
|
|
|
end
|
|
|
|
|
2006-06-01 20:51:56 -04:00
|
|
|
%w(file data).each do |method|
|
|
|
|
define_method "test_send_#{method}_status" do
|
|
|
|
@controller.options = { :stream => false, :status => 500 }
|
|
|
|
assert_nothing_raised { assert_not_nil process(method) }
|
2009-05-02 15:57:40 -04:00
|
|
|
assert_equal 500, @response.status
|
2006-06-01 20:51:56 -04:00
|
|
|
end
|
|
|
|
|
2010-02-23 18:37:17 -05:00
|
|
|
define_method "test_send_#{method}_content_type" do
|
|
|
|
@controller.options = { :stream => false, :content_type => "application/x-ruby" }
|
|
|
|
assert_nothing_raised { assert_not_nil process(method) }
|
|
|
|
assert_equal "application/x-ruby", @response.content_type
|
|
|
|
end
|
|
|
|
|
2006-06-01 20:51:56 -04:00
|
|
|
define_method "test_default_send_#{method}_status" do
|
|
|
|
@controller.options = { :stream => false }
|
|
|
|
assert_nothing_raised { assert_not_nil process(method) }
|
2009-05-02 15:57:40 -04:00
|
|
|
assert_equal 200, @response.status
|
2006-06-01 20:51:56 -04:00
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|