2008-01-05 13:32:06 +00:00
|
|
|
require 'abstract_unit'
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
module TestFileUtils
|
|
|
|
def file_name() File.basename(__FILE__) end
|
|
|
|
def file_path() File.expand_path(__FILE__) end
|
2009-08-02 03:07:28 +00:00
|
|
|
def file_data() @data ||= File.open(file_path, 'rb') { |f| f.read } end
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class SendFileController < ActionController::Base
|
|
|
|
include TestFileUtils
|
2014-06-05 19:31:35 +00:00
|
|
|
include ActionController::Testing
|
2005-05-30 07:51:02 +00:00
|
|
|
layout "layouts/standard" # to make sure layouts don't interfere
|
2004-11-24 01:04:44 +00:00
|
|
|
|
|
|
|
attr_writer :options
|
2009-05-21 22:26:58 +00:00
|
|
|
def options
|
|
|
|
@options ||= {}
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2009-05-21 22:26:58 +00:00
|
|
|
def file
|
|
|
|
send_file(file_path, options)
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
|
2015-09-23 22:44:18 +00:00
|
|
|
def test_send_file_headers_bang
|
|
|
|
options = {
|
|
|
|
:type => Mime::Type[:PNG],
|
|
|
|
:disposition => 'disposition',
|
|
|
|
:filename => 'filename'
|
|
|
|
}
|
|
|
|
|
|
|
|
self.headers = {}
|
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_with_disposition_as_a_symbol
|
|
|
|
options = {
|
|
|
|
:type => Mime::Type[:PNG],
|
|
|
|
:disposition => :disposition,
|
|
|
|
:filename => 'filename'
|
|
|
|
}
|
|
|
|
|
|
|
|
self.headers = {}
|
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_with_mime_lookup_with_symbol
|
|
|
|
options = {
|
|
|
|
:type => :png
|
|
|
|
}
|
|
|
|
|
|
|
|
self.headers = {}
|
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_with_bad_symbol
|
|
|
|
options = { :type => :this_type_is_not_registered }
|
|
|
|
self.headers = {}
|
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_with_nil_content_type
|
|
|
|
options = { :type => nil }
|
|
|
|
self.headers = {}
|
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_guess_type_from_extension
|
|
|
|
options = { :filename => params[:filename] }
|
|
|
|
self.headers = {}
|
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
2009-05-21 22:26:58 +00:00
|
|
|
def data
|
|
|
|
send_data(file_data, options)
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2013-11-29 01:01:57 +00:00
|
|
|
class SendFileWithActionControllerLive < SendFileController
|
|
|
|
include ActionController::Live
|
|
|
|
end
|
|
|
|
|
2009-01-07 21:23:10 +00:00
|
|
|
class SendFileTest < ActionController::TestCase
|
2004-11-24 01:04:44 +00:00
|
|
|
include TestFileUtils
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@controller = SendFileController.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 23:37:17 +00:00
|
|
|
body = response.body
|
|
|
|
assert_kind_of String, body
|
|
|
|
assert_equal file_data, body
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2009-06-17 23:51:51 +00:00
|
|
|
def test_file_stream
|
2010-02-19 15:31:10 +00:00
|
|
|
response = nil
|
|
|
|
assert_nothing_raised { response = process('file') }
|
|
|
|
assert_not_nil response
|
2012-07-30 00:02:00 +00:00
|
|
|
assert_respond_to response.stream, :each
|
|
|
|
assert_respond_to response.stream, :to_path
|
2009-05-21 19:48:26 +00:00
|
|
|
|
2010-02-19 15:31:10 +00:00
|
|
|
require 'stringio'
|
|
|
|
output = StringIO.new
|
|
|
|
output.binmode
|
2011-12-25 11:34:58 +00:00
|
|
|
output.string.force_encoding(file_data.encoding)
|
2012-07-30 00:02:00 +00:00
|
|
|
response.body_parts.each { |part| output << part.to_s }
|
2010-02-19 15:31:10 +00:00
|
|
|
assert_equal file_data, output.string
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2008-01-11 22:07:04 +00:00
|
|
|
|
2007-02-09 11:25:37 +00: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-24 01:04:44 +00: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 13:29:49 +00:00
|
|
|
|
2006-11-25 19:29:10 +00:00
|
|
|
def test_headers_after_send_shouldnt_include_charset
|
|
|
|
response = process('data')
|
2009-06-15 18:21:08 +00:00
|
|
|
assert_equal "application/octet-stream", response.headers["Content-Type"]
|
2006-11-25 19:29:10 +00:00
|
|
|
|
|
|
|
response = process('file')
|
2009-06-15 18:21:08 +00:00
|
|
|
assert_equal "application/octet-stream", response.headers["Content-Type"]
|
2006-11-25 19:29:10 +00:00
|
|
|
end
|
|
|
|
|
2005-01-13 13:29:49 +00:00
|
|
|
# Test that send_file_headers! is setting the correct HTTP headers.
|
2010-02-20 03:19:20 +00:00
|
|
|
def test_send_file_headers_bang
|
2005-01-13 13:29:49 +00:00
|
|
|
# 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.
|
2015-09-23 22:44:18 +00:00
|
|
|
5.times do
|
|
|
|
get :test_send_file_headers_bang
|
2006-06-02 00:51:56 +00:00
|
|
|
|
2015-09-23 22:44:18 +00:00
|
|
|
assert_equal 'image/png', response.content_type
|
|
|
|
assert_equal 'disposition; filename="filename"', response.get_header('Content-Disposition')
|
|
|
|
assert_equal 'binary', response.get_header('Content-Transfer-Encoding')
|
|
|
|
assert_equal 'private', response.get_header('Cache-Control')
|
|
|
|
end
|
2005-01-13 13:29:49 +00:00
|
|
|
end
|
2006-06-02 00:51:56 +00:00
|
|
|
|
2012-11-26 22:49:14 +00:00
|
|
|
def test_send_file_headers_with_disposition_as_a_symbol
|
2015-09-23 22:44:18 +00:00
|
|
|
get :test_send_file_headers_with_disposition_as_a_symbol
|
2012-11-26 22:49:14 +00:00
|
|
|
|
2015-09-23 22:44:18 +00:00
|
|
|
assert_equal 'disposition; filename="filename"', response.get_header('Content-Disposition')
|
2012-11-26 22:49:14 +00:00
|
|
|
end
|
|
|
|
|
2008-12-21 18:58:55 +00:00
|
|
|
def test_send_file_headers_with_mime_lookup_with_symbol
|
2015-09-23 22:44:18 +00:00
|
|
|
get __method__
|
|
|
|
assert_equal 'image/png', response.content_type
|
2008-12-21 18:58:55 +00:00
|
|
|
end
|
2010-04-25 17:48:40 +00:00
|
|
|
|
2008-12-21 18:58:55 +00:00
|
|
|
|
|
|
|
def test_send_file_headers_with_bad_symbol
|
2015-09-23 22:44:18 +00:00
|
|
|
error = assert_raise(ArgumentError) { get __method__ }
|
|
|
|
assert_equal "Unknown MIME type this_type_is_not_registered", error.message
|
2015-09-19 13:05:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_with_nil_content_type
|
2015-09-23 22:44:18 +00:00
|
|
|
error = assert_raise(ArgumentError) { get __method__ }
|
2015-09-19 13:05:41 +00:00
|
|
|
assert_equal ":type option required", error.message
|
2008-12-21 18:58:55 +00:00
|
|
|
end
|
2012-10-27 14:03:18 +00:00
|
|
|
|
2011-06-28 04:57:41 +00: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|
|
2015-09-23 22:44:18 +00:00
|
|
|
get __method__, params: { filename: filename }
|
|
|
|
assert_equal expected_type, response.content_type
|
2011-06-28 04:57:41 +00:00
|
|
|
end
|
|
|
|
end
|
2008-12-21 18:58:55 +00:00
|
|
|
|
2012-04-30 07:12:55 +00: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-02 00:51:56 +00: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 19:57:40 +00:00
|
|
|
assert_equal 500, @response.status
|
2006-06-02 00:51:56 +00:00
|
|
|
end
|
|
|
|
|
2010-02-23 23:37:17 +00: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-02 00:51:56 +00:00
|
|
|
define_method "test_default_send_#{method}_status" do
|
|
|
|
@controller.options = { :stream => false }
|
|
|
|
assert_nothing_raised { assert_not_nil process(method) }
|
2009-05-02 19:57:40 +00:00
|
|
|
assert_equal 200, @response.status
|
2006-06-02 00:51:56 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-29 01:01:57 +00:00
|
|
|
|
|
|
|
def test_send_file_with_action_controller_live
|
|
|
|
@controller = SendFileWithActionControllerLive.new
|
|
|
|
@controller.options = { :content_type => "application/x-ruby" }
|
|
|
|
|
|
|
|
response = process('file')
|
|
|
|
assert_equal 200, response.status
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|