class SendFileTest

Public Instance Methods

setup() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 84
def setup
  @controller = SendFileController.new
end
test_data() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 121
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
test_file_nostream() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 88
def test_file_nostream
  @controller.options = { stream: false }
  response = nil
  assert_nothing_raised { response = process("file") }
  assert_not_nil response
  body = response.body
  assert_kind_of String, body
  assert_equal file_data, body
end
test_file_stream() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 98
def test_file_stream
  response = nil
  assert_nothing_raised { response = process("file") }
  assert_not_nil response
  assert_respond_to response.stream, :each
  assert_respond_to response.stream, :to_path

  require "stringio"
  output = StringIO.new
  output.binmode
  output.string.force_encoding(file_data.encoding)
  response.body_parts.each { |part| output << part.to_s }
  assert_equal file_data, output.string
end
test_file_url_based_filename() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 113
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
test_headers_after_send_shouldnt_include_charset() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 130
def test_headers_after_send_shouldnt_include_charset
  response = process("data")
  assert_equal "application/octet-stream", response.headers["Content-Type"]

  response = process("file")
  assert_equal "application/octet-stream", response.headers["Content-Type"]
end
test_send_file_charset_with_content_type_options_key() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 253
def test_send_file_charset_with_content_type_options_key
  @controller = SendFileWithActionControllerLive.new
  @controller.options = { content_type: "text/calendar" }
  response = process("file")
  assert_equal "text/calendar", response.headers["Content-Type"]
end
test_send_file_charset_with_type_options_key() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 239
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
test_send_file_charset_with_type_options_key_without_charset() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 246
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
test_send_file_from_before_action() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 202
def test_send_file_from_before_action
  response = nil
  assert_nothing_raised { response = process("file_from_before_action") }
  assert_not_nil response

  assert_kind_of String, response.body
  assert_equal file_data, response.body
end
test_send_file_headers_bang() click to toggle source

Test that send_file_headers! is setting the correct HTTP headers.

# File actionpack/test/controller/send_file_test.rb, line 139
def test_send_file_headers_bang
  # 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.
  5.times do
    get :test_send_file_headers_bang

    assert_equal "image/png", response.content_type
    assert_equal 'disposition; filename="filename"', response.get_header("Content-Disposition")
    assert_equal "binary", response.get_header("Content-Transfer-Encoding")
    assert_equal "private", response.get_header("Cache-Control")
  end
end
test_send_file_headers_guess_type_from_extension() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 174
def test_send_file_headers_guess_type_from_extension
  {
    "image.png" => "image/png",
    "image.jpeg" => "image/jpeg",
    "image.jpg" => "image/jpeg",
    "image.tif" => "image/tiff",
    "image.gif" => "image/gif",
    "movie.mpg" => "video/mpeg",
    "file.zip" => "application/zip",
    "file.unk" => "application/octet-stream",
    "zip" => "application/octet-stream"
  }.each do |filename, expected_type|
    get __method__, params: { filename: filename }
    assert_equal expected_type, response.content_type
  end
end
test_send_file_headers_with_bad_symbol() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 164
def test_send_file_headers_with_bad_symbol
  error = assert_raise(ArgumentError) { get __method__ }
  assert_equal "Unknown MIME type this_type_is_not_registered", error.message
end
test_send_file_headers_with_disposition_as_a_symbol() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 153
def test_send_file_headers_with_disposition_as_a_symbol
  get :test_send_file_headers_with_disposition_as_a_symbol

  assert_equal 'disposition; filename="filename"', response.get_header("Content-Disposition")
end
test_send_file_headers_with_mime_lookup_with_symbol() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 159
def test_send_file_headers_with_mime_lookup_with_symbol
  get __method__
  assert_equal "image/png", response.content_type
end
test_send_file_headers_with_nil_content_type() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 169
def test_send_file_headers_with_nil_content_type
  error = assert_raise(ArgumentError) { get __method__ }
  assert_equal ":type option required", error.message
end
test_send_file_with_action_controller_live() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 231
def test_send_file_with_action_controller_live
  @controller = SendFileWithActionControllerLive.new
  @controller.options = { content_type: "application/x-ruby" }

  response = process("file")
  assert_equal 200, response.status
end
test_send_file_with_default_content_disposition_header() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 191
def test_send_file_with_default_content_disposition_header
  process("data")
  assert_equal "attachment", @controller.headers["Content-Disposition"]
end
test_send_file_without_content_disposition_header() click to toggle source
# File actionpack/test/controller/send_file_test.rb, line 196
def test_send_file_without_content_disposition_header
  @controller.options = { disposition: nil }
  process("data")
  assert_nil @controller.headers["Content-Disposition"]
end