mime-types 2.99.3 → 3.4.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} +29 -30
  3. data/Contributing.md +132 -0
  4. data/History.md +269 -0
  5. data/{Licence.rdoc → Licence.md} +4 -18
  6. data/Manifest.txt +8 -25
  7. data/README.rdoc +63 -73
  8. data/Rakefile +200 -97
  9. data/lib/mime/type/columnar.rb +30 -63
  10. data/lib/mime/type.rb +294 -458
  11. data/lib/mime/types/_columnar.rb +137 -0
  12. data/lib/mime/types/cache.rb +52 -74
  13. data/lib/mime/types/columnar.rb +2 -147
  14. data/lib/mime/types/container.rb +96 -0
  15. data/lib/mime/types/deprecations.rb +17 -34
  16. data/lib/mime/types/full.rb +19 -0
  17. data/lib/mime/types/loader.rb +36 -152
  18. data/lib/mime/types/logger.rb +7 -5
  19. data/lib/mime/types/registry.rb +90 -0
  20. data/lib/mime/types.rb +55 -148
  21. data/lib/mime-types.rb +2 -2
  22. data/test/minitest_helper.rb +8 -18
  23. data/test/test_mime_type.rb +489 -464
  24. data/test/test_mime_types.rb +139 -91
  25. data/test/test_mime_types_cache.rb +85 -57
  26. data/test/test_mime_types_class.rb +120 -100
  27. data/test/test_mime_types_lazy.rb +30 -28
  28. data/test/test_mime_types_loader.rb +18 -45
  29. metadata +92 -77
  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,159 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'mime/types'
4
- require 'minitest_helper'
3
+ require "mime/types"
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
@@ -1,28 +1,16 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'mime/types'
4
- require 'minitest_helper'
3
+ require "mime/types"
4
+ require "minitest_helper"
5
5
 
6
- class TestMIMETypesLazy < Minitest::Test
6
+ describe MIME::Types, "lazy loading" do
7
7
  def setup
8
- @cache_file = File.expand_path('../cache.tst', __FILE__)
9
- ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'true'
10
- ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
11
- MIME::Types::Cache.save
8
+ ENV["RUBY_MIME_TYPES_LAZY_LOAD"] = "true"
12
9
  end
13
10
 
14
11
  def teardown
15
- clear_cache_file
16
12
  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
- ENV.delete('RUBY_MIME_TYPES_LAZY_LOAD')
22
- end
23
-
24
- def clear_cache_file
25
- FileUtils.rm @cache_file if File.exist? @cache_file
13
+ ENV.delete("RUBY_MIME_TYPES_LAZY_LOAD")
26
14
  end
27
15
 
28
16
  def reset_mime_types
@@ -30,18 +18,32 @@ class TestMIMETypesLazy < Minitest::Test
30
18
  MIME::Types.send(:load_default_mime_types)
31
19
  end
32
20
 
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?))
21
+ describe ".lazy_load?" do
22
+ it "is true when RUBY_MIME_TYPES_LAZY_LOAD is set" do
23
+ assert_output "", /RUBY_MIME_TYPES_LAZY_LOAD/ do
24
+ assert_equal true, MIME::Types.send(:lazy_load?)
25
+ end
26
+ end
27
+
28
+ it "is nil when RUBY_MIME_TYPES_LAZY_LOAD is unset" do
29
+ ENV["RUBY_MIME_TYPES_LAZY_LOAD"] = nil
30
+ assert_output "", "" do
31
+ assert_nil MIME::Types.send(:lazy_load?)
32
+ end
33
+ end
34
+
35
+ it "is false when RUBY_MIME_TYPES_LAZY_LOAD is false" do
36
+ ENV["RUBY_MIME_TYPES_LAZY_LOAD"] = "false"
37
+ assert_output "", /RUBY_MIME_TYPES_LAZY_LOAD/ do
38
+ assert_equal false, MIME::Types.send(:lazy_load?)
39
+ end
40
+ end
39
41
  end
40
42
 
41
- def test_lazy_loading
43
+ it "loads lazily when RUBY_MIME_TYPES_LAZY_LOAD is set" do
42
44
  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__))
45
+ assert_nil MIME::Types.instance_variable_get(:@__types__)
46
+ refute_nil MIME::Types["text/html"].first
47
+ refute_nil MIME::Types.instance_variable_get(:@__types__)
46
48
  end
47
49
  end
@@ -1,59 +1,32 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'mime/types'
4
- require 'minitest_helper'
3
+ require "mime/types"
4
+ require "minitest_helper"
5
5
 
6
- class TestMIMETypesLoader < Minitest::Test
6
+ describe MIME::Types::Loader do
7
7
  def setup
8
- @path = File.expand_path('../fixture', __FILE__)
9
- @loader = MIME::Types::Loader.new(@path)
10
- @bad_path = File.expand_path('../bad-fixtures', __FILE__)
8
+ @path = File.expand_path("../fixture", __FILE__)
9
+ @loader = MIME::Types::Loader.new(@path)
10
+ @bad_path = File.expand_path("../bad-fixtures", __FILE__)
11
11
  end
12
12
 
13
13
  def assert_correctly_loaded(types)
14
- 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
- assert_equal(%w(webm), types['audio/webm'].first.extensions)
22
- refute(types['audio/webm'].first.registered?)
14
+ assert_includes(types, "application/1d-interleaved-parityfec")
15
+ assert_equal(%w[webm], types["audio/webm"].first.extensions)
16
+ refute(types["audio/webm"].first.registered?)
23
17
 
24
- assert_equal('Fixes a bug with IE6 and progressive JPEGs',
25
- types['image/pjpeg'].first.docs)
18
+ assert_equal("Fixes a bug with IE6 and progressive JPEGs",
19
+ types["image/pjpeg"].first.docs)
26
20
 
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
- assert(types['audio/vnd.qcelp'].first.obsolete?)
35
- assert_equal('audio/QCELP', types['audio/vnd.qcelp'].first.use_instead)
36
- end
37
-
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)
21
+ assert(types["audio/vnd.qcelp"].first.obsolete?)
22
+ assert_equal("audio/QCELP", types["audio/vnd.qcelp"].first.use_instead)
44
23
  end
45
24
 
46
- def test_load_v1
47
- assert_deprecated('MIME::Types::Loader.load_v1') do
48
- assert_correctly_loaded(@loader.load_v1)
49
- end
25
+ it "loads YAML files correctly" do
26
+ assert_correctly_loaded @loader.load_yaml
50
27
  end
51
28
 
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
- }
29
+ it "loads JSON files correctly" do
30
+ assert_correctly_loaded @loader.load_json
58
31
  end
59
32
  end