mime-types 2.99.3 → 3.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +139 -0
  4. data/History.md +208 -0
  5. data/{Licence.rdoc → Licence.md} +4 -18
  6. data/Manifest.txt +8 -25
  7. data/README.rdoc +63 -67
  8. data/Rakefile +144 -55
  9. data/lib/mime/type/columnar.rb +29 -62
  10. data/lib/mime/type.rb +192 -417
  11. data/lib/mime/types/_columnar.rb +137 -0
  12. data/lib/mime/types/cache.rb +51 -73
  13. data/lib/mime/types/columnar.rb +2 -147
  14. data/lib/mime/types/container.rb +94 -0
  15. data/lib/mime/types/deprecations.rb +5 -24
  16. data/lib/mime/types/full.rb +19 -0
  17. data/lib/mime/types/loader.rb +12 -141
  18. data/lib/mime/types/logger.rb +4 -0
  19. data/lib/mime/types/registry.rb +90 -0
  20. data/lib/mime/types.rb +43 -139
  21. data/lib/mime-types.rb +1 -1
  22. data/test/minitest_helper.rb +6 -12
  23. data/test/test_mime_type.rb +473 -455
  24. data/test/test_mime_types.rb +136 -86
  25. data/test/test_mime_types_cache.rb +83 -53
  26. data/test/test_mime_types_class.rb +119 -97
  27. data/test/test_mime_types_lazy.rb +27 -23
  28. data/test/test_mime_types_loader.rb +7 -32
  29. metadata +102 -62
  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/columnar.rb +0 -88
  51. data/support/convert.rb +0 -158
  52. data/support/iana_registry.rb +0 -172
@@ -1,139 +1,161 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # -*- ruby encoding: utf-8 -*-
2
4
 
3
5
  require 'mime/types'
4
6
  require 'minitest_helper'
5
7
 
6
- class TestMIMETypesQueryClassMethods < Minitest::Test
8
+ describe MIME::Types, 'registry' do
7
9
  def setup
8
10
  MIME::Types.send(:load_default_mime_types)
9
11
  end
10
12
 
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
13
+ describe 'is enumerable' do
14
+ it 'correctly uses an Enumerable method like #any?' do
15
+ assert MIME::Types.any? { |type| type.content_type == 'text/plain' }
16
+ end
16
17
 
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)
18
+ it 'implements each with no parameters to return an Enumerator' do
19
+ assert_kind_of Enumerator, MIME::Types.each
20
+ assert_kind_of Enumerator, MIME::Types.map
21
21
  end
22
- end
23
22
 
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
23
+ it 'will create a lazy enumerator' do
24
+ assert_kind_of Enumerator::Lazy, MIME::Types.lazy
25
+ assert_kind_of Enumerator::Lazy, MIME::Types.map.lazy
26
+ end
29
27
 
30
- def test_index_with_regex
31
- assert_includes(MIME::Types[/plain/], 'text/plain')
32
- assert_equal(1, MIME::Types[/plain/].size)
28
+ it 'is countable with an enumerator' do
29
+ assert MIME::Types.each.count > 999
30
+ assert MIME::Types.lazy.count > 999
31
+ end
33
32
  end
34
33
 
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
34
+ describe '.[]' do
35
+ it 'can be searched with a MIME::Type' do
36
+ text_plain = MIME::Type.new('text/plain')
37
+ assert_includes MIME::Types[text_plain], 'text/plain'
38
+ assert_equal 1, MIME::Types[text_plain].size
39
+ end
39
40
 
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
41
+ it 'can be searched with a regular expression' do
42
+ assert_includes MIME::Types[/plain$/], 'text/plain'
43
+ assert_equal 1, MIME::Types[/plain$/].size
44
+ end
44
45
 
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
46
+ it 'sorts by priority with multiple matches' do
47
+ types = MIME::Types[/gzip$/].select { |t|
48
+ t == 'application/gzip' || t == 'application/x-gzip' || t == 'multipart/x-gzip'
49
+ }
50
+ # This is this way because of a new type ending with gzip that only
51
+ # appears in some data files.
52
+ assert_equal %w(application/gzip application/x-gzip multipart/x-gzip), types
53
+ assert_equal 3, types.size
54
+ end
54
55
 
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]
56
+ it 'can be searched with a string' do
57
+ assert_includes MIME::Types['text/plain'], 'text/plain'
58
+ assert_equal 1, MIME::Types['text/plain'].size
58
59
  end
