1 require File.join(File.dirname(__FILE__), '..', 'abstract_unit')
5 def file_name() File.basename(__FILE__) end
6 def file_path() File.expand_path(__FILE__) end
7 def file_data() File.open(file_path, 'rb') { |f| f.read } end
11 class SendFileController < ActionController::Base
13 layout "layouts/standard" # to make sure layouts don't interfere
16 def options() @options ||= {} end
18 def file() send_file(file_path, options) end
19 def data() send_data(file_data, options) end
21 def rescue_action(e) raise end
24 SendFileController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
26 class SendFileTest < Test::Unit::TestCase
29 Mime::Type.register "image/png", :png unless defined? Mime::PNG
32 @controller = SendFileController.new
33 @request = ActionController::TestRequest.new
34 @response = ActionController::TestResponse.new
37 def test_file_nostream
38 @controller.options = { :stream => false }
40 assert_nothing_raised { response = process('file') }
41 assert_not_nil response
42 assert_kind_of String, response.body
43 assert_equal file_data, response.body
48 assert_nothing_raised { response = process('file') }
49 assert_not_nil response
50 assert_kind_of Proc, response.body
55 assert_nothing_raised { response.body.call(response, output) }
56 assert_equal file_data, output.string
59 def test_file_url_based_filename
60 @controller.options = { :url_based_filename => true }
62 assert_nothing_raised { response = process('file') }
63 assert_not_nil response
64 assert_equal "attachment", response.headers["Content-Disposition"]
69 assert_nothing_raised { response = process('data') }
70 assert_not_nil response
72 assert_kind_of String, response.body
73 assert_equal file_data, response.body
76 def test_headers_after_send_shouldnt_include_charset
77 response = process('data')
78 assert_equal "application/octet-stream", response.content_type
80 response = process('file')
81 assert_equal "application/octet-stream", response.content_type
84 # Test that send_file_headers! is setting the correct HTTP headers.
85 def test_send_file_headers!
89 :disposition => 'disposition',
90 :filename => 'filename'
93 # Do it a few times: the resulting headers should be identical
94 # no matter how many times you send with the same options.
95 # Test resolving Ticket #458.
96 @controller.headers = {}
97 @controller.send(:send_file_headers!, options)
98 @controller.send(:send_file_headers!, options)
99 @controller.send(:send_file_headers!, options)
101 h = @controller.headers
102 assert_equal 1, h['Content-Length']
103 assert_equal 'image/png', h['Content-Type']
104 assert_equal 'disposition; filename="filename"', h['Content-Disposition']
105 assert_equal 'binary', h['Content-Transfer-Encoding']
107 # test overriding Cache-Control: no-cache header to fix IE open/save dialog
108 @controller.headers = { 'Cache-Control' => 'no-cache' }
109 @controller.send(:send_file_headers!, options)
110 h = @controller.headers
111 assert_equal 'private', h['Cache-Control']
114 %w(file data).each do |method|
115 define_method "test_send_#{method}_status" do
116 @controller.options = { :stream => false, :status => 500 }
117 assert_nothing_raised { assert_not_nil process(method) }
118 assert_equal '500 Internal Server Error', @response.headers['Status']
121 define_method "test_default_send_#{method}_status" do
122 @controller.options = { :stream => false }
123 assert_nothing_raised { assert_not_nil process(method) }
124 assert_equal ActionController::Base::DEFAULT_RENDER_STATUS_CODE, @response.headers['Status']