2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
module TestFileUtils
|
|
|
|
def file_name() File.basename(__FILE__) end
|
2017-05-15 10:17:28 -04:00
|
|
|
def file_path() __FILE__ end
|
2020-05-12 19:05:32 -04:00
|
|
|
def file_data() @data ||= File.binread(file_path) end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class SendFileController < ActionController::Base
|
|
|
|
include TestFileUtils
|
2014-06-05 15:31:35 -04:00
|
|
|
include ActionController::Testing
|
2005-05-30 03:51:02 -04:00
|
|
|
layout "layouts/standard" # to make sure layouts don't interfere
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2016-05-19 09:20:03 -04:00
|
|
|
before_action :file, only: :file_from_before_action
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
attr_writer :options
|
2009-05-21 18:26:58 -04:00
|
|
|
def options
|
|
|
|
@options ||= {}
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2009-05-21 18:26:58 -04:00
|
|
|
def file
|
|
|
|
send_file(file_path, options)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2016-05-19 09:20:03 -04:00
|
|
|
def file_from_before_action
|
2016-08-06 12:54:50 -04:00
|
|
|
raise "No file sent from before action."
|
2016-05-19 09:20:03 -04:00
|
|
|
end
|
|
|
|
|
2015-09-23 18:44:18 -04:00
|
|
|
def test_send_file_headers_bang
|
|
|
|
options = {
|
2016-08-06 13:35:13 -04:00
|
|
|
type: Mime[:png],
|
|
|
|
disposition: "disposition",
|
|
|
|
filename: "filename"
|
2015-09-23 18:44:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_with_disposition_as_a_symbol
|
|
|
|
options = {
|
2016-08-06 13:35:13 -04:00
|
|
|
type: Mime[:png],
|
|
|
|
disposition: :disposition,
|
|
|
|
filename: "filename"
|
2015-09-23 18:44:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_with_mime_lookup_with_symbol
|
2016-08-06 13:35:13 -04:00
|
|
|
options = { type: :png }
|
2015-09-23 18:44:18 -04:00
|
|
|
|
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_with_bad_symbol
|
2016-08-06 13:35:13 -04:00
|
|
|
options = { type: :this_type_is_not_registered }
|
2015-09-23 18:44:18 -04:00
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_with_nil_content_type
|
2016-08-06 13:35:13 -04:00
|
|
|
options = { type: nil }
|
2015-09-23 18:44:18 -04:00
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_guess_type_from_extension
|
2016-08-06 13:35:13 -04:00
|
|
|
options = { filename: params[:filename] }
|
2015-09-23 18:44:18 -04:00
|
|
|
send_data "foo", options
|
|
|
|
end
|
|
|
|
|
2009-05-21 18:26:58 -04:00
|
|
|
def data
|
|
|
|
send_data(file_data, options)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2013-11-28 20:01:57 -05:00
|
|
|
class SendFileWithActionControllerLive < SendFileController
|
|
|
|
include ActionController::Live
|
|
|
|
end
|
|
|
|
|
2009-01-07 16:23:10 -05:00
|
|
|
class SendFileTest < ActionController::TestCase
|
2004-11-23 20:04:44 -05:00
|
|
|
include TestFileUtils
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@controller = SendFileController.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_nostream
|
2016-08-06 13:35:13 -04:00
|
|
|
@controller.options = { stream: false }
|
2004-11-23 20:04:44 -05:00
|
|
|
response = nil
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_nothing_raised { response = process("file") }
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_not_nil response
|
2010-02-23 18:37:17 -05:00
|
|
|
body = response.body
|
|
|
|
assert_kind_of String, body
|
|
|
|
assert_equal file_data, body
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2009-06-17 19:51:51 -04:00
|
|
|
def test_file_stream
|
2010-02-19 10:31:10 -05:00
|
|
|
response = nil
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_nothing_raised { response = process("file") }
|
2010-02-19 10:31:10 -05:00
|
|
|
assert_not_nil response
|
2012-07-29 20:02:00 -04:00
|
|
|
assert_respond_to response.stream, :each
|
|
|
|
assert_respond_to response.stream, :to_path
|
2009-05-21 15:48:26 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "stringio"
|
2010-02-19 10:31:10 -05:00
|
|
|
output = StringIO.new
|
|
|
|
output.binmode
|
2011-12-25 06:34:58 -05:00
|
|
|
output.string.force_encoding(file_data.encoding)
|
2012-07-29 20:02:00 -04:00
|
|
|
response.body_parts.each { |part| output << part.to_s }
|
2010-02-19 10:31:10 -05:00
|
|
|
assert_equal file_data, output.string
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-11 17:07:04 -05:00
|
|
|
|
2007-02-09 06:25:37 -05:00
|
|
|
def test_file_url_based_filename
|
2016-08-06 13:35:13 -04:00
|
|
|
@controller.options = { url_based_filename: true }
|
2007-02-09 06:25:37 -05:00
|
|
|
response = nil
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_nothing_raised { response = process("file") }
|
2007-02-09 06:25:37 -05:00
|
|
|
assert_not_nil response
|
|
|
|
assert_equal "attachment", response.headers["Content-Disposition"]
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def test_data
|
|
|
|
response = nil
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_nothing_raised { response = process("data") }
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_not_nil response
|
|
|
|
|
|
|
|
assert_kind_of String, response.body
|
|
|
|
assert_equal file_data, response.body
|
|
|
|
end
|
2005-01-13 08:29:49 -05:00
|
|
|
|
2006-11-25 14:29:10 -05:00
|
|
|
def test_headers_after_send_shouldnt_include_charset
|
2016-08-06 12:54:50 -04:00
|
|
|
response = process("data")
|
2009-06-15 14:21:08 -04:00
|
|
|
assert_equal "application/octet-stream", response.headers["Content-Type"]
|
2006-11-25 14:29:10 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
response = process("file")
|
2009-06-15 14:21:08 -04:00
|
|
|
assert_equal "application/octet-stream", response.headers["Content-Type"]
|
2006-11-25 14:29:10 -05:00
|
|
|
end
|
|
|
|
|
2005-01-13 08:29:49 -05:00
|
|
|
# Test that send_file_headers! is setting the correct HTTP headers.
|
2010-02-19 22:19:20 -05:00
|
|
|
def test_send_file_headers_bang
|
2005-01-13 08:29:49 -05:00
|
|
|
# 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.
|
2015-09-23 18:44:18 -04:00
|
|
|
5.times do
|
|
|
|
get :test_send_file_headers_bang
|
2006-06-01 20:51:56 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "image/png", response.content_type
|
2018-09-09 03:35:48 -04:00
|
|
|
assert_equal %(disposition; filename="filename"; filename*=UTF-8''filename), response.get_header("Content-Disposition")
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "binary", response.get_header("Content-Transfer-Encoding")
|
2015-09-23 18:44:18 -04:00
|
|
|
end
|
2005-01-13 08:29:49 -05:00
|
|
|
end
|
2006-06-01 20:51:56 -04:00
|
|
|
|
2012-11-26 17:49:14 -05:00
|
|
|
def test_send_file_headers_with_disposition_as_a_symbol
|
2015-09-23 18:44:18 -04:00
|
|
|
get :test_send_file_headers_with_disposition_as_a_symbol
|
2012-11-26 17:49:14 -05:00
|
|
|
|
2018-09-09 03:35:48 -04:00
|
|
|
assert_equal %(disposition; filename="filename"; filename*=UTF-8''filename), response.get_header("Content-Disposition")
|
2012-11-26 17:49:14 -05:00
|
|
|
end
|
|
|
|
|
2008-12-21 13:58:55 -05:00
|
|
|
def test_send_file_headers_with_mime_lookup_with_symbol
|
2015-09-23 18:44:18 -04:00
|
|
|
get __method__
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "image/png", response.content_type
|
2008-12-21 13:58:55 -05:00
|
|
|
end
|
2010-04-25 13:48:40 -04:00
|
|
|
|
2008-12-21 13:58:55 -05:00
|
|
|
def test_send_file_headers_with_bad_symbol
|
2015-09-23 18:44:18 -04:00
|
|
|
error = assert_raise(ArgumentError) { get __method__ }
|
|
|
|
assert_equal "Unknown MIME type this_type_is_not_registered", error.message
|
2015-09-19 09:05:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_headers_with_nil_content_type
|
2015-09-23 18:44:18 -04:00
|
|
|
error = assert_raise(ArgumentError) { get __method__ }
|
2015-09-19 09:05:41 -04:00
|
|
|
assert_equal ":type option required", error.message
|
2008-12-21 13:58:55 -05:00
|
|
|
end
|
2012-10-27 10:03:18 -04:00
|
|
|
|
2011-06-28 00:57:41 -04:00
|
|
|
def test_send_file_headers_guess_type_from_extension
|
|
|
|
{
|
2016-08-06 12:54:50 -04:00
|
|
|
"image.png" => "image/png",
|
|
|
|
"image.jpeg" => "image/jpeg",
|
|
|
|
"image.jpg" => "image/jpeg",
|
|
|
|
"image.tif" => "image/tiff",
|
|
|
|
"image.gif" => "image/gif",
|
2017-11-28 21:15:01 -05:00
|
|
|
"movie.mp4" => "video/mp4",
|
2016-08-06 12:54:50 -04:00
|
|
|
"file.zip" => "application/zip",
|
|
|
|
"file.unk" => "application/octet-stream",
|
|
|
|
"zip" => "application/octet-stream"
|
2016-10-28 23:05:58 -04:00
|
|
|
}.each do |filename, expected_type|
|
2015-09-23 18:44:18 -04:00
|
|
|
get __method__, params: { filename: filename }
|
|
|
|
assert_equal expected_type, response.content_type
|
2011-06-28 00:57:41 -04:00
|
|
|
end
|
|
|
|
end
|
2008-12-21 13:58:55 -05:00
|
|
|
|
2012-04-30 03:12:55 -04:00
|
|
|
def test_send_file_with_default_content_disposition_header
|
2016-08-06 12:54:50 -04:00
|
|
|
process("data")
|
|
|
|
assert_equal "attachment", @controller.headers["Content-Disposition"]
|
2012-04-30 03:12:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file_without_content_disposition_header
|
2016-08-16 03:30:11 -04:00
|
|
|
@controller.options = { disposition: nil }
|
2016-08-06 12:54:50 -04:00
|
|
|
process("data")
|
|
|
|
assert_nil @controller.headers["Content-Disposition"]
|
2012-04-30 03:12:55 -04:00
|
|
|
end
|
|
|
|
|
2016-05-19 09:20:03 -04:00
|
|
|
def test_send_file_from_before_action
|
|
|
|
response = nil
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_nothing_raised { response = process("file_from_before_action") }
|
2016-05-19 09:20:03 -04:00
|
|
|
assert_not_nil response
|
|
|
|
|
|
|
|
assert_kind_of String, response.body
|
|
|
|
assert_equal file_data, response.body
|
|
|
|
end
|
|
|
|
|
2006-06-01 20:51:56 -04:00
|
|
|
%w(file data).each do |method|
|
|
|
|
define_method "test_send_#{method}_status" do
|
2016-08-06 13:35:13 -04:00
|
|
|
@controller.options = { stream: false, status: 500 }
|
2015-10-05 19:50:37 -04:00
|
|
|
assert_not_nil process(method)
|
2009-05-02 15:57:40 -04:00
|
|
|
assert_equal 500, @response.status
|
2006-06-01 20:51:56 -04:00
|
|
|
end
|
|
|
|
|
2010-02-23 18:37:17 -05:00
|
|
|
define_method "test_send_#{method}_content_type" do
|
2016-08-06 13:35:13 -04:00
|
|
|
@controller.options = { stream: false, content_type: "application/x-ruby" }
|
2010-02-23 18:37:17 -05:00
|
|
|
assert_nothing_raised { assert_not_nil process(method) }
|
|
|
|
assert_equal "application/x-ruby", @response.content_type
|
|
|
|
end
|
|
|
|
|
2006-06-01 20:51:56 -04:00
|
|
|
define_method "test_default_send_#{method}_status" do
|
2016-08-06 13:35:13 -04:00
|
|
|
@controller.options = { stream: false }
|
2006-06-01 20:51:56 -04:00
|
|
|
assert_nothing_raised { assert_not_nil process(method) }
|
2009-05-02 15:57:40 -04:00
|
|
|
assert_equal 200, @response.status
|
2006-06-01 20:51:56 -04:00
|
|
|
end
|
|
|
|
end
|
2013-11-28 20:01:57 -05:00
|
|
|
|
|
|
|
def test_send_file_with_action_controller_live
|
|
|
|
@controller = SendFileWithActionControllerLive.new
|
2016-08-06 13:35:13 -04:00
|
|
|
@controller.options = { content_type: "application/x-ruby" }
|
2013-11-28 20:01:57 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
response = process("file")
|
2013-11-28 20:01:57 -05:00
|
|
|
assert_equal 200, response.status
|
|
|
|
end
|
2016-08-29 17:35:15 -04:00
|
|
|
|
|
|
|
def test_send_file_charset_with_type_options_key
|
|
|
|
@controller = SendFileWithActionControllerLive.new
|
|
|
|
@controller.options = { type: "text/calendar; charset=utf-8" }
|
|
|
|
response = process("file")
|
|
|
|
assert_equal "text/calendar; charset=utf-8", response.headers["Content-Type"]
|
|
|
|
end
|
|
|
|
|
2016-12-19 13:31:42 -05:00
|
|
|
def test_send_file_charset_with_type_options_key_without_charset
|
|
|
|
@controller = SendFileWithActionControllerLive.new
|
|
|
|
@controller.options = { type: "image/png" }
|
|
|
|
response = process("file")
|
|
|
|
assert_equal "image/png", response.headers["Content-Type"]
|
|
|
|
end
|
|
|
|
|
2016-08-29 17:35:15 -04:00
|
|
|
def test_send_file_charset_with_content_type_options_key
|
|
|
|
@controller = SendFileWithActionControllerLive.new
|
|
|
|
@controller.options = { content_type: "text/calendar" }
|
|
|
|
response = process("file")
|
2016-12-19 13:31:42 -05:00
|
|
|
assert_equal "text/calendar", response.headers["Content-Type"]
|
2016-08-29 17:35:15 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|