59
- end
60
60
 
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)
61
+ it 'can be searched with the complete flag' do
62
+ assert_empty MIME::Types[
63
+ 'application/x-www-form-urlencoded',
64
+ complete: true
65
+ ]
66
+ assert_includes MIME::Types['text/plain', complete: true], 'text/plain'
67
+ assert_equal 1, MIME::Types['text/plain', complete: true].size
68
68
  end
69
- assert_empty(MIME::Types.type_for('zzz'))
70
- end
71
69
 
72
- def test_count
73
- assert(MIME::Types.count.nonzero?,
74
- 'A lot of types are expected to be known.')
70
+ it 'can be searched with the registered flag' do
71
+ assert_empty MIME::Types['application/x-wordperfect6.1', registered: true]
72
+ refute_empty MIME::Types[
73
+ 'application/x-www-form-urlencoded',
74
+ registered: true
75
+ ]
76
+ refute_empty MIME::Types[/gzip/, registered: true]
77
+ refute_equal MIME::Types[/gzip/], MIME::Types[/gzip/, registered: true]
78
+ end
75
79
  end
76
80
 
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)
81
+ describe '.type_for' do
82
+ it 'finds all types for a given extension' do
83
+ assert_equal %w(application/gzip application/x-gzip),
84
+ MIME::Types.type_for('gz')
81
85
  end
82
- ENV.delete('RUBY_MIME_TYPES_CACHE')
83
- assert_deprecated('MIME::Types.cache_file') do
84
- assert_nil(MIME::Types.cache_file)
86
+
87
+ it 'separates the extension from filenames' do
88
+ assert_equal %w(image/jpeg), MIME::Types.of(['foo.jpeg', 'bar.jpeg'])
85
89
  end
86
- end
87
- end
88
90
 
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
91
+ it 'finds multiple extensions' do
92
+ assert_equal %w(image/jpeg text/plain),
93
+ MIME::Types.type_for(%w(foo.txt foo.jpeg))
94
+ end
94
95
 
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
96
+ it 'does not find unknown extensions' do
97
+ assert_empty MIME::Types.type_for('zzz')
98
+ end
99
99
 
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'])
100
+ it 'modifying type extensions causes reindexing' do
101
+ plain_text = MIME::Types['text/plain'].first
102
+ plain_text.add_extensions('xtxt')
103
+ assert_includes MIME::Types.type_for('xtxt'), 'text/plain'
104
+ end
105
105
  end
106
106
 
107
- def test_add_with_array
108
- MIME::Types.add([MIME::Type.new('application/x-eruby')])
109
- refute_empty(MIME::Types['application/x-eruby'])
107
+ describe '.count' do
108
+ it 'can count the number of types inside' do
109
+ assert MIME::Types.count > 999
110
+ end
110
111
  end
111
112
 
112
- def test_add_with_noise_suppression
113
- assert_silent do
114
- MIME::Types.add(MIME::Type.new('application/x-eruby'))
113
+ describe '.add' do
114
+ def setup
115
+ MIME::Types.instance_variable_set(:@__types__, nil)
116
+ MIME::Types.send(:load_default_mime_types)
117
+ end
118
+
119
+ let(:eruby) { MIME::Type.new('application/x-eruby') }
120
+ let(:jinja) { MIME::Type.new('application/jinja2' )}
121
+
122
+ it 'successfully adds a new type' do
123
+ MIME::Types.add(eruby)
124
+ assert_equal MIME::Types['application/x-eruby'], [ eruby ]
115
125
  end
