1 require 'abstract_unit'
4 def file_name() File
.basename(__FILE__
) end
5 def file_path() File
.expand_path(__FILE__
) end
6 def file_data() File
.open(file_path
, 'rb') { |f
| f
.read
} end
9 class SendFileController
< ActionController
::Base
11 layout
"layouts/standard" # to make sure layouts don't interfere
14 def options() @options ||= {} end
16 def file() send_file(file_path
, options
) end
17 def data() send_data(file_data
, options
) end
19 def rescue_action(e
) raise end
22 class SendFileTest
< Test
::Unit::TestCase
25 Mime
::Type.register
"image/png", :png unless defined? Mime
::PNG
28 @controller = SendFileController
.new
29 @request = ActionController
::TestRequest.new
30 @response = ActionController
::TestResponse.new
33 def test_file_nostream
34 @controller.options
= { :stream => false }
36 assert_nothing_raised
{ response
= process('file') }
37 assert_not_nil response
38 assert_kind_of String
, response
.body
39 assert_equal file_data
, response
.body
44 assert_nothing_raised
{ response
= process('file') }
45 assert_not_nil response
46 assert_kind_of Proc
, response
.body
51 assert_nothing_raised
{ response
.body
.call(response
, output
) }
52 assert_equal file_data
, output
.string
55 def test_file_url_based_filename
56 @controller.options
= { :url_based_filename => true }
58 assert_nothing_raised
{ response
= process('file') }
59 assert_not_nil response
60 assert_equal
"attachment", response
.headers
["Content-Disposition"]
63 def test_x_sendfile_header
64 @controller.options
= { :x_sendfile => true }
67 assert_nothing_raised
{ response
= process('file') }
68 assert_not_nil response
70 assert_equal
@controller.file_path
, response
.headers
['X-Sendfile']
71 assert response
.body
.blank
?
76 assert_nothing_raised
{ response
= process('data') }
77 assert_not_nil response
79 assert_kind_of String
, response
.body
80 assert_equal file_data
, response
.body
83 def test_headers_after_send_shouldnt_include_charset
84 response
= process('data')
85 assert_equal
"application/octet-stream", response
.content_type
87 response
= process('file')
88 assert_equal
"application/octet-stream", response
.content_type
91 # Test that send_file_headers! is setting the correct HTTP headers.
92 def test_send_file_headers
!
96 :disposition => 'disposition',
97 :filename => 'filename'
100 # Do it a few times: the resulting headers should be identical
101 # no matter how many times you send with the same options.
102 # Test resolving Ticket #458.
103 @controller.headers
= {}
104 @controller.send(:send_file_headers!, options
)
105 @controller.send(:send_file_headers!, options
)
106 @controller.send(:send_file_headers!, options
)
108 h
= @controller.headers
109 assert_equal
1, h
['Content-Length']
110 assert_equal
'image/png', h
['Content-Type']
111 assert_equal
'disposition; filename="filename"', h
['Content-Disposition']
112 assert_equal
'binary', h
['Content-Transfer-Encoding']
114 # test overriding Cache-Control: no-cache header to fix IE open/save dialog
115 @controller.headers
= { 'Cache-Control' => 'no-cache' }
116 @controller.send(:send_file_headers!, options
)
117 h
= @controller.headers
118 assert_equal
'private', h
['Cache-Control']
121 %w(file
data).each
do |method
|
122 define_method
"test_send_#{method}_status" do
123 @controller.options
= { :stream => false, :status => 500 }
124 assert_nothing_raised
{ assert_not_nil
process(method
) }
125 assert_equal
'500 Internal Server Error', @response.headers
['Status']
128 define_method
"test_default_send_#{method}_status" do
129 @controller.options
= { :stream => false }
130 assert_nothing_raised
{ assert_not_nil
process(method
) }
131 assert_equal ActionController
::Base::DEFAULT_RENDER_STATUS_CODE, @response.headers
['Status']