mime-types 1.25.1 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +8 -8
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.autotest +5 -0
  5. data/.minitest.rb +2 -0
  6. data/.travis.yml +0 -4
  7. data/Contributing.rdoc +13 -14
  8. data/Gemfile +1 -0
  9. data/History.rdoc +100 -7
  10. data/Licence.rdoc +1 -1
  11. data/Manifest.txt +17 -24
  12. data/README.rdoc +26 -47
  13. data/Rakefile +42 -185
  14. data/data/mime-types.json +1 -0
  15. data/docs/COPYING.txt +339 -339
  16. data/docs/artistic.txt +127 -127
  17. data/lib/mime.rb +50 -0
  18. data/lib/mime/type.rb +634 -0
  19. data/lib/mime/types.rb +254 -912
  20. data/lib/mime/types/cache.rb +73 -0
  21. data/lib/mime/types/loader.rb +248 -0
  22. data/lib/mime/types/loader_path.rb +16 -0
  23. data/support/benchmarker.rb +55 -0
  24. data/support/convert.rb +130 -0
  25. data/support/iana_downloader.rb +201 -0
  26. data/test/fixture/json.json +1 -0
  27. data/test/fixture/old-data +9 -0
  28. data/test/fixture/yaml.yaml +75 -0
  29. data/test/minitest_helper.rb +22 -0
  30. data/test/test_mime_type.rb +337 -143
  31. data/test/test_mime_types.rb +75 -84
  32. data/test/test_mime_types_cache.rb +30 -29
  33. data/test/test_mime_types_class.rb +135 -0
  34. data/test/test_mime_types_lazy.rb +3 -2
  35. data/test/test_mime_types_loader.rb +42 -0
  36. metadata +61 -90
  37. metadata.gz.sig +0 -0
  38. data/lib/mime/types/application +0 -1010
  39. data/lib/mime/types/application.mac +0 -3
  40. data/lib/mime/types/application.nonstandard +0 -132
  41. data/lib/mime/types/application.obsolete +0 -41
  42. data/lib/mime/types/audio +0 -138
  43. data/lib/mime/types/audio.nonstandard +0 -11
  44. data/lib/mime/types/audio.obsolete +0 -1
  45. data/lib/mime/types/image +0 -46
  46. data/lib/mime/types/image.nonstandard +0 -20
  47. data/lib/mime/types/image.obsolete +0 -5
  48. data/lib/mime/types/message +0 -18
  49. data/lib/mime/types/message.obsolete +0 -2
  50. data/lib/mime/types/model +0 -15
  51. data/lib/mime/types/multipart +0 -14
  52. data/lib/mime/types/multipart.nonstandard +0 -1
  53. data/lib/mime/types/multipart.obsolete +0 -7
  54. data/lib/mime/types/other.nonstandard +0 -8
  55. data/lib/mime/types/text +0 -61
  56. data/lib/mime/types/text.nonstandard +0 -7
  57. data/lib/mime/types/text.obsolete +0 -8
  58. data/lib/mime/types/text.vms +0 -1
  59. data/lib/mime/types/video +0 -75
  60. data/lib/mime/types/video.nonstandard +0 -16
  61. data/lib/mime/types/video.obsolete +0 -3
