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
< ActionController
::TestCase
23 tests SendFileController
26 Mime
::Type.register
"image/png", :png unless defined? Mime
::PNG
29 @controller = SendFileController
.new
30 @request = ActionController
::TestRequest.new
31 @response = ActionController
::TestResponse.new
34 def test_file_nostream
35 @controller.options
= { :stream => false }
37 assert_nothing_raised
{ response
= process('file') }
38 assert_not_nil response
39 assert_kind_of String
, response
.body
40 assert_equal file_data
, response
.body
45 assert_nothing_raised
{ response
= process('file') }
46 assert_not_nil response
47 assert_kind_of Proc
, response
.body
52 assert_nothing_raised
{ response
.body
.call(response
, output
) }
53 assert_equal file_data
, output
.string
56 def test_file_url_based_filename
57 @controller.options
= { :url_based_filename => true }
59 assert_nothing_raised
{ response
= process('file') }
60 assert_not_nil response
61 assert_equal
"attachment", response
.headers
["Content-Disposition"]
64 def test_x_sendfile_header
65 @controller.options
= { :x_sendfile => true }
68 assert_nothing_raised
{ response
= process('file') }
69 assert_not_nil response
71 assert_equal
@controller.file_path
, response
.headers
['X-Sendfile']
72 assert response
.body
.blank
?
73 assert
!response
.etag
?
78 assert_nothing_raised
{ response
= process('data') }
79 assert_not_nil response
81 assert_kind_of String
, response
.body
82 assert_equal file_data
, response
.body
85 def test_headers_after_send_shouldnt_include_charset
86 response
= process('data')
87 assert_equal
"application/octet-stream", response
.content_type
89 response
= process('file')
90 assert_equal
"application/octet-stream", response
.content_type
93 # Test that send_file_headers! is setting the correct HTTP headers.
94 def test_send_file_headers
!
98 :disposition => 'disposition',
99 :filename => 'filename'
102 # Do it a few times: the resulting headers should be identical
103 # no matter how many times you send with the same options.
104 # Test resolving Ticket #458.
105 @controller.headers
= {}
106 @controller.send(:send_file_headers!, options
)
107 @controller.send(:send_file_headers!, options
)
108 @controller.send(:send_file_headers!, options
)
110 h
= @controller.headers
111 assert_equal
1, h
['Content-Length']
112 assert_equal
'image/png', h
['Content-Type']
113 assert_equal
'disposition; filename="filename"', h
['Content-Disposition']
114 assert_equal
'binary', h
['Content-Transfer-Encoding']
116 # test overriding Cache-Control: no-cache header to fix IE open/save dialog
117 @controller.headers
= { 'Cache-Control' => 'no-cache' }
118 @controller.send(:send_file_headers!, options
)
119 h
= @controller.headers
120 assert_equal
'private', h
['Cache-Control']
123 def test_send_file_headers_with_mime_lookup_with_symbol
129 @controller.headers
= {}
130 @controller.send(:send_file_headers!, options
)
132 headers
= @controller.headers
134 assert_equal
'image/png', headers
['Content-Type']
138 def test_send_file_headers_with_bad_symbol
141 :type => :this_type_is_not_registered
144 @controller.headers
= {}
145 assert_raise(ArgumentError
){ @controller.send(:send_file_headers!, options
) }
148 %w(file
data).each
do |method
|
149 define_method
"test_send_#{method}_status" do
150 @controller.options
= { :stream => false, :status => 500 }
151 assert_nothing_raised
{ assert_not_nil
process(method
) }
152 assert_equal
'500 Internal Server Error', @response.status
155 define_method
"test_default_send_#{method}_status" do
156 @controller.options
= { :stream => false }
157 assert_nothing_raised
{ assert_not_nil
process(method
) }
158 assert_equal ActionController
::Base::DEFAULT_RENDER_STATUS_CODE, @response.status