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
|
|
|
|
def file_data() File.open(file_path, 'rb') { |f| f.read } end
|
|
|
|
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
|
|
|
|
def options() @options ||= {} end
|
|
|
|
|
|
|
|
def file() send_file(file_path, options) end
|
|
|
|
def data() send_data(file_data, options) end
|
|
|
|
|
|
|
|
def rescue_action(e) raise end
|
|
|
|
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
|
|
|
|
assert_kind_of String, response.body
|
|
|
|
assert_equal file_data, response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_stream
|
|
|
|
response = nil
|
|
|
|
assert_nothing_raised { response = process('file') }
|
|
|
|
assert_not_nil response
|
2009-03-13 00:47:34 -04:00
|
|
|
assert_kind_of Proc, response.body_parts
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-09-20 06:53:33 -04:00
|
|
|
require 'stringio'
|
|
|
|
output = StringIO.new
|
|
|
|
output.binmode
|
2009-03-13 00:47:34 -04:00
|
|
|
assert_nothing_raised { response.body_parts.call(response, output) }
|
2005-09-20 06:53:33 -04:00
|
|
|
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
|
|
|
|
2008-01-11 17:07:04 -05:00
|
|
|
def test_x_sendfile_header
|
|
|
|
@controller.options = { :x_sendfile => true }
|
|
|
|
|
|
|
|
response = nil
|
|
|
|
assert_nothing_raised { response = process('file') }
|
|
|
|
assert_not_nil response
|
|
|
|
|
|
|
|
assert_equal @controller.file_path, response.headers['X-Sendfile']
|
|
|
|
assert response.body.blank?
|
2008-12-15 15:36:33 -05:00
|
|
|
assert !response.etag?
|
2008-01-11 17:07:04 -05:00
|
|
|
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')
|
2007-02-18 21:25:01 -05:00
|
|
|
assert_equal "application/octet-stream", response.content_type
|
2006-11-25 14:29:10 -05:00
|
|
|
|
|
|
|
response = process('file')
|
2007-02-18 21:25:01 -05:00
|
|
|
assert_equal "application/octet-stream", response.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.
|
|
|
|
def test_send_file_headers!
|
|
|
|
options = {
|
|
|
|
:length => 1,
|
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
|
|
|
|
assert_equal 1, h['Content-Length']
|
2007-02-25 15:16:58 -05:00
|
|
|
assert_equal 'image/png', h['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.headers = { 'Cache-Control' => 'no-cache' }
|
|
|
|
@controller.send(:send_file_headers!, options)
|
|
|
|
h = @controller.headers
|
|
|
|
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 = {
|
|
|
|
:length => 1,
|
|
|
|
:type => :png
|
|
|
|
}
|
|
|
|
|
|
|
|
@controller.headers = {}
|
|
|
|
@controller.send(:send_file_headers!, options)
|
|
|
|
|
|
|
|
headers = @controller.headers
|
|
|
|
|
|
|
|
assert_equal 'image/png', headers['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_file_headers_with_bad_symbol
|
|
|
|
options = {
|
|
|
|
:length => 1,
|
|
|
|
: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
|
|
|
|
|
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) }
|
2008-12-19 16:05:51 -05:00
|
|
|
assert_equal '500 Internal Server Error', @response.status
|
2006-06-01 20:51:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
define_method "test_default_send_#{method}_status" do
|
|
|
|
@controller.options = { :stream => false }
|
|
|
|
assert_nothing_raised { assert_not_nil process(method) }
|
2009-01-27 19:17:39 -05:00
|
|
|
assert_equal ActionController::DEFAULT_RENDER_STATUS_CODE, @response.status
|
2006-06-01 20:51:56 -04:00
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|