Sha256: 57e925212e4b767513200700c6049f6127adaef8900018e9f080800fa2be52d0

Contents?: true

Size: 1.95 KB

Versions: 18

Compression:

Stored size: 1.95 KB

Contents

require 'spec_helper'

describe Paperclip::ContentTypeDetector do
  it 'returns a meaningful content type for open xml spreadsheets' do
    file = File.new(fixture_file("empty.xlsx"))
    assert_equal "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                 Paperclip::ContentTypeDetector.new(file.path).detect
  end

  it 'gives a sensible default when the name is empty' do
    assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new("").detect
  end

  it 'returns the empty content type when the file is empty' do
    tempfile = Tempfile.new("empty")
    assert_equal "inode/x-empty", Paperclip::ContentTypeDetector.new(tempfile.path).detect
    tempfile.close
  end

  it 'returns content type of file if it is an acceptable type' do
    MIME::Types.stubs(:type_for).returns([MIME::Type.new('application/mp4'), MIME::Type.new('video/mp4'), MIME::Type.new('audio/mp4')])
    Paperclip::ContentTypeDetector.any_instance
      .stubs(:type_from_file_contents).returns("video/mp4")
    @filename = "my_file.mp4"
    assert_equal "video/mp4", Paperclip::ContentTypeDetector.new(@filename).detect
  end

  it 'finds the right type in the list via the file command' do
    @filename = "#{Dir.tmpdir}/something.hahalolnotreal"
    File.open(@filename, "w+") do |file|
      file.puts "This is a text file."
      file.rewind
      assert_equal "text/plain", Paperclip::ContentTypeDetector.new(file.path).detect
    end
    FileUtils.rm @filename
  end

  it 'returns a sensible default if something is wrong, like the file is gone' do
    @filename = "/path/to/nothing"
    assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new(@filename).detect
  end

  it 'returns a sensible default when the file command is missing' do
    Paperclip.stubs(:run).raises(Cocaine::CommandLineError.new)
    @filename = "/path/to/something"
    assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new(@filename).detect
  end
end

Version data entries

18 entries across 18 versions & 5 rubygems

Version Path
kt-paperclip-4.4.0 spec/paperclip/content_type_detector_spec.rb
paperclip-5.2.1 spec/paperclip/content_type_detector_spec.rb
paperclip-5.2.0 spec/paperclip/content_type_detector_spec.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/paperclip-5.1.0/spec/paperclip/content_type_detector_spec.rb
paperclip-5.1.0 spec/paperclip/content_type_detector_spec.rb
paperclip-5.0.0 spec/paperclip/content_type_detector_spec.rb
paperclip-4.3.7 spec/paperclip/content_type_detector_spec.rb
paperclip_jk-5.0.0.beta2 spec/paperclip/content_type_detector_spec.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/paperclip-4.3.6/spec/paperclip/content_type_detector_spec.rb
paperclip-5.0.0.beta2 spec/paperclip/content_type_detector_spec.rb
paperclip-5.0.0.beta1 spec/paperclip/content_type_detector_spec.rb
paperclip-4.3.6 spec/paperclip/content_type_detector_spec.rb
paperclip-4.3.5 spec/paperclip/content_type_detector_spec.rb
paperclip-4.3.4 spec/paperclip/content_type_detector_spec.rb
paperclip-4.3.3 spec/paperclip/content_type_detector_spec.rb
paperclip-4.3.2 spec/paperclip/content_type_detector_spec.rb
paperclip-4.3.1 spec/paperclip/content_type_detector_spec.rb
paperclip-4.3.0 spec/paperclip/content_type_detector_spec.rb