a27e9519290f0f58ead423227ed6cfc62205e381
[feedcatcher.git] / vendor / rails / actionpack / test / controller / send_file_test.rb
1 require 'abstract_unit'
2
3 module TestFileUtils
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
7 end
8
9 class SendFileController < ActionController::Base
10 include TestFileUtils
11 layout "layouts/standard" # to make sure layouts don't interfere
12
13 attr_writer :options
14 def options() @options ||= {} end
15
16 def file() send_file(file_path, options) end
17 def data() send_data(file_data, options) end
18
19 def rescue_action(e) raise end
20 end
21
22 class SendFileTest < ActionController::TestCase
23 tests SendFileController
24 include TestFileUtils
25
26 Mime::Type.register "image/png", :png unless defined? Mime::PNG
27
28 def setup
29 @controller = SendFileController.new
30 @request = ActionController::TestRequest.new
31 @response = ActionController::TestResponse.new
32 end
33
34 def test_file_nostream
35 @controller.options = { :stream => false }
36 response = nil
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
41 end
42
43 def test_file_stream
44 response = nil
45 assert_nothing_raised { response = process('file') }
46 assert_not_nil response
47 assert_kind_of Proc, response.body
48
49 require 'stringio'
50 output = StringIO.new
51 output.binmode
52 assert_nothing_raised { response.body.call(response, output) }
53 assert_equal file_data, output.string
54 end
55
56 def test_file_url_based_filename
57 @controller.options = { :url_based_filename => true }
58 response = nil
59 assert_nothing_raised { response = process('file') }
60 assert_not_nil response
61 assert_equal "attachment", response.headers["Content-Disposition"]
62 end
63
64 def test_x_sendfile_header
65 @controller.options = { :x_sendfile => true }
66
67 response = nil
68 assert_nothing_raised { response = process('file') }
69 assert_not_nil response
70
71 assert_equal @controller.file_path, response.headers['X-Sendfile']
72 assert response.body.blank?
73 assert !response.etag?
74 end
75
76 def test_data
77 response = nil
78 assert_nothing_raised { response = process('data') }
79 assert_not_nil response
80
81 assert_kind_of String, response.body
82 assert_equal file_data, response.body
83 end
84
85 def test_headers_after_send_shouldnt_include_charset
86 response = process('data')
87 assert_equal "application/octet-stream", response.content_type
88
89 response = process('file')
90 assert_equal "application/octet-stream", response.content_type
91 end
92
93 # Test that send_file_headers! is setting the correct HTTP headers.
94 def test_send_file_headers!
95 options = {
96 :length => 1,
97 :type => Mime::PNG,
98 :disposition => 'disposition',
99 :filename => 'filename'
100 }
101
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)
109
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']
115
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']
121 end
122
123 def test_send_file_headers_with_mime_lookup_with_symbol
124 options = {
125 :length => 1,
126 :type => :png
127 }
128
129 @controller.headers = {}
130 @controller.send(:send_file_headers!, options)
131
132 headers = @controller.headers
133
134 assert_equal 'image/png', headers['Content-Type']
135 end
136
137
138 def test_send_file_headers_with_bad_symbol
139 options = {
140 :length => 1,
141 :type => :this_type_is_not_registered
142 }
143
144 @controller.headers = {}
145 assert_raise(ArgumentError){ @controller.send(:send_file_headers!, options) }
146 end
147
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
153 end
154
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
159 end
160 end
161 end