116
- assert_output(nil, %r{application/x-eruby is already registered}) do
117
- MIME::Types.add(MIME::Type.new('application/x-eruby'))
126
+
127
+ it 'complains about adding a duplicate type' do
128
+ MIME::Types.add(eruby)
129
+ assert_output '', /is already registered as a variant/ do
130
+ MIME::Types.add(eruby)
131
+ end
132
+ assert_equal MIME::Types['application/x-eruby'], [eruby]
118
133
  end
119
- assert_silent do
120
- MIME::Types.add(MIME::Type.new('application/x-eruby'), :silent)
134
+
135
+ it 'does not complain about adding a duplicate type when quiet' do
136
+ MIME::Types.add(eruby)
137
+ assert_silent do
138
+ MIME::Types.add(eruby, :silent)
139
+ end
140
+ assert_equal MIME::Types['application/x-eruby'], [ eruby ]
121
141
  end
122
- end
123
142
 
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)
143
+ it 'successfully adds from an array' do
144
+ MIME::Types.add([ eruby, jinja ])
145
+ assert_equal MIME::Types['application/x-eruby'], [ eruby ]
146
+ assert_equal MIME::Types['application/jinja2'], [ jinja ]
128
147
  end
129
- assert_includes(MIME::Types['text/plain'], xtxp)
130
- end
131
148
 
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)
149
+ it 'successfully adds from another MIME::Types' do
150
+ old_count = MIME::Types.count
151
+
152
+ mt = MIME::Types.new
153
+ mt.add(eruby)
154
+
155
+ MIME::Types.add(mt)
156
+ assert_equal old_count + 1, MIME::Types.count
157
+
158
+ assert_equal MIME::Types[eruby.content_type], [ eruby ]
136
159
  end
137
- assert_includes(MIME::Types.of('tzt'), xtxp)
138
160
  end
139
161
  end
@@ -1,47 +1,51 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # -*- ruby encoding: utf-8 -*-
2
4
 
3
5
  require 'mime/types'
4
6
  require 'minitest_helper'
5
7
 
6
- class TestMIMETypesLazy < Minitest::Test
8
+ describe MIME::Types, 'lazy loading' do
7
9
  def setup
8
- @cache_file = File.expand_path('../cache.tst', __FILE__)
9
10
  ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'true'
10
- ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
11
- MIME::Types::Cache.save
12
11
  end
13
12
 
14
13
  def teardown
15
- clear_cache_file
16
14
  reset_mime_types
17
- if File.exist? ENV['RUBY_MIME_TYPES_CACHE']
18
- FileUtils.rm ENV['RUBY_MIME_TYPES_CACHE']
19
- ENV.delete('RUBY_MIME_TYPES_CACHE')
20
- end
21
15
  ENV.delete('RUBY_MIME_TYPES_LAZY_LOAD')
22
16
  end
23
17
 
24
- def clear_cache_file
25
- FileUtils.rm @cache_file if File.exist? @cache_file
26
- end
27
-
28
18
  def reset_mime_types
29
19
  MIME::Types.instance_variable_set(:@__types__, nil)
30
20
  MIME::Types.send(:load_default_mime_types)
31
21
  end
32
22
 
33
- def test_lazy_load?
34
- assert_equal(true, MIME::Types.send(:lazy_load?))
35
- ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = nil
36
- assert_equal(nil, MIME::Types.send(:lazy_load?))
37
- ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'false'
38
- assert_equal(false, MIME::Types.send(:lazy_load?))
23
+ describe '.lazy_load?' do
24
+ it 'is true when RUBY_MIME_TYPES_LAZY_LOAD is set' do
25
+ assert_output '', /RUBY_MIME_TYPES_LAZY_LOAD/ do
26
+ assert_equal true, MIME::Types.send(:lazy_load?)
27
+ end
28
+ end
29
+
30
+ it 'is nil when RUBY_MIME_TYPES_LAZY_LOAD is unset' do
31
+ ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = nil
32
+ assert_output '', '' do
33
+ assert_nil MIME::Types.send(:lazy_load?)
34
+ end
35
+ end
36
+
37
+ it 'is false when RUBY_MIME_TYPES_LAZY_LOAD is false' do
38
+ ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'false'
39
+ assert_output '', /RUBY_MIME_TYPES_LAZY_LOAD/ do
40
+ assert_equal false, MIME::Types.send(:lazy_load?)
41
+ end
42
+ end
39
43
  end
