1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/controller/send_file_test.rb
Carl Lerche & Yehuda Katz 906aebceed Bring abstract_controller up to date with rails/master
Resolved all the conflicts since 2.3.0 -> HEAD. Following is a list
of commits that could not be applied cleanly or are obviated with the
abstract_controller refactor. They all need to be revisited to ensure
that fixes made in 2.3 do not reappear in 3.0:

2259ecf368
AR not available
  * This will be reimplemented with ActionORM or equivalent

06182ea02e
implicitly rendering a js response should not use the default layout
[#1844 state:resolved]
  * This will be handled generically

893e9eb995
Improve view rendering performance in development mode and reinstate
template recompiling in production [#1909 state:resolved]
  * We will need to reimplement rails-dev-boost on top of the refactor;
    the changes here are very implementation specific and cannot be
    cleanly applied. The following commits are implicated:

      199e750d46
      3942cb406e
      f8ea9f85d4
      e3b166aab3
      ae9f258e03
      44423126c6

0cb020b4d6
workaround for picking layouts based on wrong view_paths
[#1974 state:resolved]
  * The specifics of this commit no longer apply. Since it is a two-line
    commit, we will reimplement this change.

8c5cc66a83
make action_controller/layouts pick templates from the current instance's
view_paths instead of the class view_paths [#1974 state:resolved]
  * This does not apply at all. It should be trivial to apply the feature
    to the reimplemented ActionController::Base.

87e8b16246
fix HTML fallback for explicit templates [#2052 state:resolved]
  * There were a number of patches related to this that simply compounded
    each other. Basically none of them apply cleanly, and the underlying
    issue needs to be revisited. After discussing the underlying problem
    with Koz, we will defer these fixes for further discussion.
2009-04-13 15:18:45 -07:00

161 lines
4.7 KiB
Ruby

require 'abstract_unit'
module TestFileUtils
def file_name() File.basename(__FILE__) end
def file_path() File.expand_path(__FILE__) end
def file_data() File.open(file_path, 'rb') { |f| f.read } end
end
class SendFileController < ActionController::Base
include TestFileUtils
layout "layouts/standard" # to make sure layouts don't interfere
attr_writer :options
def options() @options ||= {} end
def file() send_file(file_path, options) end
def data() send_data(file_data, options) end
def rescue_action(e) raise end
end
class SendFileTest < ActionController::TestCase
tests SendFileController
include TestFileUtils
Mime::Type.register "image/png", :png unless defined? Mime::PNG
def setup
@controller = SendFileController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_file_nostream
@controller.options = { :stream => false }
response = nil
assert_nothing_raised { response = process('file') }
assert_not_nil response
assert_kind_of String, response.body
assert_equal file_data, response.body
end
def test_file_stream
response = nil
assert_nothing_raised { response = process('file') }
assert_not_nil response
assert_kind_of Proc, response.body_parts
require 'stringio'
output = StringIO.new
output.binmode
assert_nothing_raised { response.body_parts.call(response, output) }
assert_equal file_data, output.string
end
def test_file_url_based_filename
@controller.options = { :url_based_filename => true }
response = nil
assert_nothing_raised { response = process('file') }
assert_not_nil response
assert_equal "attachment", response.headers["Content-Disposition"]
end
def test_x_sendfile_header
@controller.options = { :x_sendfile => true }
response = nil
assert_nothing_raised { response = process('file') }
assert_not_nil response
assert_equal @controller.file_path, response.headers['X-Sendfile']
assert response.body.blank?
assert !response.etag?
end
def test_data
response = nil
assert_nothing_raised { response = process('data') }
assert_not_nil response
assert_kind_of String, response.body
assert_equal file_data, response.body
end
def test_headers_after_send_shouldnt_include_charset
response = process('data')
assert_equal "application/octet-stream", response.content_type
response = process('file')
assert_equal "application/octet-stream", response.content_type
end
# Test that send_file_headers! is setting the correct HTTP headers.
def test_send_file_headers!
options = {
:length => 1,
:type => Mime::PNG,
:disposition => 'disposition',
:filename => 'filename'
}
# Do it a few times: the resulting headers should be identical
# no matter how many times you send with the same options.
# Test resolving Ticket #458.
@controller.headers = {}
@controller.send(:send_file_headers!, options)
@controller.send(:send_file_headers!, options)
@controller.send(:send_file_headers!, options)
h = @controller.headers
assert_equal 1, h['Content-Length']
assert_equal 'image/png', h['Content-Type']
assert_equal 'disposition; filename="filename"', h['Content-Disposition']
assert_equal 'binary', h['Content-Transfer-Encoding']
# test overriding Cache-Control: no-cache header to fix IE open/save dialog
@controller.headers = { 'Cache-Control' => 'no-cache' }
@controller.send(:send_file_headers!, options)
h = @controller.headers
assert_equal 'private', h['Cache-Control']
end
def test_send_file_headers_with_mime_lookup_with_symbol
options = {
:length => 1,
:type => :png
}
@controller.headers = {}
@controller.send(:send_file_headers!, options)
headers = @controller.headers
assert_equal 'image/png', headers['Content-Type']
end
def test_send_file_headers_with_bad_symbol
options = {
:length => 1,
:type => :this_type_is_not_registered
}
@controller.headers = {}
assert_raise(ArgumentError){ @controller.send(:send_file_headers!, options) }
end
%w(file data).each do |method|
define_method "test_send_#{method}_status" do
@controller.options = { :stream => false, :status => 500 }
assert_nothing_raised { assert_not_nil process(method) }
assert_equal '500 Internal Server Error', @response.status
end
define_method "test_default_send_#{method}_status" do
@controller.options = { :stream => false }
assert_nothing_raised { assert_not_nil process(method) }
assert_equal ActionController::DEFAULT_RENDER_STATUS_CODE, @response.status
end
end
end