mime-types 2.99.3 → 3.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +5 -5
  2. data/{Code-of-Conduct.rdoc → Code-of-Conduct.md} +19 -20
  3. data/Contributing.md +143 -0
  4. data/History.md +240 -0
  5. data/{Licence.rdoc → Licence.md} +4 -18
  6. data/Manifest.txt +8 -25
  7. data/README.rdoc +62 -73
  8. data/Rakefile +175 -58
  9. data/lib/mime-types.rb +1 -1
  10. data/lib/mime/type.rb +213 -424
  11. data/lib/mime/type/columnar.rb +29 -62
  12. data/lib/mime/types.rb +46 -141
  13. data/lib/mime/types/_columnar.rb +136 -0
  14. data/lib/mime/types/cache.rb +51 -73
  15. data/lib/mime/types/columnar.rb +2 -147
  16. data/lib/mime/types/container.rb +96 -0
  17. data/lib/mime/types/deprecations.rb +4 -25
  18. data/lib/mime/types/full.rb +19 -0
  19. data/lib/mime/types/loader.rb +12 -141
  20. data/lib/mime/types/logger.rb +5 -1
  21. data/lib/mime/types/registry.rb +90 -0
  22. data/test/minitest_helper.rb +5 -13
  23. data/test/test_mime_type.rb +470 -456
  24. data/test/test_mime_types.rb +135 -87
  25. data/test/test_mime_types_cache.rb +82 -54
  26. data/test/test_mime_types_class.rb +118 -98
  27. data/test/test_mime_types_lazy.rb +26 -24
  28. data/test/test_mime_types_loader.rb +6 -33
  29. metadata +107 -64
  30. data/Contributing.rdoc +0 -170
  31. data/History-Types.rdoc +0 -454
  32. data/History.rdoc +0 -590
  33. data/data/mime-types.json +0 -1
  34. data/data/mime.content_type.column +0 -1980
  35. data/data/mime.docs.column +0 -1980
  36. data/data/mime.encoding.column +0 -1980
  37. data/data/mime.friendly.column +0 -1980
  38. data/data/mime.obsolete.column +0 -1980
  39. data/data/mime.registered.column +0 -1980
  40. data/data/mime.signature.column +0 -1980
  41. data/data/mime.use_instead.column +0 -1980
  42. data/data/mime.xrefs.column +0 -1980
  43. data/docs/COPYING.txt +0 -339
  44. data/docs/artistic.txt +0 -127
  45. data/lib/mime/types/loader_path.rb +0 -15
  46. data/support/apache_mime_types.rb +0 -108
  47. data/support/benchmarks/load.rb +0 -64
  48. data/support/benchmarks/load_allocations.rb +0 -83
  49. data/support/benchmarks/object_counts.rb +0 -41
  50. data/support/convert.rb +0 -158
  51. data/support/convert/columnar.rb +0 -88
  52. data/support/iana_registry.rb +0 -172
@@ -1,121 +1,169 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'mime/types'
4
4
  require 'minitest_helper'
5
5
 
6
- class TestMIMETypes < Minitest::Test
7
- def setup
8
- @mime_types = MIME::Types.new
9
- @mime_types.add(MIME::Type.new(['text/plain', %w(txt)]),
10
- MIME::Type.new(['image/jpeg', %w(jpg jpeg)]),
11
- MIME::Type.new('application/x-wordperfect6.1'),
12
- MIME::Type.new('content-type' => 'application/x-www-form-urlencoded', 'registered' => true),
13
- MIME::Type.new(['application/x-gzip', %w(gz)]),
14
- MIME::Type.new(['application/gzip', %w(gz)]))
6
+ describe MIME::Types do
7
+ def mime_types
8
+ @mime_types ||= MIME::Types.new.tap { |mt|
9
+ mt.add MIME::Type.new(['text/plain', %w(txt)]),
10
+ MIME::Type.new(['image/jpeg', %w(jpg jpeg)]),
11
+ MIME::Type.new('application/x-wordperfect6.1'),
12
+ MIME::Type.new(
13
+ 'content-type' => 'application/x-www-form-urlencoded',
14
+ 'registered' => true
15
+ ),
16
+ MIME::Type.new(['application/x-gzip', %w(gz)]),
17
+ MIME::Type.new(
18
+ 'content-type' => 'application/gzip',
19
+ 'extensions' => 'gz',
20
+ 'registered' => true
21
+ )
22
+ }
15
23
  end