40
44
 
41
- def test_lazy_loading
45
+ it 'loads lazily when RUBY_MIME_TYPES_LAZY_LOAD is set' do
42
46
  MIME::Types.instance_variable_set(:@__types__, nil)
43
- assert_nil(MIME::Types.instance_variable_get(:@__types__))
44
- refute_nil(MIME::Types['text/html'].first)
45
- refute_nil(MIME::Types.instance_variable_get(:@__types__))
47
+ assert_nil MIME::Types.instance_variable_get(:@__types__)
48
+ refute_nil MIME::Types['text/html'].first
49
+ refute_nil MIME::Types.instance_variable_get(:@__types__)
46
50
  end
47
51
  end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # -*- ruby encoding: utf-8 -*-
2
4
 
3
5
  require 'mime/types'
4
6
  require 'minitest_helper'
5
7
 
6
- class TestMIMETypesLoader < Minitest::Test
8
+ describe MIME::Types::Loader do
7
9
  def setup
8
10
  @path = File.expand_path('../fixture', __FILE__)
9
11
  @loader = MIME::Types::Loader.new(@path)
@@ -12,48 +14,21 @@ class TestMIMETypesLoader < Minitest::Test
12
14
 
13
15
  def assert_correctly_loaded(types)
14
16
  assert_includes(types, 'application/1d-interleaved-parityfec')
15
- assert_deprecated('MIME::Type#references') do
16
- assert_empty types['application/acad'].first.references
17
- end
18
- assert_deprecated('MIME::Type#urls') do
19
- assert_empty types['audio/webm'].first.urls
20
- end
21
17
  assert_equal(%w(webm), types['audio/webm'].first.extensions)
22
18
  refute(types['audio/webm'].first.registered?)
23
19
 
24
20
  assert_equal('Fixes a bug with IE6 and progressive JPEGs',
25
21
  types['image/pjpeg'].first.docs)
26
22
 
27
- assert_deprecated('MIME::Type#system?') do
28
- refute types['application/x-apple-diskimage'].first.system?
29
- end
30
- assert_deprecated('MIME::Type#system') do
31
- assert_nil types['application/x-apple-diskimage'].first.system
32
- end
33
-
34
23
  assert(types['audio/vnd.qcelp'].first.obsolete?)
35
24
  assert_equal('audio/QCELP', types['audio/vnd.qcelp'].first.use_instead)
36
25
  end
37
26
 
38
- def test_load_yaml
39
- assert_correctly_loaded(@loader.load_yaml)
40
- end
41
-
42
- def test_load_json
43
- assert_correctly_loaded(@loader.load_json)
44
- end
45
-
46
- def test_load_v1
47
- assert_deprecated('MIME::Types::Loader.load_v1') do
48
- assert_correctly_loaded(@loader.load_v1)
49
- end
27
+ it 'loads YAML files correctly' do
28
+ assert_correctly_loaded @loader.load_yaml
50
29
  end
51
30
 
52
- def test_malformed_v1
53
- assert_output(nil, /1: Parsing error in v1 MIME type definition/) {
54
- assert_raises(MIME::Types::Loader::BadV1Format) {
55
- MIME::Types::Loader.load_from_v1(File.join(@bad_path, 'malformed'))
56
- }
57
- }
31
+ it 'loads JSON files correctly' do
32
+ assert_correctly_loaded @loader.load_json
58
33
  end
59
34
  end