@@ -0,0 +1,201 @@
1
+ # -*- ruby encoding: utf-8 -*-
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+
5
+ require 'open-uri'
6
+ require 'nokogiri'
7
+ require 'cgi'
8
+ require 'fileutils'
9
+ require 'yaml'
10
+
11
+ ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes'
12
+ require 'mime/types'
13
+
14
+ class IANADownloader
15
+ INDEX_URL = %q(https://www.iana.org/assignments/media-types/)
16
+ MIME_HREF = %r{/assignments/media-types/(.+)/?$}
17
+
18
+ def self.download_to(destination)
19
+ new(destination).download_all
20
+ end
21
+
22
+ attr_reader :destination
23
+
24
+ def initialize(destination = nil)
25
+ @destination =
26
+ File.expand_path(destination ||
27
+ File.expand_path('../../type-lists', __FILE__))
28
+ end
29
+
30
+ def download_all
31
+ puts "Downloading index of MIME types from #{INDEX_URL}."
32
+ index = Nokogiri::HTML(open(INDEX_URL) { |f| f.read })
33
+ index.xpath('//a').each do |tag|
34
+ next unless tag['href']
35
+ href_match = MIME_HREF.match(tag['href'])
36
+ next unless href_match
37
+ href = href_match.captures.first
38
+ next if tag.content == 'example'
39
+ download_one(href, tag.content, href)
40
+ end
41
+ end
42
+
43
+ def download_one(url, name = url, type = nil)
44
+ if url =~ %r{^https?://}
45
+ name = File.basename(url) if name == url
46
+ else
47
+ url = File.join(INDEX_URL, url)
48
+ end
49
+
50
+ Parser.download(name, from: url, to: @destination, type: type)
51
+ end
52
+ end
53
+
54
+ class IANADownloader::Parser
55
+ def self.download(name, options = {})
56
+ new(name, options) do |parser|
57
+ parser.parse(parser.download)
58
+ parser.save
59
+ end
60
+ end
61
+
62
+ def initialize(name, options = {})
63
+ raise ArgumentError, ":from not specified" unless options[:from]
64
+ raise ArgumentError, ":to not specified" unless options[:to]
65
+
66
+ @name = "#{File.basename(name, '.yml')}.yml"
67
+ @from = options[:from]
68
+ @to = File.expand_path(options[:to])
69
+ @type = File.basename(options[:type] || name, '.yml')
70
+ @file = File.join(@to, @name)
71
+ @types = load_mime_types || MIME::Types.new
72
+
73
+ yield self if block_given?
74
+ end
75
+
76
+ def download
77
+ puts "Downloading #{@name} from #{@from}"
78
+ Nokogiri::HTML(open(@from) { |f| f.read })
79
+ end
80
+
81
+ def parse(html)
82
+ nodes = html.xpath('//table//table//tr')
83
+
84
+ # How many <td> children does the first node have?
85
+ node_count = child_elems(nodes.first).size
86
+
87
+ if node_count == 1
88
+ # The title node doesn't have what we expect. Let's try it based on
89
+ # the first real node.
90
+ node_count = child_elems(nodes.first.next).size
91
+ end
92
+
93
+ nodes.each do |node|
94
+ next if node == nodes.first
95
+
96
+ elems = child_elems(node)
97
+ next if elems.size.zero?
98
+
99
+ if elems.size != node_count
100
+ warn "size mismatch (#{elems.size} != #{node_count}) in node: #{node}"
101
+ next
102
+ end
103
+
104
+ sub_ix, ref_ix = case elems.size
105
+ when 3
106
+ [ 1, 2 ]
107
+ when 4
108
+ [ 1, 3 ]
109
+ else
110
+ warn "size error (#{elems.size} != {3,4}) in node: #{node}"
111
+ raise
112
+ end
113
+ subtype = elems[sub_ix].content.chomp.strip
114
+ refs = child_elems(elems[ref_ix]).map { |ref|
115
+ ref = ref.xpath('a') unless ref.name == 'a'
116
+ [ ref ].flatten.map { |r| href_to_ref(r) }
117
+
118
+ }.flatten
119
+
120
+ content_type = [ @type, subtype].join('/')
121
+ use_instead = nil
122
+ obsolete = false
123
+
124
+ if content_type =~ OBSOLETE
125
+ content_type = $1
126
+ obsolete = true
127
+ elsif content_type =~ DEPRECATED
128
+ content_type = $1
129
+ use_instead = [ $2 ]
130
+ obsolete = true
131
+ end
132
+
133
+ types = @types.select { |t|
134
+ (t.content_type == content_type)
135
+ }
136
+
137
+ if types.empty?
138
+ MIME::Type.new(content_type) do |mt|
139
+ mt.references = %w(IANA) + refs
140
+ mt.registered = true
141
+ mt.obsolete = obsolete if obsolete
142
+ mt.use_instead = use_instead if use_instead
143
+ @types << mt
144
+ end
145
+ else
146
+ types.each { |mt|
147
+ mt.references = %w(IANA) + refs
148
+ mt.registered = true
149
+ mt.obsolete = obsolete if obsolete
150
+ mt.use_instead = use_instead if use_instead
151
+ }
152
+ end
153
+ end
154
+ end
155
+
156
+ def save
157
+ FileUtils.mkdir_p(@to)
158
+ File.open(@file, 'wb') { |f|
159
+ f.puts @types.map.to_a.sort.to_yaml
160
+ }
161
+ end
162
+
163
+ private
164
+ def child_elems(node)
165
+ node.children.select { |n| n.elem? }
166
+ end
167
+
168
+ def load_mime_types
169
+ if File.exist?(@file)
170
+ MIME::Types::Loader.load_from_yaml(@file)
171
+ end
172
+ end
173
+
174
+ def href_to_ref(ref)
175
+ case ref['href']
176
+ when CONTACT_PEOPLE
177
+ tag = CGI::unescape($1).chomp.strip
178
+ if tag == ref.content
179
+ "[#{ref.content}]"
180
+ else
181
+ "[#{ref.content}=#{tag}]"
182
+ end
183
+ when RFC_EDITOR, IETF_RFC, IETF_RFC_TOOLS
184
+ "RFC#$1"
185
+ when RFC_BAD_EDITOR
186
+ ref.content
187
+ when %r{(https?://.*)}
188
+ "{#{ref.content}=#$1}"
189
+ else
190
+ ref
191
+ end
192
+ end
193
+
194
+ CONTACT_PEOPLE = %r{https?://www.iana.org/assignments/contact-people.html?l?#(.*)}
195
+ RFC_EDITOR = %r{https?://www.rfc-editor.org/rfc/rfc(\d+).txt}
196
+ RFC_BAD_EDITOR = %r{https?://www.rfc-editor.org/rfc/rfcxxxx.txt}
197
+ IETF_RFC = %r{https?://www.ietf.org/rfc/rfc(\d+).txt}
198
+ IETF_RFC_TOOLS = %r{https?://tools.ietf.org/html/rfc(\d+)}
199
+ OBSOLETE = %r{(.+)\s+\((?:obsolete|deprecated)\)}i
200
+ DEPRECATED = %r{(.+)\s+-\s+DEPRECATED\s+-\s+Please\s+use\s+(.+)}
201
+ end
@@ -0,0 +1 @@
1
+ [{"content-type":"application/smil","encoding":"8bit","extensions":["smi","smil"],"obsolete":true,"use-instead":["application/smil+xml"],"references":["IANA","RFC4536"],"registered":true},{"content-type":"audio/vnd.qcelp","encoding":"base64","extensions":["qcp"],"obsolete":true,"use-instead":["audio/QCELP"],"references":["IANA","RFC3625"],"registered":true},{"content-type":"image/bmp","encoding":"base64","extensions":["bmp"],"obsolete":true,"use-instead":["image/x-bmp"],"registered":false},{"content-type":"application/acad","encoding":"base64","references":["LTSW"],"registered":false},{"content-type":"audio/webm","encoding":"base64","extensions":["webm"],"references":["{WebM=http://www.webmproject.org/code/specs/container/}"],"registered":false},{"content-type":"image/pjpeg","docs":"Fixes a bug with IE6 and progressive JPEGs","encoding":"base64","registered":false},{"content-type":"application/1d-interleaved-parityfec","encoding":"base64","references":["IANA","RFC6015"],"registered":true},{"content-type":"audio/1d-interleaved-parityfec","encoding":"base64","references":["IANA","RFC6015"],"registered":true},{"content-type":"application/x-apple-diskimage","encoding":"base64","extensions":["dmg"],"registered":false,"system":"mac"}]
@@ -0,0 +1,9 @@
1
+ !application/smil @smi,smil :8bit 'IANA,RFC4536 =use-instead:application/smil+xml
2
+ !audio/vnd.qcelp @qcp 'IANA,RFC3625 =use-instead:audio/QCELP
3
+ *!image/bmp @bmp =use-instead:image/x-bmp
4
+ *application/acad 'LTSW
5
+ *audio/webm @webm '{WebM=http://www.webmproject.org/code/specs/container/}
6
+ *image/pjpeg :base64 =Fixes a bug with IE6 and progressive JPEGs
7
+ application/1d-interleaved-parityfec 'IANA,RFC6015
8
+ audio/1d-interleaved-parityfec 'IANA,RFC6015
9
+ mac:application/x-apple-diskimage @dmg
@@ -0,0 +1,75 @@
1
+ ---
2
+ - !ruby/object:MIME::Type
3
+ content-type: application/smil
4
+ encoding: 8bit
5
+ extensions:
6
+ - smi
7
+ - smil
8
+ obsolete: true
9
+ use-instead:
10
+ - application/smil+xml
11
+ references:
12
+ - IANA
13
+ - RFC4536
14
+ registered: true
15
+ - !ruby/object:MIME::Type
16
+ content-type: audio/vnd.qcelp
17
+ encoding: base64
18
+ extensions:
19
+ - qcp
20
+ obsolete: true
21
+ use-instead:
22
+ - audio/QCELP
23
+ references:
24
+ - IANA
25
+ - RFC3625
26
+ registered: true
27
+ - !ruby/object:MIME::Type
28
+ content-type: image/bmp
29
+ encoding: base64
30
+ extensions:
31
+ - bmp
32
+ obsolete: true
33
+ use-instead:
34
+ - image/x-bmp
35
+ registered: false
36
+ - !ruby/object:MIME::Type
37
+ content-type: application/acad
38
+ encoding: base64
39
+ references:
40
+ - LTSW
41
+ registered: false
42
+ - !ruby/object:MIME::Type
43
+ content-type: audio/webm
44
+ encoding: base64
45
+ extensions:
46
+ - webm
47
+ references:
48
+ - ! '{WebM=http://www.webmproject.org/code/specs/container/}'
49
+ registered: false
50
+ - !ruby/object:MIME::Type
51
+ content-type: image/pjpeg
52
+ docs: Fixes a bug with IE6 and progressive JPEGs
53
+ encoding: base64
54
+ registered: false
55
+ - !ruby/object:MIME::Type
56
+ content-type: application/1d-interleaved-parityfec
57
+ encoding: base64
58
+ references:
59
+ - IANA
60
+ - RFC6015
61
+ registered: true
62
+ - !ruby/object:MIME::Type
63
+ content-type: audio/1d-interleaved-parityfec
64
+ encoding: base64
65
+ references:
66
+ - IANA
67
+ - RFC6015
68
+ registered: true
69
+ - !ruby/object:MIME::Type
70
+ content-type: application/x-apple-diskimage
71
+ encoding: base64
72
+ extensions:
73
+ - dmg
74
+ registered: false
75
+ system: !ruby/regexp /mac/
@@ -0,0 +1,22 @@
1
+ # -*- ruby encoding: utf-8 -*-
2
+
3
+ require 'mime/type'
4
+ require 'fileutils'
5
+
6
+ gem 'minitest'
7
+ require 'minitest/autorun'
8
+
9
+ module MIME
10
+ @__deprecated = Hash.new { |h, k| h[k] = true }
11
+
12
+ class << self
13
+ attr_reader :__deprecated
14
+ end
15
+ end
16
+
17
+ def assert_deprecated(name, message = "and will be removed")
18
+ MIME.__deprecated[name] = false
19
+ assert_output(nil, /#{Regexp.escape(name)} is deprecated #{Regexp.escape(message)}./) { yield }
20
+ ensure
21
+ MIME.__deprecated[name] = true
22
+ end
@@ -1,46 +1,77 @@
1
1
  # -*- ruby encoding: utf-8 -*-
2
2
 
3
3
  require 'mime/types'
4
+ require 'minitest_helper'
4
5
 
5
6
  class TestMIMEType < Minitest::Test
6
- def yaml_mime_type_from_array
7
- MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit', 'd9d172f608')
7
+ def make(content_type)
8
+ MIME::Type.new(content_type) { |mt| yield mt if block_given? }
9
+ end
10
+
11
+ def make_yaml_mime_type
12
+ make('text/x-yaml') do |yaml|
13
+ yaml.extensions = %w(yaml yml)
14
+ yaml.encoding = '8bit'
15
+ yaml.system = 'd9d172f608'
16
+ end
17
+ end
18
+
19
+ def make_yaml_mime_type_with_docs
20
+ make('text/x-yaml') do |yaml|
21
+ yaml.extensions = %w(yaml yml)
22
+ yaml.encoding = '8bit'
23
+ yaml.system = 'd9d172f608'
24
+ yaml.docs = 'Test YAML'
25
+ end
8
26
  end
9
27
 
10
28
  def setup
11
- @zip = MIME::Type.new('x-appl/x-zip') { |t| t.extensions = ['zip', 'zp'] }
29
+ @applzip = MIME::Type.new('x-appl/x-zip') { |t|
30
+ t.extensions = ['zip', 'zp']
31
+ }
12
32
  end
13
33
 
14
34
  def test_class_from_array
15
- yaml = yaml_mime_type_from_array
35
+ yaml = nil
36
+ assert_deprecated("MIME::Type.from_array") do
37
+ yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit',
38
+ 'd9d172f608')
39
+ end
16
40
  assert_instance_of(MIME::Type, yaml)
17
41
  assert_equal('text/yaml', yaml.simplified)
42
+ assert_raises(ArgumentError) { MIME::Type.from_array }
18
43
  end
19
44
 
20
45
  def test_class_from_hash
21
- yaml = MIME::Type.from_hash('Content-Type' => 'text/x-yaml',
22
- 'Content-Transfer-Encoding' => '8bit',
23
- 'System' => 'd9d172f608',
24
- 'Extensions' => %w(yaml yml))
46
+ yaml = nil
47
+ assert_deprecated("MIME::Type.from_hash") do
48
+ yaml = MIME::Type.from_hash('Content-Type' => 'text/x-yaml',
49
+ 'Content-Transfer-Encoding' => '8bit',
50
+ 'System' => 'd9d172f608',
51
+ 'Extensions' => %w(yaml yml))
52
+ end
25
53
  assert_instance_of(MIME::Type, yaml)
26
54
  assert_equal('text/yaml', yaml.simplified)
27
55
  end
28
56
 
29
57
  def test_class_from_mime_type
30
- zip2 = MIME::Type.from_mime_type(@zip)
31
- assert_instance_of(MIME::Type, @zip)
32
- assert_equal('appl/zip', @zip.simplified)
33
- refute_equal(@zip.object_id, zip2.object_id)
58
+ zip2 = nil
59
+ assert_deprecated("MIME::Type.from_mime_type") do
60
+ zip2 = MIME::Type.from_mime_type(@applzip)
61
+ end
62
+ assert_instance_of(MIME::Type, @applzip)
63
+ assert_equal('appl/zip', @applzip.simplified)
64
+ refute_equal(@applzip.object_id, zip2.object_id)
34
65
  end
35
66
 
36
67
  def test_class_simplified
37
- assert_equal(MIME::Type.simplified('text/plain'), 'text/plain')
38
- assert_equal(MIME::Type.simplified('image/jpeg'), 'image/jpeg')
39
- assert_equal(MIME::Type.simplified('application/x-msword'), 'application/msword')
40
- assert_equal(MIME::Type.simplified('text/vCard'), 'text/vcard')
41
- assert_equal(MIME::Type.simplified('application/pkcs7-mime'), 'application/pkcs7-mime')
42
- assert_equal(@zip.simplified, 'appl/zip')
43
- assert_equal(MIME::Type.simplified('x-xyz/abc'), 'xyz/abc')
68
+ assert_equal('text/plain', MIME::Type.simplified('text/plain'))
69
+ assert_equal('image/jpeg', MIME::Type.simplified('image/jpeg'))
70
+ assert_equal('application/msword', MIME::Type.simplified('application/x-msword'))
71
+ assert_equal('text/vcard', MIME::Type.simplified('text/vCard'))
72
+ assert_equal('application/pkcs7-mime', MIME::Type.simplified('application/pkcs7-mime'))
73
+ assert_equal('xyz/abc', MIME::Type.simplified('x-xyz/abc'))
74
+ assert_nil(MIME::Type.simplified('text'))
44
75
  end
45
76
 
46
77
  def test_CMP # '<=>'
@@ -63,7 +94,7 @@ class TestMIMEType < Minitest::Test
63
94
  refute(MIME::Type.new('application/x-msword').ascii?)
64
95
  assert(MIME::Type.new('text/vCard').ascii?)
65
96
  refute(MIME::Type.new('application/pkcs7-mime').ascii?)
66
- refute(@zip.ascii?)
97
+ refute(@applzip.ascii?)
67
98
  end
68
99
 
69
100
  def test_binary_eh
@@ -72,52 +103,69 @@ class TestMIMEType < Minitest::Test
72
103
  assert(MIME::Type.new('application/x-msword').binary?)
73
104
  refute(MIME::Type.new('text/vCard').binary?)
74
105
  assert(MIME::Type.new('application/pkcs7-mime').binary?)
75
- assert(@zip.binary?)
106
+ assert(@applzip.binary?)
76
107
  end
77
108
 
78
109
  def test_complete_eh
79
- yaml = yaml_mime_type_from_array
110
+ yaml = make_yaml_mime_type
80
111
  assert(yaml.complete?)
81
112
  yaml.extensions = nil
82
113
  refute(yaml.complete?)
83
114
  end
84
115
 
85
116
  def test_content_type
86
- assert_equal(MIME::Type.new('text/plain').content_type, 'text/plain')
87
- assert_equal(MIME::Type.new('image/jpeg').content_type, 'image/jpeg')
88
- assert_equal(MIME::Type.new('application/x-msword').content_type, 'application/x-msword')
89
- assert_equal(MIME::Type.new('text/vCard').content_type, 'text/vCard')
90
- assert_equal(MIME::Type.new('application/pkcs7-mime').content_type, 'application/pkcs7-mime')
91
- assert_equal(@zip.content_type, 'x-appl/x-zip');
117
+ assert_equal('text/plain', MIME::Type.new('text/plain').content_type)
118
+ assert_equal('image/jpeg', MIME::Type.new('image/jpeg').content_type)
119
+ assert_equal('application/x-msword',
120
+ MIME::Type.new('application/x-msword').content_type)
121
+ assert_equal('text/vCard', MIME::Type.new('text/vCard').content_type)
122
+ assert_equal('application/pkcs7-mime',
123
+ MIME::Type.new('application/pkcs7-mime').content_type)
124
+ assert_equal('x-appl/x-zip', @applzip.content_type);
125
+ assert_equal('base64', @applzip.encoding)
92
126
  end
93
127
 
94
128
  def test_encoding
95
- assert_equal(MIME::Type.new('text/plain').encoding, 'quoted-printable')
96
- assert_equal(MIME::Type.new('image/jpeg').encoding, 'base64')
97
- assert_equal(MIME::Type.new('application/x-msword').encoding, 'base64')
98
- assert_equal(MIME::Type.new('text/vCard').encoding, 'quoted-printable')
99
- assert_equal(MIME::Type.new('application/pkcs7-mime').encoding, 'base64')
100
-
101
- yaml = yaml_mime_type_from_array
102
- assert_equal(yaml.encoding, '8bit')
129
+ assert_equal('quoted-printable', MIME::Type.new('text/plain').encoding)
130
+ assert_equal('base64', MIME::Type.new('image/jpeg').encoding)
131
+ assert_equal('base64', MIME::Type.new('application/x-msword').encoding)
132
+ assert_equal('quoted-printable', MIME::Type.new('text/vCard').encoding)
133
+ assert_equal('base64', MIME::Type.new('application/pkcs7-mime').encoding)
134
+ end
135
+
136
+ def test_encoding_equals
137
+ yaml = make_yaml_mime_type
138
+ assert_equal('8bit', yaml.encoding)
103
139
  yaml.encoding = 'base64'
104
- assert_equal(yaml.encoding, 'base64')
140
+ assert_equal('base64', yaml.encoding)
105
141
  yaml.encoding = :default
106
- assert_equal(yaml.encoding, 'quoted-printable')
107
- assert_raises(ArgumentError) { yaml.encoding = 'binary' }
108
- assert_equal(@zip.encoding, 'base64')
142
+ assert_equal('quoted-printable', yaml.encoding)
143
+ begin
144
+ yaml.encoding = 'binary'
145
+ rescue MIME::Type::InvalidEncoding => ex
146
+ assert_equal('Invalid Encoding "binary" (valid values are [nil, :default, "base64", "8bit", "7bit", "quoted-printable"]).', ex.message)
147
+ end
109
148
  end
110
149
 
111
- def _test_default_encoding
112
- raise NotImplementedError, 'Need to write test_default_encoding'
150
+ def test_default_encoding
151
+ %w(text/plain text/html).each { |mt|
152
+ assert_equal('quoted-printable', MIME::Type.new(mt).default_encoding)
153
+ }
154
+ %w(image/jpeg applicatoin/pkcs7-mime).each { |mt|
155
+ assert_equal('base64', MIME::Type.new(mt).default_encoding)
156
+ }
113
157
  end
114
158
 
115
- def _test_docs
116
- raise NotImplementedError, 'Need to write test_docs'
159
+ def test_docs
160
+ yaml = make_yaml_mime_type_with_docs
161
+ assert_equal('Test YAML', yaml.docs)
117
162
  end
118
163
 
119
- def _test_docs_equals
120
- raise NotImplementedError, 'Need to write test_docs_equals'
164
+ def test_docs_equals
165
+ yaml = make_yaml_mime_type
166
+ assert_nil(yaml.docs)
167
+ yaml.docs = 'YAML docs'
168
+ assert_equal('YAML docs', yaml.docs)
121
169
  end
122
170
 
123
171
  def test_eql?
@@ -127,25 +175,30 @@ class TestMIMEType < Minitest::Test
127
175
  refute(MIME::Type.new('text/plain').eql?('image/jpeg'))
128
176
  end
129
177
 
130
- def _test_encoding
131
- raise NotImplementedError, 'Need to write test_encoding'
132
- end
133
-
134
- def _test_encoding_equals
135
- raise NotImplementedError, 'Need to write test_encoding_equals'
136
- end
137
-
138
178
  def test_extensions
139
- yaml = yaml_mime_type_from_array
140
- assert_equal(yaml.extensions, %w(yaml yml))
179
+ yaml = make_yaml_mime_type
180
+ assert_equal(%w(yaml yml), yaml.extensions)
181
+ assert_equal(2, @applzip.extensions.size)
182
+ assert_equal(%w(zip zp), @applzip.extensions)
183
+ end
184
+
185
+ def test_add_extensions
186
+ expected = make_yaml_mime_type
187
+ test_doc = make_yaml_mime_type
188
+ test_doc.add_extensions(nil)
189
+ assert_equal(expected.extensions, test_doc.extensions)
190
+ test_doc.add_extensions('yaml')
191
+ assert_equal(expected.extensions, test_doc.extensions)
192
+ test_doc.add_extensions(%w(yaml))
193
+ assert_equal(expected.extensions, test_doc.extensions)
194
+ test_doc.add_extensions('yz')
195
+ assert_equal(%w(yaml yml yz), test_doc.extensions)
196
+ end
197
+
198
+ def test_extensions_equals
199
+ yaml = make_yaml_mime_type
141
200
  yaml.extensions = 'yaml'
142
- assert_equal(yaml.extensions, ['yaml'])
143
- assert_equal(@zip.extensions.size, 2)
144
- assert_equal(@zip.extensions, ['zip', 'zp'])
145
- end
146
-
147
- def _test_extensions_equals
148
- raise NotImplementedError, 'Need to write test_extensions_equals'
201
+ assert_equal(%w(yaml), yaml.extensions)
149
202
  end
150
203
 
151
204
  def test_like_eh
@@ -158,50 +211,92 @@ class TestMIMEType < Minitest::Test
158
211
  end
159
212
 
160
213
  def test_media_type
161
- assert_equal(MIME::Type.new('text/plain').media_type, 'text')
162
- assert_equal(MIME::Type.new('image/jpeg').media_type, 'image')
163
- assert_equal(MIME::Type.new('application/x-msword').media_type, 'application')
164
- assert_equal(MIME::Type.new('text/vCard').media_type, 'text')
165
- assert_equal(MIME::Type.new('application/pkcs7-mime').media_type, 'application')
166
- assert_equal(MIME::Type.new('x-chemical/x-pdb').media_type, 'chemical')
167
- assert_equal(@zip.media_type, 'appl')
214
+ assert_equal('text', MIME::Type.new('text/plain').media_type)
215
+ assert_equal('image', MIME::Type.new('image/jpeg').media_type)
216
+ assert_equal('application', MIME::Type.new('application/x-msword').media_type)
217
+ assert_equal('text', MIME::Type.new('text/vCard').media_type)
218
+ assert_equal('application', MIME::Type.new('application/pkcs7-mime').media_type)
219
+ assert_equal('chemical', MIME::Type.new('x-chemical/x-pdb').media_type)
220
+ assert_equal('appl', @applzip.media_type)
168
221
  end
169
222
 
170
- def _test_obsolete_eh
171
- raise NotImplementedError, 'Need to write test_obsolete_eh'
223
+ def test_obsolete_eh
224
+ type = MIME::Type.new('content-type' => 'test/type',
225
+ 'obsolete' => true)
226
+ assert(type.obsolete?)
227
+ refute(make_yaml_mime_type.obsolete?)
172
228
  end
173
229
 
174
- def _test_obsolete_equals
175
- raise NotImplementedError, 'Need to write test_obsolete_equals'
230
+ def test_obsolete_equals
231
+ yaml = make_yaml_mime_type
232
+ refute(yaml.obsolete?)
233
+ yaml.obsolete = true
234
+ assert(yaml.obsolete?)
176
235
  end
177
236
 
178
237
  def test_platform_eh
179
- yaml = yaml_mime_type_from_array
180
- refute(yaml.platform?)
238
+ yaml = nil
239
+ assert_deprecated("MIME::Type#platform?") do
240
+ yaml = make_yaml_mime_type
241
+ refute(yaml.platform?)
242
+ end
181
243
  yaml.system = nil
182
244
  refute(yaml.platform?)
183
245
  yaml.system = %r{#{RUBY_PLATFORM}}
184
246
  assert(yaml.platform?)
185
247
  end
186
248
 
187
- def test_raw_media_type
188
- assert_equal(MIME::Type.new('text/plain').raw_media_type, 'text')
189
- assert_equal(MIME::Type.new('image/jpeg').raw_media_type, 'image')
190
- assert_equal(MIME::Type.new('application/x-msword').raw_media_type, 'application')
191
- assert_equal(MIME::Type.new('text/vCard').raw_media_type, 'text')
192
- assert_equal(MIME::Type.new('application/pkcs7-mime').raw_media_type, 'application')
249
+ def assert_priority(l, e, r)
250
+ assert_equal(-1, l.first.priority_compare(l.last))
251
+ assert_equal(0, e.first.priority_compare(e.last))
252
+ assert_equal(1, r.first.priority_compare(r.last))
253
+ end
254
+
255
+ def test_priority_compare
256
+ tl, te, tr = make('text/1'), make('text/1'), make('text/2')
257
+ assert_priority([tl, tr], [tl, te], [tr, tl])
258
+
259
+ tl.registered = te.registered = true
260
+ tr = make(tl) { |t| t.registered = false }
261
+ assert_priority([tl, tr], [tl, te], [tr, tl])
193
262
 
194
- assert_equal(MIME::Type.new('x-chemical/x-pdb').raw_media_type, 'x-chemical')
195
- assert_equal(@zip.raw_media_type, 'x-appl')
263
+ tl.system = te.system = nil
264
+ tr = make(tl) { |t| t.system = /#{RUBY_PLATFORM}/ }
265
+ assert_priority([tl, tr], [tl, te], [tr, tl])
266
+
267
+ tl.extensions = te.extensions = %w(1)
268
+ tr = make(tl) { |t| t.extensions = nil }
269
+ assert_priority([tl, tr], [tl, te], [tr, tl])
270
+
271
+ tl.obsolete = te.obsolete = false
272
+ tr = make(tl) { |t| t.obsolete = true }
273
+ assert_priority([tl, tr], [tl, te], [tr, tl])
274
+
275
+ tl.obsolete = te.obsolete = true
276
+ tl.use_instead = te.use_instead = 'abc/xyz'
277
+ tr = make(tl) { |t| t.use_instead = nil }
278
+ assert_priority([tl, tr], [tl, te], [tr, tl])
279
+ tr.use_instead = 'abc/zzz'
280
+ assert_priority([tl, tr], [tl, te], [tr, tl])
281
+ end
282
+
283
+ def test_raw_media_type
284
+ assert_equal('text', MIME::Type.new('text/plain').raw_media_type)
285
+ assert_equal('image', MIME::Type.new('image/jpeg').raw_media_type)
286
+ assert_equal('application', MIME::Type.new('application/x-msword').raw_media_type)
287
+ assert_equal('text', MIME::Type.new('text/vCard').raw_media_type)
288
+ assert_equal('application', MIME::Type.new('application/pkcs7-mime').raw_media_type)
289
+ assert_equal('x-chemical', MIME::Type.new('x-chemical/x-pdb').raw_media_type)
290
+ assert_equal('x-appl', @applzip.raw_media_type)
196
291
  end
197
292
 
198
293
  def test_raw_sub_type
199
- assert_equal(MIME::Type.new('text/plain').raw_sub_type, 'plain')
200
- assert_equal(MIME::Type.new('image/jpeg').raw_sub_type, 'jpeg')
201
- assert_equal(MIME::Type.new('application/x-msword').raw_sub_type, 'x-msword')
202
- assert_equal(MIME::Type.new('text/vCard').raw_sub_type, 'vCard')
203
- assert_equal(MIME::Type.new('application/pkcs7-mime').raw_sub_type, 'pkcs7-mime')
204
- assert_equal(@zip.raw_sub_type, 'x-zip')
294
+ assert_equal('plain', MIME::Type.new('text/plain').raw_sub_type)
295
+ assert_equal('jpeg', MIME::Type.new('image/jpeg').raw_sub_type)
296
+ assert_equal('x-msword', MIME::Type.new('application/x-msword').raw_sub_type)
297
+ assert_equal('vCard', MIME::Type.new('text/vCard').raw_sub_type)
298
+ assert_equal('pkcs7-mime', MIME::Type.new('application/pkcs7-mime').raw_sub_type)
299
+ assert_equal('x-zip', @applzip.raw_sub_type)
205
300
  end
206
301
 
207
302
  def test_registered_eh
@@ -210,107 +305,206 @@ class TestMIMEType < Minitest::Test
210
305
  refute(MIME::Type.new('application/x-msword').registered?)
211
306
  assert(MIME::Type.new('text/vCard').registered?)
212
307
  assert(MIME::Type.new('application/pkcs7-mime').registered?)
213
- refute(@zip.registered?)
308
+ refute(@applzip.registered?)
309
+ refute(MIME::Types['image/webp'].first.registered?)
310
+ # Temporarily broken: requires the new data format to be enabled.
311
+ assert(MIME::Types['application/x-www-form-urlencoded'].first.registered?)
214
312
  end
215
313
 
216
- def _test_registered_equals
217
- raise NotImplementedError, 'Need to write test_registered_equals'
314
+ def test_registered_equals
315
+ [ nil, false, true ].each { |v|
316
+ @applzip.registered = v
317
+ assert_equal(v, @applzip.instance_variable_get(:@registered))
318
+ }
319
+ @applzip.registered = 1
320
+ assert_equal(true, @applzip.instance_variable_get(:@registered))
218
321
  end
219
322
 
220
323
  def test_signature_eh
221
324
  refute(MIME::Type.new('text/plain').signature?)
222
325
  refute(MIME::Type.new('image/jpeg').signature?)
223
326
  refute(MIME::Type.new('application/x-msword').signature?)
224
- assert(MIME::Type.new('text/vCard').signature?)
225
- assert(MIME::Type.new('application/pkcs7-mime').signature?)
327
+ end
328
+
329
+ def test_signature_equals
330
+ sig = MIME::Type.new('text/vCard') { |t| t.signature = true }
331
+ assert(sig.signature?)
226
332
  end
227
333
 
228
334
  def test_simplified
229
- assert_equal(MIME::Type.new('text/plain').simplified, 'text/plain')
230
- assert_equal(MIME::Type.new('image/jpeg').simplified, 'image/jpeg')
231
- assert_equal(MIME::Type.new('application/x-msword').simplified, 'application/msword')
232
- assert_equal(MIME::Type.new('text/vCard').simplified, 'text/vcard')
233
- assert_equal(MIME::Type.new('application/pkcs7-mime').simplified, 'application/pkcs7-mime')
234
- assert_equal(MIME::Type.new('x-chemical/x-pdb').simplified, 'chemical/pdb')
335
+ assert_equal('text/plain', MIME::Type.new('text/plain').simplified)
336
+ assert_equal('image/jpeg', MIME::Type.new('image/jpeg').simplified)
337
+ assert_equal('application/msword', MIME::Type.new('application/x-msword').simplified)
338
+ assert_equal('text/vcard', MIME::Type.new('text/vCard').simplified)
339
+ assert_equal('application/pkcs7-mime', MIME::Type.new('application/pkcs7-mime').simplified)
340
+ assert_equal('chemical/pdb', MIME::Type.new('x-chemical/x-pdb').simplified)
235
341
  end
236
342
 
237
343
  def test_sub_type
238
- assert_equal(MIME::Type.new('text/plain').sub_type, 'plain')
239
- assert_equal(MIME::Type.new('image/jpeg').sub_type, 'jpeg')
240
- assert_equal(MIME::Type.new('application/x-msword').sub_type, 'msword')
241
- assert_equal(MIME::Type.new('text/vCard').sub_type, 'vcard')
242
- assert_equal(MIME::Type.new('application/pkcs7-mime').sub_type, 'pkcs7-mime')
243
- assert_equal(@zip.sub_type, 'zip')
344
+ assert_equal('plain', MIME::Type.new('text/plain').sub_type)
345
+ assert_equal('jpeg', MIME::Type.new('image/jpeg').sub_type)
346
+ assert_equal('msword', MIME::Type.new('application/x-msword').sub_type)
347
+ assert_equal('vcard', MIME::Type.new('text/vCard').sub_type)
348
+ assert_equal('pkcs7-mime', MIME::Type.new('application/pkcs7-mime').sub_type)
349
+ assert_equal('zip', @applzip.sub_type)
350
+ end
351
+
352
+ def test_system
353
+ assert_deprecated("MIME::Type#system") do
354
+ yaml = make_yaml_mime_type
355
+ assert_equal(%r{d9d172f608}, yaml.system)
356
+ end
244
357
  end
245
358
 
246
359
  def test_system_equals
247
- yaml = yaml_mime_type_from_array
248
- assert_equal(yaml.system, %r{d9d172f608})
360
+ yaml = make_yaml_mime_type
249
361
  yaml.system = /win32/
250
- assert_equal(yaml.system, %r{win32})
362
+ assert_equal(%r{win32}, yaml.system)
251
363
  yaml.system = nil
252
364
  assert_nil(yaml.system)
253
365
  end
254
366
 
255
367
  def test_system_eh
256
- yaml = yaml_mime_type_from_array
257
- assert(yaml.system?)
368
+ yaml = make_yaml_mime_type
369
+ assert_deprecated("MIME::Type#system?") do
370
+ assert(yaml.system?)
371
+ end
258
372
  yaml.system = nil
259
373
  refute(yaml.system?)
260
374
  end
261
375
 
262
376
  def test_to_a
263
- yaml = yaml_mime_type_from_array
264
- assert_equal(yaml.to_a, ['text/x-yaml', %w(yaml yml), '8bit',
265
- /d9d172f608/, nil, nil, nil, false])
377
+ yaml = make_yaml_mime_type
378
+ assert_deprecated("MIME::Type#to_a") do
379
+ assert_equal(['text/x-yaml', %w(yaml yml), '8bit', /d9d172f608/,
380
+ false, nil, [], false], yaml.to_a)
381
+ end
266
382
  end
267
383
 
268
384
  def test_to_hash
269
- yaml = yaml_mime_type_from_array
270
- assert_equal(yaml.to_hash,
271
- { 'Content-Type' => 'text/x-yaml',
385
+ yaml = make_yaml_mime_type
386
+ assert_deprecated("MIME::Type#to_hash") do
387
+ assert_equal({'Content-Type' => 'text/x-yaml',
272
388
  'Content-Transfer-Encoding' => '8bit',
273
- 'Extensions' => %w(yaml yml),
274
- 'System' => /d9d172f608/,
275
- 'Registered' => false,
276
- 'URL' => nil,
277
- 'Obsolete' => nil,
278
- 'Docs' => nil })
389
+ 'Extensions' => %w(yaml yml),
390
+ 'System' => /d9d172f608/,
391
+ 'Registered' => false,
392
+ 'URL' => [],
393
+ 'Obsolete' => false,
394
+ 'Docs' => nil },
395
+ yaml.to_hash)
396
+ end
397
+ end
398
+
399
+ def assert_type_has_keys(type, *keys)
400
+ hash = type.to_h
401
+ keys.flatten.each { |key| assert(hash.has_key?(key)) }
402
+ end
403
+
404
+ def test_to_h
405
+ t = make('a/b')
406
+ assert_type_has_keys(t, %w(content-type registered encoding))
407
+ assert_type_has_keys(make(t) { |v| v.docs = 'Something' }, 'docs')
408
+ assert_type_has_keys(make(t) { |v| v.extensions = %w(b) }, 'extensions')
409
+ assert_type_has_keys(make(t) { |v| v.obsolete = true }, 'obsolete')
410
+ assert_type_has_keys(make(t) { |v| v.obsolete = true; v.use_instead = 'c/d' },
411
+ 'obsolete', 'use-instead')
412
+ assert_type_has_keys(make(t) { |v| v.references = 'IANA' }, 'references')
413
+ assert_type_has_keys(make(t) { |v| v.signature = true }, 'signature')
414
+ assert_type_has_keys(make(t) { |v| v.system = /xyz/ }, 'system')
415
+ end
416
+
417
+ def test_to_json
418
+ assert_equal('{"content-type":"a/b","encoding":"base64","registered":true}',
419
+ make('a/b').to_json)
279
420
  end
280
421
 
281
422
  def test_to_s
282
- assert_equal("#{MIME::Type.new('text/plain')}", 'text/plain')
423
+ assert_equal('text/plain', "#{MIME::Type.new('text/plain')}")
283
424
  end
284
425
 
285
426
  def test_class_constructors
286
- refute_nil(@zip)
287
- yaml = MIME::Type.new('text/x-yaml') do |y|
288
- y.extensions = %w(yaml yml)
289
- y.encoding = '8bit'
290
- y.system = 'd9d172f608'
427
+ assert_instance_of(MIME::Type, MIME::Type.new('text/x-yaml'))
428
+ assert_instance_of(MIME::Type, MIME::Type.new('text/x-yaml') { |y|
429
+ assert_instance_of(MIME::Type, y)
430
+ })
431
+ assert_instance_of(MIME::Type, MIME::Type.new('content-type' => 'text/x-yaml'))
432
+ assert_instance_of(MIME::Type, MIME::Type.new(['text/x-yaml', %w(yaml)]))
433
+ assert_raises(MIME::Type::InvalidContentType) { MIME::Type.new('apps') }
434
+ begin
435
+ MIME::Type.new(nil)
436
+ rescue MIME::Type::InvalidContentType => ex
437
+ assert_equal("Invalid Content-Type nil", ex.message)
291
438
  end
292
- assert_instance_of(MIME::Type, yaml)
293
- assert_raises(MIME::InvalidContentType) { MIME::Type.new('apps') }
294
- assert_raises(MIME::InvalidContentType) { MIME::Type.new(nil) }
295
439
  end
296
440
 
297
- def _test_to_str
298
- raise NotImplementedError, 'Need to write test_to_str'
441
+ def test_to_str
442
+ assert_equal('stringy', 'text/plain'.sub(MIME::Type.new('text/plain'), 'stringy'))
299
443
  end
300
444
 
301
- def _test_url
302
- raise NotImplementedError, 'Need to write test_url'
445
+ def test_references
446
+ assert_empty(make_yaml_mime_type.references)
303
447
  end
304
448
 
305
- def _test_url_equals
306
- raise NotImplementedError, 'Need to write test_url_equals'
449
+ def test_references_equals
450
+ yaml = make_yaml_mime_type
451
+ yaml.references = "IANA"
452
+ assert_equal(%W(IANA), yaml.references)
307
453
  end
308
454
 
309
- def _test_urls
310
- raise NotImplementedError, 'Need to write test_urls'
455
+ def test_url
456
+ assert_deprecated("MIME::Type#url", "and has been renamed to #references") do
457
+ assert_empty(make_yaml_mime_type.url)
458
+ end
311
459
  end
312
460
 
313
- def __test_use_instead
314
- raise NotImplementedError, 'Need to write test_use_instead'
461
+ def test_url_equals
462
+ yaml = make_yaml_mime_type
463
+ assert_deprecated("MIME::Type#url=", "and has been renamed to #references=") do
464
+ yaml.url = "IANA"
465
+ end
466
+ assert_equal(%W(IANA), yaml.url)
467
+ end
468
+
469
+ def test_urls
470
+ yaml = make_yaml_mime_type
471
+ assert_empty(yaml.urls)
472
+ yaml.references = %w(IANA RFC123 DRAFT:xyz LTSW [abc])
473
+ assert_equal(%w(http://www.iana.org/assignments/media-types/text/yaml
474
+ http://rfc-editor.org/rfc/rfc123.txt
475
+ http://datatracker.ietf.org/public/idindex.cgi?command=id_details&filename=xyz
476
+ http://www.ltsw.se/knbase/internet/text.htp
477
+ http://www.iana.org/assignments/contact-people.htm#abc),
478
+ yaml.urls)
479
+ yaml.references = '[def=lax]'
480
+ assert_equal([%w(def http://www.iana.org/assignments/contact-people.htm#lax)],
481
+ yaml.urls)
482
+ yaml.references = '{mno=pqr}'
483
+ assert_equal([%w(mno pqr)], yaml.urls)
484
+ yaml.references = 'hoge'
485
+ assert_equal(%w(hoge), yaml.urls)
486
+ end
487
+
488
+ def test_use_instead
489
+ t = make('t/1') { |v| v.use_instead = 't/2' }
490
+ assert_nil(t.use_instead)
491
+ t.obsolete = true
492
+ assert_equal('t/2', t.use_instead)
493
+ end
494
+
495
+ def test_use_instead_equals
496
+ t = make('t/1') { |v| v.obsolete = true }
497
+ assert_nil(t.use_instead)
498
+ t.use_instead = 't/2'
499
+ assert_equal('t/2', t.use_instead)
500
+ end
501
+
502
+ def test_deprecated_constant
503
+ assert_output(nil, /MIME::InvalidContentType/) do
504
+ assert_same(MIME::InvalidContentType, MIME::Type::InvalidContentType)
505
+ end
506
+ assert_silent do
507
+ assert_same(MIME::InvalidContentType, MIME::Type::InvalidContentType)
508
+ end
315
509
  end
316
510
  end