16
24
 
17
- def test_enumerable
18
- assert(@mime_types.any? { |type| type.content_type == 'text/plain' })
19
- assert_kind_of(Enumerator, @mime_types.each)
20
- assert_equal(6, @mime_types.each.count)
21
- end
25
+ describe 'is enumerable' do
26
+ it 'correctly uses an Enumerable method like #any?' do
27
+ assert(mime_types.any? { |type| type.content_type == 'text/plain' })
28
+ end
22
29
 
23
- def test_index_with_mime_type
24
- xtxp = MIME::Type.new('x-text/x-plain')
25
- assert_includes(@mime_types[xtxp], 'text/plain')
26
- assert_equal(1, @mime_types[xtxp].size)
27
- end
30
+ it 'implements each with no parameters to return an Enumerator' do
31
+ assert_kind_of Enumerator, mime_types.each
32
+ assert_kind_of Enumerator, mime_types.map
33
+ end
28
34
 
29
- def test_index_with_regex
30
- assert_includes(@mime_types[/plain/], 'text/plain')
31
- assert_equal(1, @mime_types[/plain/].size)
32
- end
35
+ it 'will create a lazy enumerator' do
36
+ assert_kind_of Enumerator::Lazy, mime_types.lazy
37
+ assert_kind_of Enumerator::Lazy, mime_types.map.lazy
38
+ end
33
39
 
34
- def test_index_with_string
35
- assert_includes(@mime_types['text/plain'], 'text/plain')
36
- assert_equal(1, @mime_types['text/plain'].size)
40
+ it 'is countable with an enumerator' do
41
+ assert_equal 6, mime_types.each.count
42
+ assert_equal 6, mime_types.lazy.count
43
+ end
37
44
  end
38
45
 
39
- def test_index_with_complete_flag
40
- assert_empty(@mime_types['text/vnd.fly', complete: true])
41
- refute_empty(@mime_types['text/plain', complete: true])
42
- end
46
+ describe '#[]' do
47
+ it 'can be searched with a MIME::Type' do
48
+ text_plain = MIME::Type.new('text/plain')
49
+ assert_includes mime_types[text_plain], 'text/plain'
50
+ assert_equal 1, mime_types[text_plain].size
51
+ end
43
52
 
44
- def test_index_with_registered_flag
45
- assert_empty(@mime_types['application/x-wordperfect6.1',
46
- registered: true])
47
- refute_empty(@mime_types['application/x-www-form-urlencoded',
48
- registered: true])
49
- refute_empty(@mime_types['application/gzip', registered: true])
50
- refute_equal(@mime_types['application/gzip'].size,
51
- @mime_types['application/gzip', registered: true])
52
- end
53
+ it 'can be searched with a regular expression' do
54
+ assert_includes mime_types[/plain$/], 'text/plain'
55
+ assert_equal 1, mime_types[/plain$/].size
56
+ end
53
57
 
54
- def test_index_with_platform_flag
55
- assert_deprecated('MIME::Types#[]', 'using the :platform flag') do
56
- refute_empty MIME::Types['text/plain', platform: true]
58
+ it 'sorts by priority with multiple matches' do
59
+ assert_equal %w(application/gzip application/x-gzip), mime_types[/gzip$/]
60
+ assert_equal 2, mime_types[/gzip$/].size
61
+ end
62
+
63
+ it 'can be searched with a string' do
64
+ assert_includes mime_types['text/plain'], 'text/plain'
65
+ assert_equal 1, mime_types['text/plain'].size
57
66
  end
58
- end
59
67
 
60
- def test_add
61
- eruby = MIME::Type.new('application/x-eruby') do |t|
62
- t.extensions = 'rhtml'
63
- t.encoding = '8bit'
68
+ it 'can be searched with the complete flag' do
69
+ assert_empty mime_types[
70
+ 'application/x-www-form-urlencoded',
71
+ complete: true
72
+ ]
73
+ assert_includes mime_types['text/plain', complete: true], 'text/plain'
74
+ assert_equal 1, mime_types['text/plain', complete: true].size
64
75
  end
