Froze rails gems
[depot.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 < Test::Unit::TestCase
23 include TestFileUtils
24
25 Mime::Type.register "image/png", :png unless defined? Mime::PNG
26
27 def setup
28 @controller = SendFileController.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
31 end
32
33 def test_file_nostream
34 @controller.options = { :stream => false }
35 response = nil
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
40 end
41
42 def test_file_stream
43 response = nil
44 assert_nothing_raised { response = process('file') }
45 assert_not_nil response
46 assert_kind_of Proc, response.body
47
48 require 'stringio'
49 output = StringIO.new
50 output.binmode
51 assert_nothing_raised { response.body.call(response, output) }
52 assert_equal file_data, output.string
53 end
54
55 def test_file_url_based_filename
56 @controller.options = { :url_based_filename => true }
57 response = nil
58 assert_nothing_raised { response = process('file') }
59 assert_not_nil response
60 assert_equal "attachment", response.headers["Content-Disposition"]
61 end
62
63 def test_x_sendfile_header
64 @controller.options = { :x_sendfile => true }
65
66 response = nil
67 assert_nothing_raised { response = process('file') }
68 assert_not_nil response
69
70 assert_equal @controller.file_path, response.headers['X-Sendfile']
71 assert response.body.blank?
72 end
73
74 def test_data
75 response = nil
76 assert_nothing_raised { response = process('data') }
77 assert_not_nil response
78
79 assert_kind_of String, response.body
80 assert_equal file_data, response.body
81 end
82
83 def test_headers_after_send_shouldnt_include_charset
84 response = process('data')
85 assert_equal "application/octet-stream", response.content_type
86
87 response = process('file')
88 assert_equal "application/octet-stream", response.content_type
89 end
90
91 # Test that send_file_headers! is setting the correct HTTP headers.
92 def test_send_file_headers!
93 options = {
94 :length => 1,
95 :type => Mime::PNG,
96 :disposition => 'disposition',
97 :filename => 'filename'
98 }
99
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)
107
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']
113
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']
119 end
120
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']
126 end
127
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']
132 end
133 end
134 end