Sha256: 95f1c5d5b634fa1542b117e76a18daea5a1f7c0711b599f7fd0e0adfa1fc0e0d

Contents?: true

Size: 1.99 KB

Versions: 9

Compression:

Stored size: 1.99 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
    allow(MIME::Types).to receive(:type_for).and_return([MIME::Type.new("application/mp4"), MIME::Type.new("video/mp4"), MIME::Type.new("audio/mp4")])
    allow_any_instance_of(Paperclip::ContentTypeDetector).to receive(:type_from_file_contents).and_return("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
    allow(Paperclip).to receive(:run).and_raise(Terrapin::CommandLineError.new)
    @filename = "/path/to/something"
    assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new(@filename).detect
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
kt-paperclip-6.4.2 spec/paperclip/content_type_detector_spec.rb
kt-paperclip-7.0.1 spec/paperclip/content_type_detector_spec.rb
kt-paperclip-7.0.0 spec/paperclip/content_type_detector_spec.rb
kt-paperclip-6.4.1 spec/paperclip/content_type_detector_spec.rb
kt-paperclip-6.4.0 spec/paperclip/content_type_detector_spec.rb
kt-paperclip-6.3.0 spec/paperclip/content_type_detector_spec.rb
kt-paperclip-6.2.2 spec/paperclip/content_type_detector_spec.rb
kt-paperclip-6.2.1 spec/paperclip/content_type_detector_spec.rb
kt-paperclip-6.2.0 spec/paperclip/content_type_detector_spec.rb