65
76
 
66
- @mime_types.add(eruby)
77
+ it 'can be searched with the registered flag' do
78
+ assert_empty mime_types['application/x-wordperfect6.1', registered: true]
79
+ refute_empty mime_types[
80
+ 'application/x-www-form-urlencoded',
81
+ registered: true
82
+ ]
83
+ refute_empty mime_types[/gzip/, registered: true]
84
+ refute_equal mime_types[/gzip/], mime_types[/gzip/, registered: true]
85
+ end
67
86
 
68
- assert_equal(@mime_types['application/x-eruby'], [eruby])
87
+ it 'properly returns an empty result on a regular expression miss' do
88
+ assert_empty mime_types[/^foo/]
89
+ assert_empty mime_types[/^foo/, registered: true]
90
+ assert_empty mime_types[/^foo/, complete: true]
91
+ end
69
92
  end
70
93
 
71
- def test_type_for
72
- assert_equal(%w(application/gzip application/x-gzip),
73
- @mime_types.type_for('gz'))
74
- assert_equal(%w(image/jpeg), MIME::Types.of('foo.jpeg'))
75
- assert_equal(%w(image/jpeg text/plain),
76
- MIME::Types.type_for(%w(foo.txt foo.jpeg)))
77
- assert_deprecated('MIME::Types#type_for', 'using the platform parameter') do
78
- assert_equal(@mime_types.of('gif', true), @mime_types['image/gif'])
94
+ describe '#add' do
95
+ let(:eruby) { MIME::Type.new('application/x-eruby') }
96
+ let(:jinja) { MIME::Type.new('application/jinja2') }
97
+
98
+ it 'successfully adds a new type' do
99
+ mime_types.add(eruby)
100
+ assert_equal mime_types['application/x-eruby'], [eruby]
79
101
  end
80
- assert_empty(MIME::Types.type_for('coverallsjson'))
81
- assert_deprecated('MIME::Types#type_for', 'using the platform parameter') do
82
- assert_equal @mime_types.type_for('jpeg', true), @mime_types['image/jpeg']
102
+
103
+ it 'complains about adding a duplicate type' do
104
+ mime_types.add(eruby)
105
+ assert_output '', /is already registered as a variant/ do
106
+ mime_types.add(eruby)
107
+ end
108
+ assert_equal mime_types['application/x-eruby'], [eruby]
83
109
  end
84
- assert_empty(@mime_types.type_for('zzz'))
85
- end
86
110
 
87
- def test_count
88
- assert_equal(6, @mime_types.count)
89
- end
111
+ it 'does not complain about adding a duplicate type when quiet' do
112
+ mime_types.add(eruby)
113
+ assert_output '', '' do
114
+ mime_types.add(eruby, :silent)
115
+ end
116
+ assert_equal mime_types['application/x-eruby'], [eruby]
117
+ end
90
118
 
91
- # This tests the instance implementation through the class implementation.
92
- def test_add_type_variant
93
- xtxp = MIME::Type.new('x-text/x-plain')
94
- assert_deprecated('MIME::Types#add_type_variant', 'and will be private') do
95
- @mime_types.add_type_variant(xtxp)
119
+ it 'successfully adds from an array' do
120
+ mime_types.add([eruby, jinja])
121
+ assert_equal mime_types['application/x-eruby'], [eruby]
122
+ assert_equal mime_types['application/jinja2'], [jinja]
96
123
  end
97
- assert_includes(@mime_types['text/plain'], xtxp)
98
- end
99
124
 
100
- def test_data_version
101
- assert_equal(MIME::Type::VERSION, @mime_types.data_version)
102
- end
125
+ it 'successfully adds from another MIME::Types' do
126
+ mt = MIME::Types.new
127
+ mt.add(mime_types)
128
+ assert_equal mime_types.count, mt.count
103
129
 
