summaryrefslogtreecommitdiff
path: root/test/test_mime_types_cache.rb
blob: bcfc4c6aa0fe31dae995be6ede1a2965a84114cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- ruby encoding: utf-8 -*-

require 'mime/types'
require 'minitest_helper'

describe MIME::Types::Cache do
  def setup
    require 'fileutils'
    @cache_file = File.expand_path('../cache.tst', __FILE__)
    ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
    clear_cache_file
  end

  def teardown
    clear_cache_file
    ENV.delete('RUBY_MIME_TYPES_CACHE')
  end

  def reset_mime_types
    MIME::Types.instance_variable_set(:@__types__, nil)
    MIME::Types.send(:load_default_mime_types)
  end

  def clear_cache_file
    FileUtils.rm @cache_file if File.exist? @cache_file
  end

  describe '.load' do
    it 'does not use cache when RUBY_MIME_TYPES_CACHE is unset' do
      ENV.delete('RUBY_MIME_TYPES_CACHE')
      assert_equal(nil, MIME::Types::Cache.load)
    end

    it 'does not use cache when missing' do
      assert_equal(nil, MIME::Types::Cache.load)
    end

    it 'outputs an error when there is an invalid version' do
      v = MIME::Types::Data::VERSION.dup
      MIME::Types::Data::VERSION.gsub!(/.*/, '0.0')
      MIME::Types::Cache.save
      MIME::Types::Data::VERSION.gsub!(/.*/, v)
      MIME::Types.instance_variable_set(:@__types__, nil)
      assert_output '', /MIME::Types cache: invalid version/ do
        MIME::Types['text/html']
      end
    end

    it 'outputs an error when there is a marshal file incompatibility' do
      MIME::Types::Cache.save
      data = File.binread(@cache_file).reverse
      File.open(@cache_file, 'wb') { |f| f.write(data) }
      MIME::Types.instance_variable_set(:@__types__, nil)
      assert_output '', /incompatible marshal file format/ do
        MIME::Types['text/html']
      end
    end
  end

  describe '.save' do
    it 'does not create cache when RUBY_MIME_TYPES_CACHE is unset' do
      ENV.delete('RUBY_MIME_TYPES_CACHE')
      assert_equal(nil, MIME::Types::Cache.save)
    end

    it 'creates the cache ' do
      assert_equal(false, File.exist?(@cache_file))
      MIME::Types::Cache.save
      assert_equal(true, File.exist?(@cache_file))
    end

    it 'uses the cache' do
      MIME::Types['text/html'].first.add_extensions('hex')
      MIME::Types::Cache.save
      MIME::Types.instance_variable_set(:@__types__, nil)

      assert_includes MIME::Types['text/html'].first.extensions, 'hex'

      reset_mime_types
    end
  end
end

describe MIME::Types::Container do
  it 'marshals and unmarshals correctly' do
    container = MIME::Types::Container.new
    container['xyz'] << 'abc'

    # default proc should return Set[]
    assert_equal(Set[], container['abc'])
    assert_equal(Set['abc'], container['xyz'])

    marshalled = Marshal.dump(container)
    loaded_container = Marshal.load(marshalled)

    # default proc should still return Set[]
    assert_equal(Set[], loaded_container['abc'])
    assert_equal(Set['abc'], container['xyz'])
  end
end