File: test_mime_types_cache.rb

package info (click to toggle)
ruby-mime-types 1.25-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 392 kB
  • ctags: 152
  • sloc: ruby: 957; makefile: 4
file content (77 lines) | stat: -rw-r--r-- 2,076 bytes parent folder | download | duplicates (2)
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
# -*- ruby encoding: utf-8 -*-

require 'mime/types'

class TestMIMETypesCache < Minitest::Test
  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_mime_types)
  end

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

  def test_uses_correct_cache_file
    assert_equal(@cache_file, MIME::Types.cache_file)
  end

  def test_does_not_use_cache_when_unset
    ENV.delete('RUBY_MIME_TYPES_CACHE')
    assert_equal(nil, MIME::Types.send(:load_mime_types_from_cache))
  end

  def test_raises_exception_when_load_forced_without_cache_file
    assert_raises(ArgumentError) {
      ENV.delete('RUBY_MIME_TYPES_CACHE')
      MIME::Types.send(:load_mime_types_from_cache!)
    }
  end

  def test_does_not_use_cache_when_missing
    assert_equal(false, MIME::Types.send(:load_mime_types_from_cache))
  end

  def test_does_not_create_cache_when_unset
    ENV.delete('RUBY_MIME_TYPES_CACHE')
    assert_equal(nil, MIME::Types.send(:write_mime_types_to_cache))
  end

  def test_raises_exception_when_write_forced_without_cache_file
    assert_raises(ArgumentError) {
      ENV.delete('RUBY_MIME_TYPES_CACHE')
      MIME::Types.send(:write_mime_types_to_cache!)
    }
  end

  def test_creates_cache
    assert_equal(false, File.exist?(@cache_file))
    MIME::Types.send(:write_mime_types_to_cache)
    assert_equal(true, File.exist?(@cache_file))
  end

  def test_uses_cache
    html = MIME::Types['text/html'].first
    html.extensions << 'hex'
    MIME::Types.send(:write_mime_types_to_cache)
    MIME::Types.instance_variable_set(:@__types__, nil)

    assert_equal(true, MIME::Types.send(:load_mime_types_from_cache))
    html = MIME::Types['text/html'].first
    assert_includes(html.extensions, 'hex')

    reset_mime_types
  end
end