104
- # This tests the instance implementation through the class implementation.
105
- def test_index_extensions
106
- xtxp = MIME::Type.new(['x-text/x-plain', %w(tzt)])
107
- assert_deprecated('MIME::Types#index_extensions', 'and will be private') do
108
- @mime_types.index_extensions(xtxp)
130
+ mime_types.each do |type|
131
+ assert_equal mt[type.content_type], [type]
132
+ end
109
133
  end
110
- assert_includes(@mime_types.of('tzt'), xtxp)
111
134
  end
112
135
 
113
- def test_defined_types
114
- assert_deprecated('MIME::Types#defined_types') do
115
- assert_empty(MIME::Types.new.defined_types)
136
+ describe '#type_for' do
137
+ it 'finds all types for a given extension' do
138
+ assert_equal %w(application/gzip application/x-gzip),
139
+ mime_types.type_for('gz')
140
+ end
141
+
142
+ it 'separates the extension from filenames' do
143
+ assert_equal %w(image/jpeg), mime_types.of(['foo.jpeg', 'bar.jpeg'])
116
144
  end
117
- assert_deprecated('MIME::Types#defined_types') do
118
- refute_empty(@mime_types.defined_types)
145
+
146
+ it 'finds multiple extensions' do
147
+ assert_equal %w(image/jpeg text/plain),
148
+ mime_types.type_for(%w(foo.txt foo.jpeg))
149
+ end
150
+
151
+ it 'does not find unknown extensions' do
152
+ keys = mime_types.instance_variable_get(:@extension_index).keys
153
+ assert_empty mime_types.type_for('zzz')
154
+ assert_equal keys, mime_types.instance_variable_get(:@extension_index).keys
155
+ end
156
+
157
+ it 'modifying type extensions causes reindexing' do
158
+ plain_text = mime_types['text/plain'].first
159
+ plain_text.add_extensions('xtxt')
160
+ assert_includes mime_types.type_for('xtxt'), 'text/plain'
161
+ end
162
+ end
163
+
164
+ describe '#count' do
165
+ it 'can count the number of types inside' do
166
+ assert_equal 6, mime_types.count
119
167
  end
120
168
  end
121
169
  end
@@ -1,19 +1,26 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'mime/types'
4
4
  require 'minitest_helper'
5
5
 
6
- class TestMIMETypesCache < Minitest::Test
7
- def setup
6
+ describe MIME::Types::Cache do
7
+ include Minitest::Hooks
8
+
9
+ MUTEX = Mutex.new
10
+
11
+ def around
8
12
  require 'fileutils'
9
- @cache_file = File.expand_path('../cache.tst', __FILE__)
10
- ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
11
- clear_cache_file
12
- end
13
13
 
14
- def teardown
15
- clear_cache_file
16
- ENV.delete('RUBY_MIME_TYPES_CACHE')
14
+ MUTEX.synchronize do
15
+ @cache_file = File.expand_path('../cache.tst', __FILE__)
16
+ ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
17
+ clear_cache_file
18
+
19
+ super
20
+
21
+ clear_cache_file
22
+ ENV.delete('RUBY_MIME_TYPES_CACHE')
23
+ end
17
24
  end
18
25
 
19
26
  def reset_mime_types
@@ -25,66 +32,87 @@ class TestMIMETypesCache < Minitest::Test
25
32
  FileUtils.rm @cache_file if File.exist? @cache_file
26
33
  end
27
34
 
28
- def test_does_not_use_cache_when_unset
29
- ENV.delete('RUBY_MIME_TYPES_CACHE')
30
- assert_equal(nil, MIME::Types::Cache.load)
31
- end
35
+ describe '.load' do
36
+ it 'does not use cache when RUBY_MIME_TYPES_CACHE is unset' do
37
+ ENV.delete('RUBY_MIME_TYPES_CACHE')
38
+ assert_nil MIME::Types::Cache.load
39
+ end
32
40
 
33
- def test_does_not_use_cache_when_missing
34
- assert_equal(nil, MIME::Types::Cache.load)
35
- end
41
+ it 'does not use cache when missing' do
42
+ assert_nil MIME::Types::Cache.load
43
+ end
36
44
 
37
- def test_does_not_create_cache_when_unset
38
- ENV.delete('RUBY_MIME_TYPES_CACHE')
39
- assert_equal(nil, MIME::Types::Cache.save)
40
- end
45
+ it 'registers the data to be updated by #add_extensions' do
46
+ MIME::Types::Cache.save
47
+ reset_mime_types
48
+ assert_equal([], MIME::Types.type_for('foo.additional'))
49
+ html = MIME::Types['text/html'][0]
50
+ html.add_extensions('additional')
51
+ assert_equal([html], MIME::Types.type_for('foo.additional'))
52
+ end
41
53
 
42
- def test_creates_cache
43
- assert_equal(false, File.exist?(@cache_file))
44
- MIME::Types::Cache.save
45
- assert_equal(true, File.exist?(@cache_file))
54
+ it 'outputs an error when there is an invalid version' do
55
+ v = MIME::Types::Data::VERSION
56
+ MIME::Types::Data.send(:remove_const, :VERSION)
57
+ MIME::Types::Data.const_set(:VERSION, '0.0')
58
+ MIME::Types::Cache.save
59
+ MIME::Types::Data.send(:remove_const, :VERSION)
60
+ MIME::Types::Data.const_set(:VERSION, v)
61
+ MIME::Types.instance_variable_set(:@__types__, nil)
62
+ assert_output '', /MIME::Types cache: invalid version/ do
63
+ MIME::Types['text/html']
64
+ end
65
+ end
66
+
67
+ it 'outputs an error when there is a marshal file incompatibility' do
68
+ MIME::Types::Cache.save
69
+ data = File.binread(@cache_file).reverse
70
+ File.open(@cache_file, 'wb') do |f| f.write(data) end
71
+ MIME::Types.instance_variable_set(:@__types__, nil)
72
+ assert_output '', /incompatible marshal file format/ do
73
+ MIME::Types['text/html']
74
+ end
75
+ end
46
76
  end
47
77
 
48
- def test_uses_cache
49
- MIME::Types['text/html'].first.extensions << 'hex'
50
- MIME::Types::Cache.save
51
- MIME::Types.instance_variable_set(:@__types__, nil)
78
+ describe '.save' do
79
+ it 'does not create cache when RUBY_MIME_TYPES_CACHE is unset' do
80
+ ENV.delete('RUBY_MIME_TYPES_CACHE')
81
+ assert_nil MIME::Types::Cache.save
82
+ end
52
83
 
53
- assert_includes(MIME::Types['text/html'].first.extensions, 'hex')
84
+ it 'creates the cache ' do
85
+ assert_equal(false, File.exist?(@cache_file))
86
+ MIME::Types::Cache.save
87
+ assert_equal(true, File.exist?(@cache_file))
88
+ end
54
89
 
55
- reset_mime_types
56
- end
90
+ it 'uses the cache' do
91
+ MIME::Types['text/html'].first.add_extensions('hex')
92
+ MIME::Types::Cache.save
93
+ MIME::Types.instance_variable_set(:@__types__, nil)
57
94
 
58
- def test_load_different_version
59
- v = MIME::Types::VERSION.dup
60
- MIME::Types::VERSION.gsub!(/.*/, '0.0')
61
- MIME::Types::Cache.save
62
- MIME::Types::VERSION.gsub!(/.*/, v)
63
- MIME::Types.instance_variable_set(:@__types__, nil)
64
- assert_output(nil, /MIME::Types cache: invalid version/) do
65
- MIME::Types['text/html']
66
- end
67
- end
95
+ assert_includes MIME::Types['text/html'].first.extensions, 'hex'
68
96
 
69
- def test_cache_load_failure
70
- MIME::Types::Cache.save
71
- data = File.binread(@cache_file).reverse
72
- File.open(@cache_file, 'wb') { |f| f.write(data) }
73
- MIME::Types.instance_variable_set(:@__types__, nil)
74
- assert_output(nil, /Could not load MIME::Types cache: incompatible marshal file format/) do
75
- MIME::Types['text/html']
97
+ reset_mime_types
76
98
  end
77
99
  end
100
+ end
78
101
 
79
- def test_container_marshalling
102
+ describe MIME::Types::Container do
103
+ it 'marshals and unmarshals correctly' do
80
104
  container = MIME::Types::Container.new
81
- # default proc should return []
82
- assert_equal([], container['abc'])
105
+ container.add('xyz', 'abc')
106
+
107
+ # default proc should return Set[]
108
+ assert_equal(Set[], container['abc'])
109
+ assert_equal(Set['abc'], container['xyz'])
83
110
 
84
111
  marshalled = Marshal.dump(container)
85
112
  loaded_container = Marshal.load(marshalled)
86
113
 
87
- # default proc should still return []
88
- assert_equal([], loaded_container['abcd'])
114
+ # default proc should still return Set[]
115
+ assert_equal(Set[], loaded_container['abc'])
116
+ assert_equal(Set['abc'], container['xyz'])
89
117
  end
90
118
  end
@@ -1,139 +1,159 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'mime/types'
4
4
  require 'minitest_helper'
5
5
 
6
- class TestMIMETypesQueryClassMethods < Minitest::Test
6
+ describe MIME::Types, 'registry' do
7
7
  def setup
8
8
  MIME::Types.send(:load_default_mime_types)
9
9
  end
10
10
 
11
- def test_enumerable
12
- assert(MIME::Types.any? { |type| type.content_type == 'text/plain' })
13
- assert_kind_of(Enumerator, MIME::Types.each)
14
- assert(MIME::Types.each.count > 999)
15
- end
11
+ describe 'is enumerable' do
12
+ it 'correctly uses an Enumerable method like #any?' do
13
+ assert(MIME::Types.any? { |type| type.content_type == 'text/plain' })
14
+ end
16
15
 
17
- def test_load_from_file
18
- fn = File.expand_path('../fixture/old-data', __FILE__)
19
- assert_deprecated('MIME::Types.load_from_file') do
20
- MIME::Types.load_from_file(fn)
16
+ it 'implements each with no parameters to return an Enumerator' do
17
+ assert_kind_of Enumerator, MIME::Types.each
18
+ assert_kind_of Enumerator, MIME::Types.map
21
19
  end
22
- end
23
20
 
24
- def test_index_with_mime_type
25
- xtxp = MIME::Type.new('x-text/x-plain')
26
- assert_includes(MIME::Types[xtxp], 'text/plain')
27
- assert_equal(1, MIME::Types[xtxp].size)
28
- end
21
+ it 'will create a lazy enumerator' do
22
+ assert_kind_of Enumerator::Lazy, MIME::Types.lazy
23
+ assert_kind_of Enumerator::Lazy, MIME::Types.map.lazy
24
+ end
29
25
 
30
- def test_index_with_regex
31
- assert_includes(MIME::Types[/plain/], 'text/plain')
32
- assert_equal(1, MIME::Types[/plain/].size)
26
+ it 'is countable with an enumerator' do
27
+ assert MIME::Types.each.count > 999
28
+ assert MIME::Types.lazy.count > 999
29
+ end
33
30
  end
34
31
 
35
- def test_index_with_string
36
- assert_includes(MIME::Types['text/plain'], 'text/plain')
37
- assert_equal(1, MIME::Types['text/plain'].size)
38
- end
32
+ describe '.[]' do
33
+ it 'can be searched with a MIME::Type' do
34
+ text_plain = MIME::Type.new('text/plain')
35
+ assert_includes MIME::Types[text_plain], 'text/plain'
36
+ assert_equal 1, MIME::Types[text_plain].size
37
+ end
39
38
 
40
- def test_index_with_complete_flag
41
- assert_empty(MIME::Types['application/1d-interleaved-parityfec', complete: true])
42
- refute_empty(MIME::Types['text/plain', complete: true])
43
- end
39
+ it 'can be searched with a regular expression' do
40
+ assert_includes MIME::Types[/plain$/], 'text/plain'
41
+ assert_equal 1, MIME::Types[/plain$/].size
42
+ end
44
43
 
45
- def test_index_with_registered_flag
46
- assert_empty(MIME::Types['application/x-wordperfect6.1',
47
- registered: true])
48
- refute_empty(MIME::Types['application/x-www-form-urlencoded',
49
- registered: true])
50
- refute_empty(MIME::Types['application/gzip', registered: true])
51
- refute_equal(MIME::Types['application/gzip'].size,
52
- MIME::Types['application/gzip', registered: true])
53
- end
44
+ it 'sorts by priority with multiple matches' do
45
+ types = MIME::Types[/gzip$/].select { |t|
46
+ %w(application/gzip application/x-gzip multipart/x-gzip).include?(t)
47
+ }
48
+ # This is this way because of a new type ending with gzip that only
49
+ # appears in some data files.
50
+ assert_equal %w(application/gzip application/x-gzip multipart/x-gzip), types
51
+ assert_equal 3, types.size
52
+ end
54
53
 
55
- def test_index_with_platform_flag
56
- assert_deprecated('MIME::Types#[]', 'using the :platform flag') do
57
- refute_empty MIME::Types['text/plain', platform: true]
54
+ it 'can be searched with a string' do
55
+ assert_includes MIME::Types['text/plain'], 'text/plain'
56
+ assert_equal 1, MIME::Types['text/plain'].size
58
57
  end
59
- end
60
58
 
61
- def test_type_for
62
- assert_equal(%w(application/xml text/xml), MIME::Types.type_for('xml'))
63
- assert_equal(%w(image/gif), MIME::Types.of('foo.gif'))
64
- assert_equal(%w(application/xml image/gif text/xml),
65
- MIME::Types.type_for(%w(xml gif)))
66
- assert_deprecated('MIME::Types#type_for', 'using the platform parameter') do
67
- refute_empty MIME::Types.type_for('gif', true)
59
+ it 'can be searched with the complete flag' do
60
+ assert_empty MIME::Types[
61
+ 'application/x-www-form-urlencoded',
62
+ complete: true
63
+ ]
64
+ assert_includes MIME::Types['text/plain', complete: true], 'text/plain'
65
+ assert_equal 1, MIME::Types['text/plain', complete: true].size
68
66
  end
69
- assert_empty(MIME::Types.type_for('zzz'))
70
- end
71
67
 
72
- def test_count
73
- assert(MIME::Types.count.nonzero?,
74
- 'A lot of types are expected to be known.')
68
+ it 'can be searched with the registered flag' do
69
+ assert_empty MIME::Types['application/x-wordperfect6.1', registered: true]
70
+ refute_empty MIME::Types[
71
+ 'application/x-www-form-urlencoded',
72
+ registered: true
73
+ ]
74
+ refute_empty MIME::Types[/gzip/, registered: true]
75
+ refute_equal MIME::Types[/gzip/], MIME::Types[/gzip/, registered: true]
76
+ end
75
77
  end
76
78
 
77
- def test_cache_file
78
- ENV['RUBY_MIME_TYPES_CACHE'] = 'foo'
79
- assert_deprecated('MIME::Types.cache_file') do
80
- assert_equal('foo', MIME::Types.cache_file)
79
+ describe '.type_for' do
80
+ it 'finds all types for a given extension' do
81
+ assert_equal %w(application/gzip application/x-gzip),
82
+ MIME::Types.type_for('gz')
81
83
  end
82
- ENV.delete('RUBY_MIME_TYPES_CACHE')
83
- assert_deprecated('MIME::Types.cache_file') do
84
- assert_nil(MIME::Types.cache_file)
84
+
85
+ it 'separates the extension from filenames' do
86
+ assert_equal %w(image/jpeg), MIME::Types.of(['foo.jpeg', 'bar.jpeg'])
85
87
  end
86
- end
87
- end
88
88
 
89
- class TestMIMETypesClassMethods < Minitest::Test
90
- def setup
91
- MIME::Types.instance_variable_set(:@__types__, nil)
92
- MIME::Types.send(:load_default_mime_types)
93
- end
89
+ it 'finds multiple extensions' do
90
+ assert_equal %w(image/jpeg text/plain),
91
+ MIME::Types.type_for(%w(foo.txt foo.jpeg))
92
+ end
94
93
 
95
- def test_add_with_type
96
- MIME::Types.add(MIME::Type.new('application/x-eruby'))
97
- refute_empty(MIME::Types['application/x-eruby'])
98
- end
94
+ it 'does not find unknown extensions' do
95
+ assert_empty MIME::Types.type_for('zzz')
96
+ end
99
97
 
100
- def test_add_with_types
101
- mt = MIME::Types.new
102
- mt.add MIME::Type.new('application/x-eruby')
103
- MIME::Types.add(mt)
104
- refute_empty(MIME::Types['application/x-eruby'])
98
+ it 'modifying type extensions causes reindexing' do
99
+ plain_text = MIME::Types['text/plain'].first
100
+ plain_text.add_extensions('xtxt')
101
+ assert_includes MIME::Types.type_for('xtxt'), 'text/plain'
102
+ end
105
103
  end
106
104
 
107
- def test_add_with_array
108
- MIME::Types.add([MIME::Type.new('application/x-eruby')])
109
- refute_empty(MIME::Types['application/x-eruby'])
105
+ describe '.count' do
106
+ it 'can count the number of types inside' do
107
+ assert MIME::Types.count > 999
108
+ end
110
109
  end
111
110
 
112
- def test_add_with_noise_suppression
113
- assert_silent do
114
- MIME::Types.add(MIME::Type.new('application/x-eruby'))
111
+ describe '.add' do
112
+ def setup
113
+ MIME::Types.instance_variable_set(:@__types__, nil)
114
+ MIME::Types.send(:load_default_mime_types)
115
+ end
116
+
117
+ let(:eruby) { MIME::Type.new('application/x-eruby') }
118
+ let(:jinja) { MIME::Type.new('application/jinja2') }
119
+
120
+ it 'successfully adds a new type' do
121
+ MIME::Types.add(eruby)
122
+ assert_equal MIME::Types['application/x-eruby'], [eruby]
115
123
  end
116
- assert_output(nil, %r{application/x-eruby is already registered}) do
117
- MIME::Types.add(MIME::Type.new('application/x-eruby'))
124
+
125
+ it 'complains about adding a duplicate type' do
126
+ MIME::Types.add(eruby)
127
+ assert_output '', /is already registered as a variant/ do
128
+ MIME::Types.add(eruby)
129
+ end
130
+ assert_equal MIME::Types['application/x-eruby'], [eruby]
118
131
  end
119
- assert_silent do
120
- MIME::Types.add(MIME::Type.new('application/x-eruby'), :silent)
132
+
133
+ it 'does not complain about adding a duplicate type when quiet' do
134
+ MIME::Types.add(eruby)
135
+ assert_silent do
136
+ MIME::Types.add(eruby, :silent)
137
+ end
138
+ assert_equal MIME::Types['application/x-eruby'], [eruby]
121
139
  end
122
- end
123
140
 
124
- def test_add_type_variant
125
- xtxp = MIME::Type.new('x-text/x-plain')
126
- assert_deprecated('MIME::Types#add_type_variant', 'and will be private') do
127
- MIME::Types.add_type_variant(xtxp)
141
+ it 'successfully adds from an array' do
142
+ MIME::Types.add([eruby, jinja])
143
+ assert_equal MIME::Types['application/x-eruby'], [eruby]
144
+ assert_equal MIME::Types['application/jinja2'], [jinja]
128
145
  end
129
- assert_includes(MIME::Types['text/plain'], xtxp)
130
- end
131
146
 
132
- def test_index_extensions
133
- xtxp = MIME::Type.new(['x-text/x-plain', %w(tzt)])
134
- assert_deprecated('MIME::Types#index_extensions', 'and will be private') do
135
- MIME::Types.index_extensions(xtxp)
147
+ it 'successfully adds from another MIME::Types' do
148
+ old_count = MIME::Types.count
149
+
150
+ mt = MIME::Types.new
151
+ mt.add(eruby)
152
+
153
+ MIME::Types.add(mt)
154
+ assert_equal old_count + 1, MIME::Types.count
155
+
156
+ assert_equal MIME::Types[eruby.content_type], [eruby]
136
157
  end
137
- assert_includes(MIME::Types.of('tzt'), xtxp)
138
158
  end
139
159
  end