mime-types 2.99.3 → 3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.autotest +35 -0
- data/.gemtest +0 -0
- data/.gitignore +17 -0
- data/.hoerc +20 -0
- data/Code-of-Conduct.rdoc +27 -60
- data/Contributing.rdoc +0 -1
- data/History.rdoc +75 -36
- data/Licence.rdoc +2 -16
- data/Manifest.txt +10 -18
- data/README.rdoc +46 -46
- data/Rakefile +112 -58
- data/lib/mime-types.rb +0 -2
- data/lib/mime/type.rb +183 -415
- data/lib/mime/type/columnar.rb +27 -62
- data/lib/mime/types.rb +37 -135
- data/lib/mime/types/cache.rb +49 -73
- data/lib/mime/types/columnar.rb +42 -48
- data/lib/mime/types/container.rb +30 -0
- data/lib/mime/types/deprecations.rb +1 -22
- data/lib/mime/types/full.rb +17 -0
- data/lib/mime/types/loader.rb +10 -137
- data/lib/mime/types/logger.rb +2 -0
- data/lib/mime/types/registry.rb +81 -0
- data/support/benchmarks/load.rb +27 -26
- data/support/benchmarks/load_allocations.rb +14 -7
- data/support/benchmarks/object_counts.rb +6 -4
- data/support/profile/columnar.rb +5 -0
- data/support/profile/columnar_full.rb +5 -0
- data/support/profile/full.rb +5 -0
- data/test/minitest_helper.rb +3 -12
- data/test/test_mime_type.rb +461 -454
- data/test/test_mime_types.rb +126 -86
- data/test/test_mime_types_cache.rb +55 -45
- data/test/test_mime_types_class.rb +113 -97
- data/test/test_mime_types_lazy.rb +19 -23
- data/test/test_mime_types_loader.rb +5 -32
- metadata +67 -44
- data/History-Types.rdoc +0 -454
- data/data/mime-types.json +0 -1
- data/data/mime.content_type.column +0 -1980
- data/data/mime.docs.column +0 -1980
- data/data/mime.encoding.column +0 -1980
- data/data/mime.friendly.column +0 -1980
- data/data/mime.obsolete.column +0 -1980
- data/data/mime.registered.column +0 -1980
- data/data/mime.signature.column +0 -1980
- data/data/mime.use_instead.column +0 -1980
- data/data/mime.xrefs.column +0 -1980
- data/docs/COPYING.txt +0 -339
- data/docs/artistic.txt +0 -127
- data/lib/mime/types/loader_path.rb +0 -15
- data/support/apache_mime_types.rb +0 -108
- data/support/convert.rb +0 -158
- data/support/convert/columnar.rb +0 -88
- data/support/iana_registry.rb +0 -172
data/lib/mime/types/logger.rb
CHANGED
@@ -0,0 +1,81 @@
|
|
1
|
+
class << MIME::Types
|
2
|
+
include Enumerable
|
3
|
+
|
4
|
+
##
|
5
|
+
def new(*) # :nodoc:
|
6
|
+
super.tap do |types|
|
7
|
+
__instances__.add types
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# MIME::Types#[] against the default MIME::Types registry.
|
12
|
+
def [](type_id, complete: false, registered: false)
|
13
|
+
__types__[type_id, complete: complete, registered: registered]
|
14
|
+
end
|
15
|
+
|
16
|
+
# MIME::Types#count against the default MIME::Types registry.
|
17
|
+
def count
|
18
|
+
__types__.count
|
19
|
+
end
|
20
|
+
|
21
|
+
# MIME::Types#each against the default MIME::Types registry.
|
22
|
+
def each
|
23
|
+
if block_given?
|
24
|
+
__types__.each { |t| yield t }
|
25
|
+
else
|
26
|
+
enum_for(:each)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# MIME::Types#type_for against the default MIME::Types registry.
|
31
|
+
def type_for(filename)
|
32
|
+
__types__.type_for(filename)
|
33
|
+
end
|
34
|
+
alias_method :of, :type_for
|
35
|
+
|
36
|
+
# MIME::Types#add against the default MIME::Types registry.
|
37
|
+
def add(*types)
|
38
|
+
__types__.add(*types)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def lazy_load?
|
44
|
+
(lazy = ENV['RUBY_MIME_TYPES_LAZY_LOAD']) && (lazy != 'false')
|
45
|
+
end
|
46
|
+
|
47
|
+
def __types__
|
48
|
+
(defined?(@__types__) and @__types__) or load_default_mime_types
|
49
|
+
end
|
50
|
+
|
51
|
+
unless private_method_defined?(:load_mode)
|
52
|
+
def load_mode
|
53
|
+
{ columnar: true }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def load_default_mime_types(mode = load_mode)
|
58
|
+
@__types__ = MIME::Types::Cache.load
|
59
|
+
unless @__types__
|
60
|
+
@__types__ = MIME::Types::Loader.load(mode)
|
61
|
+
MIME::Types::Cache.save(@__types__)
|
62
|
+
end
|
63
|
+
@__types__
|
64
|
+
end
|
65
|
+
|
66
|
+
def __instances__
|
67
|
+
@__instances__ ||= Set.new
|
68
|
+
end
|
69
|
+
|
70
|
+
def reindex_extensions(type)
|
71
|
+
__instances__.each do |instance|
|
72
|
+
instance.send(:reindex_extensions!, type)
|
73
|
+
end
|
74
|
+
true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
##
|
79
|
+
class MIME::Types
|
80
|
+
load_default_mime_types(load_mode) unless lazy_load?
|
81
|
+
end
|
data/support/benchmarks/load.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# -*- ruby encoding: utf-8 -*-
|
2
2
|
|
3
3
|
require 'benchmark'
|
4
|
+
require 'mime/types'
|
4
5
|
|
5
6
|
module Benchmarks
|
6
7
|
class Load
|
@@ -11,47 +12,47 @@ module Benchmarks
|
|
11
12
|
def initialize(load_path, repeats = nil)
|
12
13
|
@cache_file = File.expand_path('../cache.mtc', __FILE__)
|
13
14
|
@repeats = repeats.to_i
|
14
|
-
@repeats = 50 if repeats <= 0
|
15
|
+
@repeats = 50 if @repeats <= 0
|
15
16
|
@load_path = load_path
|
16
17
|
end
|
17
18
|
|
18
|
-
def reload_mime_types(repeats = 1,
|
19
|
-
|
20
|
-
columnar = options.fetch(:columnar, false)
|
19
|
+
def reload_mime_types(repeats = 1, force: false, columnar: false, cache: false)
|
20
|
+
loader = MIME::Types::Loader.new
|
21
21
|
|
22
22
|
repeats.times {
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
require 'mime/types/columnar'
|
28
|
-
else
|
29
|
-
require 'mime/types'
|
23
|
+
types = MIME::Types::Cache.load if cache
|
24
|
+
unless types
|
25
|
+
types = loader.load(columnar: columnar)
|
26
|
+
MIME::Types::Cache.save(types) if cache
|
30
27
|
end
|
31
|
-
|
28
|
+
types.first.to_h if force
|
32
29
|
}
|
33
30
|
end
|
34
31
|
|
35
32
|
def report
|
36
33
|
remove_cache
|
37
34
|
|
38
|
-
Benchmark.bm(
|
39
|
-
mark.report('Normal
|
40
|
-
mark.report('Columnar
|
41
|
-
|
42
|
-
|
43
|
-
mark.report('
|
44
|
-
|
45
|
-
|
46
|
-
ENV.delete('RUBY_MIME_TYPES_LAZY_LOAD')
|
35
|
+
Benchmark.bm(30) do |mark|
|
36
|
+
mark.report('Normal') { reload_mime_types(@repeats) }
|
37
|
+
mark.report('Columnar') {
|
38
|
+
reload_mime_types(@repeats, columnar: true)
|
39
|
+
}
|
40
|
+
mark.report('Columnar Full') {
|
41
|
+
reload_mime_types(@repeats, columnar: true, force: true)
|
42
|
+
}
|
47
43
|
|
48
44
|
ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
|
49
|
-
reload_mime_types
|
45
|
+
mark.report('Cache Initialize') { reload_mime_types(cache: true) }
|
46
|
+
mark.report('Cached') { reload_mime_types(@repeats, cache: true) }
|
50
47
|
|
51
|
-
|
52
|
-
ENV['
|
53
|
-
mark.report('
|
54
|
-
|
48
|
+
remove_cache
|
49
|
+
ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
|
50
|
+
mark.report('Columnar Cache Initialize') {
|
51
|
+
reload_mime_types(columnar: true, cache: true)
|
52
|
+
}
|
53
|
+
mark.report('Columnar Cached') {
|
54
|
+
reload_mime_types(@repeats, columnar: true, cache: true)
|
55
|
+
}
|
55
56
|
end
|
56
57
|
ensure
|
57
58
|
remove_cache
|
@@ -14,13 +14,18 @@ end
|
|
14
14
|
|
15
15
|
module Benchmarks
|
16
16
|
class LoadAllocations
|
17
|
-
def self.report(columnar: false, top_x: nil, mime_types_only: false)
|
18
|
-
new(
|
19
|
-
|
17
|
+
def self.report(columnar: false, full: false, top_x: nil, mime_types_only: false)
|
18
|
+
new(
|
19
|
+
columnar: columnar,
|
20
|
+
top_x: top_x,
|
21
|
+
mime_types_only: mime_types_only,
|
22
|
+
full: full
|
23
|
+
).report
|
20
24
|
end
|
21
25
|
|
22
|
-
def initialize(columnar: false, top_x: nil, mime_types_only: false)
|
23
|
-
@columnar = columnar
|
26
|
+
def initialize(columnar: false, full: false, top_x: nil, mime_types_only: false)
|
27
|
+
@columnar = !!columnar
|
28
|
+
@full = !!full
|
24
29
|
@mime_types_only = !!mime_types_only
|
25
30
|
|
26
31
|
@top_x = top_x
|
@@ -68,11 +73,13 @@ module Benchmarks
|
|
68
73
|
def collect
|
69
74
|
if @columnar
|
70
75
|
@allocations = ObjectSpace::AllocationTracer.trace do
|
71
|
-
require 'mime/types
|
76
|
+
require 'mime/types'
|
77
|
+
|
78
|
+
MIME::Types.first.to_h if @full
|
72
79
|
end
|
73
80
|
else
|
74
81
|
@allocations = ObjectSpace::AllocationTracer.trace do
|
75
|
-
require 'mime/types'
|
82
|
+
require 'mime/types/full'
|
76
83
|
end
|
77
84
|
end
|
78
85
|
|
@@ -2,12 +2,13 @@
|
|
2
2
|
|
3
3
|
module Benchmarks
|
4
4
|
class ObjectCounts
|
5
|
-
def self.report(columnar: false)
|
5
|
+
def self.report(columnar: false, full: false)
|
6
6
|
new(columnar: columnar).report
|
7
7
|
end
|
8
8
|
|
9
|
-
def initialize(columnar: false)
|
9
|
+
def initialize(columnar: false, full: false)
|
10
10
|
@columnar = columnar
|
11
|
+
@full = full
|
11
12
|
end
|
12
13
|
|
13
14
|
def report
|
@@ -25,9 +26,10 @@ module Benchmarks
|
|
25
26
|
@before = count_objects
|
26
27
|
|
27
28
|
if @columnar
|
28
|
-
require 'mime/types/columnar'
|
29
|
-
else
|
30
29
|
require 'mime/types'
|
30
|
+
MIME::Types.first.to_h if @full
|
31
|
+
else
|
32
|
+
require 'mime/types/full'
|
31
33
|
end
|
32
34
|
|
33
35
|
@after = count_objects
|
data/test/minitest_helper.rb
CHANGED
@@ -6,16 +6,7 @@ require 'fileutils'
|
|
6
6
|
gem 'minitest'
|
7
7
|
require 'fivemat/minitest/autorun'
|
8
8
|
require 'minitest/focus'
|
9
|
+
require 'minitest/rg'
|
10
|
+
require 'minitest-bonus-assertions'
|
9
11
|
|
10
|
-
|
11
|
-
def assert_deprecated name, message = 'and will be removed'
|
12
|
-
name = Regexp.escape(name)
|
13
|
-
message = Regexp.escape(message)
|
14
|
-
|
15
|
-
assert_output nil, /#{name} is deprecated #{message}./ do
|
16
|
-
yield
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
Minitest::Test.send(:include, self)
|
21
|
-
end
|
12
|
+
ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes'
|
data/test/test_mime_type.rb
CHANGED
@@ -3,13 +3,21 @@
|
|
3
3
|
require 'mime/types'
|
4
4
|
require 'minitest_helper'
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
describe MIME::Type do
|
7
|
+
# it { fail }
|
8
|
+
|
9
|
+
def mime_type(content_type)
|
8
10
|
MIME::Type.new(content_type) { |mt| yield mt if block_given? }
|
9
11
|
end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
+
let(:x_appl_x_zip) {
|
14
|
+
mime_type('x-appl/x-zip') { |t| t.extensions = %w(zip zp) }
|
15
|
+
}
|
16
|
+
let(:text_plain) { mime_type('text/plain') }
|
17
|
+
let(:text_html) { mime_type('text/html') }
|
18
|
+
let(:image_jpeg) { mime_type('image/jpeg') }
|
19
|
+
let(:application_javascript) {
|
20
|
+
mime_type('application/javascript') do |js|
|
13
21
|
js.friendly('en' => 'JavaScript')
|
14
22
|
js.xrefs = {
|
15
23
|
'rfc' => %w(rfc4239 rfc4239),
|
@@ -17,580 +25,579 @@ class TestMIMEType < Minitest::Test
|
|
17
25
|
}
|
18
26
|
js.encoding = '8bit'
|
19
27
|
js.extensions = %w(js sj)
|
20
|
-
assert_deprecated('MIME::Type#references=') do
|
21
|
-
js.references = :anything
|
22
|
-
end
|
23
28
|
js.registered = true
|
24
|
-
|
25
|
-
yield js if block_given?
|
26
29
|
end
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
make('text/x-yaml') do |yaml|
|
30
|
+
}
|
31
|
+
let(:text_x_yaml) {
|
32
|
+
mime_type('text/x-yaml') do |yaml|
|
31
33
|
yaml.extensions = %w(yaml yml)
|
32
34
|
yaml.encoding = '8bit'
|
33
|
-
assert_deprecated('MIME::Type#system=') do
|
34
|
-
yaml.system = 'd9d172f608'
|
35
|
-
end
|
36
35
|
yaml.friendly('en' => 'YAML Structured Document')
|
37
36
|
end
|
38
|
-
|
37
|
+
}
|
38
|
+
let(:text_x_yaml_with_docs) {
|
39
|
+
text_x_yaml.dup.tap do |yaml|
|
40
|
+
yaml.docs = 'Test YAML'
|
41
|
+
end
|
42
|
+
}
|
39
43
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
describe '.simplified' do
|
45
|
+
it 'leaves normal types alone' do
|
46
|
+
assert_equal 'text/plain', MIME::Type.simplified('text/plain')
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'does not remove x- prefixes by default' do
|
50
|
+
assert_equal 'application/x-msword',
|
51
|
+
MIME::Type.simplified('application/x-msword')
|
52
|
+
assert_equal 'x-xyz/abc', MIME::Type.simplified('x-xyz/abc')
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'removes x- prefixes when requested' do
|
56
|
+
assert_equal 'application/msword',
|
57
|
+
MIME::Type.simplified('application/x-msword', remove_x_prefix: true)
|
58
|
+
assert_equal 'xyz/abc',
|
59
|
+
MIME::Type.simplified('x-xyz/abc', remove_x_prefix: true)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'lowercases mixed-case types' do
|
63
|
+
assert_equal 'text/vcard', MIME::Type.simplified('text/vCard')
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'returns nil when the value provided is not a valid content type' do
|
67
|
+
assert_nil MIME::Type.simplified('text')
|
48
68
|
end
|
49
69
|
end
|
50
70
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
71
|
+
describe '.i18n_key' do
|
72
|
+
it 'converts text/plain to text.plain' do
|
73
|
+
assert_equal 'text.plain', MIME::Type.i18n_key('text/plain')
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'does not remove x-prefixes' do
|
77
|
+
assert_equal 'application.x-msword',
|
78
|
+
MIME::Type.i18n_key('application/x-msword')
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'converts text/vCard to text.vcard' do
|
82
|
+
assert_equal 'text.vcard', MIME::Type.i18n_key('text/vCard')
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'returns nil when the value provided is not a valid content type' do
|
86
|
+
assert_nil MIME::Type.i18n_key('text')
|
87
|
+
end
|
55
88
|
end
|
56
89
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
90
|
+
describe '.new' do
|
91
|
+
it 'fails if an invalid content type is provided' do
|
92
|
+
exception = assert_raises MIME::Type::InvalidContentType do
|
93
|
+
MIME::Type.new('apps')
|
94
|
+
end
|
95
|
+
assert_equal 'Invalid Content-Type "apps"', exception.to_s
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'creates a valid content type just from a string' do
|
99
|
+
type = MIME::Type.new('text/x-yaml')
|
100
|
+
|
101
|
+
assert_instance_of MIME::Type, type
|
102
|
+
assert_equal 'text/x-yaml', type.content_type
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'yields the content type in a block' do
|
106
|
+
MIME::Type.new('text/x-yaml') do |type|
|
107
|
+
assert_instance_of MIME::Type, type
|
108
|
+
assert_equal 'text/x-yaml', type.content_type
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'creates a valid content type from a hash' do
|
113
|
+
type = MIME::Type.new(
|
114
|
+
'content-type' => 'text/x-yaml',
|
115
|
+
'obsolete' => true
|
65
116
|
)
|
117
|
+
assert_instance_of MIME::Type, type
|
118
|
+
assert_equal 'text/x-yaml', type.content_type
|
119
|
+
assert type.obsolete?
|
66
120
|
end
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
121
|
+
|
122
|
+
it 'creates a valid content type from an array' do
|
123
|
+
type = MIME::Type.new(%w(text/x-yaml yaml yml yz))
|
124
|
+
assert_instance_of MIME::Type, type
|
125
|
+
assert_equal 'text/x-yaml', type.content_type
|
126
|
+
assert_equal %w(yaml yml yz), type.extensions
|
71
127
|
end
|
72
128
|
end
|
73
129
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
'Content-Transfer-Encoding' => '8bit',
|
79
|
-
'System' => 'd9d172f608',
|
80
|
-
'Extensions' => %w(yaml yml))
|
130
|
+
describe '#like?' do
|
131
|
+
it 'compares two MIME::Types on #simplified values without x- prefixes' do
|
132
|
+
assert text_plain.like?(text_plain)
|
133
|
+
refute text_plain.like?(text_html)
|
81
134
|
end
|
82
|
-
assert_instance_of(MIME::Type, yaml)
|
83
|
-
assert_equal('text/yaml', yaml.simplified)
|
84
|
-
end
|
85
135
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
zip2 = MIME::Type.from_mime_type(@applzip)
|
136
|
+
it 'compares MIME::Type against string without x- prefixes' do
|
137
|
+
assert text_plain.like?(text_plain.to_s)
|
138
|
+
refute text_plain.like?(text_html.to_s)
|
90
139
|
end
|
91
|
-
assert_instance_of(MIME::Type, @applzip)
|
92
|
-
assert_equal('appl/zip', @applzip.simplified)
|
93
|
-
refute_equal(@applzip.object_id, zip2.object_id)
|
94
140
|
end
|
95
141
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
assert_equal('text/vcard', MIME::Type.simplified('text/vCard'))
|
101
|
-
assert_equal('application/pkcs7-mime', MIME::Type.simplified('application/pkcs7-mime'))
|
102
|
-
assert_equal('xyz/abc', MIME::Type.simplified('x-xyz/abc'))
|
103
|
-
assert_nil(MIME::Type.simplified('text'))
|
104
|
-
end
|
142
|
+
describe '#<=>' do
|
143
|
+
it 'correctly compares identical types' do
|
144
|
+
assert_equal text_plain, text_plain
|
145
|
+
end
|
105
146
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
assert_equal('application.pkcs7-mime', MIME::Type.i18n_key('application/pkcs7-mime'))
|
112
|
-
assert_equal('xyz.abc', MIME::Type.i18n_key('x-xyz/abc'))
|
113
|
-
assert_nil(MIME::Type.i18n_key('text'))
|
114
|
-
end
|
147
|
+
it 'correctly compares equivalent types' do
|
148
|
+
right = mime_type('text/Plain')
|
149
|
+
refute_same text_plain, right
|
150
|
+
assert_equal text_plain, right
|
151
|
+
end
|
115
152
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
assert(MIME::Type.new('text/plain') != 'image/jpeg')
|
121
|
-
assert(MIME::Type.new('text/plain') > MIME::Type.new('text/html'))
|
122
|
-
assert(MIME::Type.new('text/plain') > 'text/html')
|
123
|
-
assert(MIME::Type.new('text/html') < MIME::Type.new('text/plain'))
|
124
|
-
assert(MIME::Type.new('text/html') < 'text/plain')
|
125
|
-
assert('text/html' == MIME::Type.new('text/html'))
|
126
|
-
assert('text/html' < MIME::Type.new('text/plain'))
|
127
|
-
assert('text/plain' > MIME::Type.new('text/html'))
|
128
|
-
end
|
153
|
+
it 'correctly compares types that sort earlier' do
|
154
|
+
refute_equal text_html, text_plain
|
155
|
+
assert_operator text_html, :<, text_plain
|
156
|
+
end
|
129
157
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
assert(MIME::Type.new('text/vCard').ascii?)
|
135
|
-
refute(MIME::Type.new('application/pkcs7-mime').ascii?)
|
136
|
-
refute(@applzip.ascii?)
|
137
|
-
end
|
158
|
+
it 'correctly compares types that sort later' do
|
159
|
+
refute_equal text_plain, text_html
|
160
|
+
assert_operator text_plain, :>, text_html
|
161
|
+
end
|
138
162
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
assert(MIME::Type.new('application/x-msword').binary?)
|
143
|
-
refute(MIME::Type.new('text/vCard').binary?)
|
144
|
-
assert(MIME::Type.new('application/pkcs7-mime').binary?)
|
145
|
-
assert(@applzip.binary?)
|
146
|
-
end
|
163
|
+
it 'correctly compares types against equivalent strings' do
|
164
|
+
assert_equal text_plain, 'text/plain'
|
165
|
+
end
|
147
166
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
refute(yaml.complete?)
|
153
|
-
end
|
167
|
+
it 'correctly compares types against strings that sort earlier' do
|
168
|
+
refute_equal text_html, 'text/plain'
|
169
|
+
assert_operator text_html, :<, 'text/plain'
|
170
|
+
end
|
154
171
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
MIME::Type.new('application/x-msword').content_type)
|
160
|
-
assert_equal('text/vCard', MIME::Type.new('text/vCard').content_type)
|
161
|
-
assert_equal('application/pkcs7-mime',
|
162
|
-
MIME::Type.new('application/pkcs7-mime').content_type)
|
163
|
-
assert_equal('x-appl/x-zip', @applzip.content_type)
|
164
|
-
assert_equal('base64', @applzip.encoding)
|
165
|
-
end
|
172
|
+
it 'correctly compares types against strings that sort later' do
|
173
|
+
refute_equal text_plain, 'text/html'
|
174
|
+
assert_operator text_plain, :>, 'text/html'
|
175
|
+
end
|
166
176
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
assert_equal('quoted-printable', MIME::Type.new('text/vCard').encoding)
|
172
|
-
assert_equal('base64', MIME::Type.new('application/pkcs7-mime').encoding)
|
177
|
+
it 'correctly compares against nil' do
|
178
|
+
refute_equal text_html, nil
|
179
|
+
assert_operator text_plain, :<, nil
|
180
|
+
end
|
173
181
|
end
|
174
182
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
assert_equal('base64', yaml.encoding)
|
180
|
-
yaml.encoding = :default
|
181
|
-
assert_equal('quoted-printable', yaml.encoding)
|
182
|
-
assert_raises(MIME::Type::InvalidEncoding) {
|
183
|
-
yaml.encoding = 'binary'
|
184
|
-
}
|
185
|
-
end
|
183
|
+
describe '#ascii?' do
|
184
|
+
it 'defaults to true for text/* types' do
|
185
|
+
assert text_plain.ascii?
|
186
|
+
end
|
186
187
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
}
|
191
|
-
%w(image/jpeg applicatoin/pkcs7-mime).each { |mt|
|
192
|
-
assert_equal('base64', MIME::Type.new(mt).default_encoding)
|
193
|
-
}
|
188
|
+
it 'defaults to false for non-text/* types' do
|
189
|
+
refute image_jpeg.ascii?
|
190
|
+
end
|
194
191
|
end
|
195
192
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
193
|
+
describe '#binary?' do
|
194
|
+
it 'defaults to false for text/* types' do
|
195
|
+
refute text_plain.binary?
|
196
|
+
end
|
200
197
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
yaml.docs = 'YAML docs'
|
205
|
-
assert_equal('YAML docs', yaml.docs)
|
198
|
+
it 'defaults to true for non-text/* types' do
|
199
|
+
assert image_jpeg.binary?
|
200
|
+
end
|
206
201
|
end
|
207
202
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
203
|
+
describe '#complete?' do
|
204
|
+
it 'is true when there are extensions' do
|
205
|
+
assert text_x_yaml.complete?
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'is false when there are no extensions' do
|
209
|
+
refute mime_type('text/plain').complete?
|
210
|
+
end
|
213
211
|
end
|
214
212
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
213
|
+
describe '#content_type' do
|
214
|
+
it 'preserves the original case' do
|
215
|
+
assert_equal 'text/plain', text_plain.content_type
|
216
|
+
assert_equal 'text/vCard', mime_type('text/vCard').content_type
|
217
|
+
end
|
218
|
+
|
219
|
+
it 'does not remove x- prefixes' do
|
220
|
+
assert_equal 'x-appl/x-zip', x_appl_x_zip.content_type
|
221
|
+
end
|
220
222
|
end
|
221
223
|
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
assert_equal(expected.extensions, test_doc.extensions)
|
231
|
-
test_doc.add_extensions('yz')
|
232
|
-
assert_equal(%w(yaml yml yz), test_doc.extensions)
|
224
|
+
describe '#default_encoding' do
|
225
|
+
it 'is quoted-printable for text/* types' do
|
226
|
+
assert_equal 'quoted-printable', text_plain.default_encoding
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'is base64 for non-text/* types' do
|
230
|
+
assert_equal 'base64', image_jpeg.default_encoding
|
231
|
+
end
|
233
232
|
end
|
234
233
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
234
|
+
describe '#encoding, #encoding=' do
|
235
|
+
it 'returns #default_encoding if not set explicitly' do
|
236
|
+
assert_equal 'quoted-printable', text_plain.encoding
|
237
|
+
assert_equal 'base64', image_jpeg.encoding
|
238
|
+
end
|
239
239
|
|
240
|
-
|
241
|
-
|
240
|
+
it 'returns the set value when set' do
|
241
|
+
text_plain.encoding = '8bit'
|
242
|
+
assert_equal '8bit', text_plain.encoding
|
243
|
+
end
|
242
244
|
|
243
|
-
|
244
|
-
|
245
|
-
|
245
|
+
it 'resets to the default encoding when set to nil or :default' do
|
246
|
+
text_plain.encoding = '8bit'
|
247
|
+
text_plain.encoding = nil
|
248
|
+
assert_equal text_plain.default_encoding, text_plain.encoding
|
249
|
+
text_plain.encoding = :default
|
250
|
+
assert_equal text_plain.default_encoding, text_plain.encoding
|
251
|
+
end
|
246
252
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
refute(MIME::Type.new('text/plain').like?('image/jpeg'))
|
253
|
+
it 'raises a MIME::Type::InvalidEncoding for an invalid encoding' do
|
254
|
+
exception = assert_raises MIME::Type::InvalidEncoding do
|
255
|
+
text_plain.encoding = 'binary'
|
256
|
+
end
|
257
|
+
assert_equal 'Invalid Encoding "binary"', exception.to_s
|
258
|
+
end
|
254
259
|
end
|
255
260
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
assert_equal('text', MIME::Type.new('text/vCard').media_type)
|
261
|
-
assert_equal('application', MIME::Type.new('application/pkcs7-mime').media_type)
|
262
|
-
assert_equal('chemical', MIME::Type.new('x-chemical/x-pdb').media_type)
|
263
|
-
assert_equal('appl', @applzip.media_type)
|
264
|
-
end
|
261
|
+
describe '#eql?' do
|
262
|
+
it 'is not true for a non-MIME::Type' do
|
263
|
+
refute text_plain.eql?('text/plain')
|
264
|
+
end
|
265
265
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
assert(type.obsolete?)
|
270
|
-
refute(make_yaml_mime_type.obsolete?)
|
271
|
-
end
|
266
|
+
it 'is not true for a different MIME::Type' do
|
267
|
+
refute text_plain.eql?(image_jpeg)
|
268
|
+
end
|
272
269
|
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
yaml.obsolete = true
|
277
|
-
assert(yaml.obsolete?)
|
270
|
+
it 'is true for an equivalent MIME::Type' do
|
271
|
+
assert text_plain, mime_type('text/Plain')
|
272
|
+
end
|
278
273
|
end
|
279
274
|
|
280
|
-
|
281
|
-
|
282
|
-
|
275
|
+
describe '#extensions, #extensions=' do
|
276
|
+
it 'returns an array of extensions' do
|
277
|
+
assert_equal %w(yaml yml), text_x_yaml.extensions
|
278
|
+
assert_equal %w(zip zp), x_appl_x_zip.extensions
|
279
|
+
end
|
280
|
+
|
281
|
+
it 'sets a single extension when provided a single value' do
|
282
|
+
text_x_yaml.extensions = 'yaml'
|
283
|
+
assert_equal %w(yaml), text_x_yaml.extensions
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'deduplicates extensions' do
|
287
|
+
text_x_yaml.extensions = %w(yaml yaml)
|
288
|
+
assert_equal %w(yaml), text_x_yaml.extensions
|
283
289
|
end
|
284
290
|
end
|
285
291
|
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
292
|
+
describe '#add_extensions' do
|
293
|
+
it 'does not modify extensions when provided nil' do
|
294
|
+
text_x_yaml.add_extensions(nil)
|
295
|
+
assert_equal %w(yaml yml), text_x_yaml.extensions
|
296
|
+
end
|
297
|
+
|
298
|
+
it 'remains deduplicated with duplicate values' do
|
299
|
+
text_x_yaml.add_extensions('yaml')
|
300
|
+
assert_equal %w(yaml yml), text_x_yaml.extensions
|
301
|
+
text_x_yaml.add_extensions(%w(yaml yz))
|
302
|
+
assert_equal %w(yaml yml yz), text_x_yaml.extensions
|
303
|
+
end
|
290
304
|
end
|
291
305
|
|
292
|
-
|
293
|
-
|
294
|
-
|
306
|
+
describe '#priority_compare' do
|
307
|
+
def assert_priority_less(left, right)
|
308
|
+
assert_equal(-1, left.priority_compare(right))
|
309
|
+
end
|
295
310
|
|
296
|
-
|
297
|
-
|
298
|
-
|
311
|
+
def assert_priority_same(left, right)
|
312
|
+
assert_equal 0, left.priority_compare(right)
|
313
|
+
end
|
299
314
|
|
300
|
-
|
301
|
-
|
302
|
-
|
315
|
+
def assert_priority_more(left, right)
|
316
|
+
assert_equal 1, left.priority_compare(right)
|
317
|
+
end
|
303
318
|
|
304
|
-
|
305
|
-
|
306
|
-
|
319
|
+
def assert_priority(left, middle, right)
|
320
|
+
assert_priority_less left, right
|
321
|
+
assert_priority_same left, middle
|
322
|
+
assert_priority_more right, left
|
323
|
+
end
|
307
324
|
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
assert_priority([tl, tr], [tl, te], [tr, tl])
|
312
|
-
tr.use_instead = 'abc/zzz'
|
313
|
-
assert_priority([tl, tr], [tl, te], [tr, tl])
|
314
|
-
end
|
325
|
+
let(:text_1) { mime_type('text/1') }
|
326
|
+
let(:text_1p) { mime_type('text/1') }
|
327
|
+
let(:text_2) { mime_type('text/2') }
|
315
328
|
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
assert_equal('application', MIME::Type.new('application/x-msword').raw_media_type)
|
320
|
-
assert_equal('text', MIME::Type.new('text/vCard').raw_media_type)
|
321
|
-
assert_equal('application', MIME::Type.new('application/pkcs7-mime').raw_media_type)
|
322
|
-
assert_equal('x-chemical', MIME::Type.new('x-chemical/x-pdb').raw_media_type)
|
323
|
-
assert_equal('x-appl', @applzip.raw_media_type)
|
324
|
-
end
|
329
|
+
it 'sorts (1) based on the simplified type' do
|
330
|
+
assert_priority text_1, text_1p, text_2
|
331
|
+
end
|
325
332
|
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
assert_equal('x-msword', MIME::Type.new('application/x-msword').raw_sub_type)
|
330
|
-
assert_equal('vCard', MIME::Type.new('text/vCard').raw_sub_type)
|
331
|
-
assert_equal('pkcs7-mime', MIME::Type.new('application/pkcs7-mime').raw_sub_type)
|
332
|
-
assert_equal('x-zip', @applzip.raw_sub_type)
|
333
|
-
end
|
333
|
+
it 'sorts (2) based on the registration state' do
|
334
|
+
text_1.registered = text_1p.registered = true
|
335
|
+
text_1b = mime_type(text_1) { |t| t.registered = false }
|
334
336
|
|
335
|
-
|
336
|
-
|
337
|
-
assert(MIME::Type.new('image/jpeg').registered?)
|
338
|
-
refute(MIME::Type.new('application/x-msword').registered?)
|
339
|
-
assert(MIME::Type.new('text/vCard').registered?)
|
340
|
-
assert(MIME::Type.new('application/pkcs7-mime').registered?)
|
341
|
-
refute(@applzip.registered?)
|
342
|
-
refute(MIME::Types['image/webp'].first.registered?)
|
343
|
-
# Temporarily broken: requires the new data format to be enabled.
|
344
|
-
assert(MIME::Types['application/x-www-form-urlencoded'].first.registered?)
|
345
|
-
end
|
337
|
+
assert_priority text_1, text_1p, text_1b
|
338
|
+
end
|
346
339
|
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
assert_equal(v, @applzip.instance_variable_get(:@registered))
|
351
|
-
}
|
352
|
-
@applzip.registered = 1
|
353
|
-
assert_equal(true, @applzip.instance_variable_get(:@registered))
|
354
|
-
end
|
340
|
+
it 'sorts (3) based on the completeness' do
|
341
|
+
text_1.extensions = text_1p.extensions = '1'
|
342
|
+
text_1b = mime_type(text_1) { |t| t.extensions = nil }
|
355
343
|
|
356
|
-
|
357
|
-
|
358
|
-
refute(MIME::Type.new('image/jpeg').signature?)
|
359
|
-
refute(MIME::Type.new('application/x-msword').signature?)
|
360
|
-
end
|
344
|
+
assert_priority text_1, text_1p, text_1b
|
345
|
+
end
|
361
346
|
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
end
|
347
|
+
it 'sorts (4) based on obsolete status' do
|
348
|
+
text_1.obsolete = text_1p.obsolete = false
|
349
|
+
text_1b = mime_type(text_1) { |t| t.obsolete = true }
|
366
350
|
|
367
|
-
|
368
|
-
|
369
|
-
assert_equal('image/jpeg', MIME::Type.new('image/jpeg').simplified)
|
370
|
-
assert_equal('application/msword', MIME::Type.new('application/x-msword').simplified)
|
371
|
-
assert_equal('text/vcard', MIME::Type.new('text/vCard').simplified)
|
372
|
-
assert_equal('application/pkcs7-mime', MIME::Type.new('application/pkcs7-mime').simplified)
|
373
|
-
assert_equal('chemical/pdb', MIME::Type.new('x-chemical/x-pdb').simplified)
|
374
|
-
end
|
351
|
+
assert_priority text_1, text_1p, text_1b
|
352
|
+
end
|
375
353
|
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
assert_equal('zip', @applzip.sub_type)
|
383
|
-
end
|
354
|
+
it 'sorts (5) based on the use-instead value' do
|
355
|
+
text_1.obsolete = text_1p.obsolete = true
|
356
|
+
text_1.use_instead = text_1p.use_instead = 'abc/xyz'
|
357
|
+
text_1b = mime_type(text_1) { |t| t.use_instead = nil }
|
358
|
+
|
359
|
+
assert_priority text_1, text_1p, text_1b
|
384
360
|
|
385
|
-
|
386
|
-
|
387
|
-
|
361
|
+
text_1b.use_instead = 'abc/zzz'
|
362
|
+
|
363
|
+
assert_priority text_1, text_1p, text_1b
|
388
364
|
end
|
389
365
|
end
|
390
366
|
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
yaml.system = /win32/
|
395
|
-
assert_nil yaml.system
|
367
|
+
describe '#raw_media_type' do
|
368
|
+
it 'extracts the media type as case-preserved' do
|
369
|
+
assert_equal 'Text', mime_type('Text/plain').raw_media_type
|
396
370
|
end
|
397
|
-
end
|
398
371
|
|
399
|
-
|
400
|
-
|
401
|
-
refute make_yaml_mime_type.system?
|
372
|
+
it 'does not remove x- prefixes' do
|
373
|
+
assert_equal('x-appl', x_appl_x_zip.raw_media_type)
|
402
374
|
end
|
403
375
|
end
|
404
376
|
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
assert_equal [ 'text/x-yaml', %w(yaml yml), '8bit', nil, false, [], [],
|
409
|
-
false ], yaml.to_a
|
377
|
+
describe '#media_type' do
|
378
|
+
it 'extracts the media type as lowercase' do
|
379
|
+
assert_equal 'text', text_plain.media_type
|
410
380
|
end
|
411
|
-
end
|
412
381
|
|
413
|
-
|
414
|
-
|
415
|
-
assert_deprecated('MIME::Type#to_hash') do
|
416
|
-
assert_equal(
|
417
|
-
{
|
418
|
-
'Content-Type' => 'text/x-yaml',
|
419
|
-
'Content-Transfer-Encoding' => '8bit',
|
420
|
-
'Extensions' => %w(yaml yml),
|
421
|
-
'System' => nil,
|
422
|
-
'Registered' => false,
|
423
|
-
'URL' => [],
|
424
|
-
'Obsolete' => false,
|
425
|
-
'Docs' => []
|
426
|
-
},
|
427
|
-
yaml.to_hash)
|
382
|
+
it 'does not remove x- prefixes' do
|
383
|
+
assert_equal('x-appl', x_appl_x_zip.media_type)
|
428
384
|
end
|
429
385
|
end
|
430
386
|
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
387
|
+
describe '#raw_media_type' do
|
388
|
+
it 'extracts the media type as case-preserved' do
|
389
|
+
assert_equal 'Text', mime_type('Text/plain').raw_media_type
|
390
|
+
end
|
435
391
|
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
assert_type_has_keys(make(t) { |v| v.docs = 'Something' }, 'docs')
|
440
|
-
assert_type_has_keys(make(t) { |v| v.extensions = %w(b) }, 'extensions')
|
441
|
-
assert_type_has_keys(make(t) { |v| v.obsolete = true }, 'obsolete')
|
442
|
-
assert_type_has_keys(make(t) { |v|
|
443
|
-
v.obsolete = true
|
444
|
-
v.use_instead = 'c/d'
|
445
|
-
}, 'obsolete', 'use-instead')
|
446
|
-
assert_type_has_keys(make(t) { |v| v.signature = true }, 'signature')
|
392
|
+
it 'does not remove x- prefixes' do
|
393
|
+
assert_equal('x-appl', x_appl_x_zip.raw_media_type)
|
394
|
+
end
|
447
395
|
end
|
448
396
|
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
397
|
+
describe '#sub_type' do
|
398
|
+
it 'extracts the sub type as lowercase' do
|
399
|
+
assert_equal 'plain', text_plain.sub_type
|
400
|
+
end
|
453
401
|
|
454
|
-
|
455
|
-
|
402
|
+
it 'does not remove x- prefixes' do
|
403
|
+
assert_equal('x-zip', x_appl_x_zip.sub_type)
|
404
|
+
end
|
456
405
|
end
|
457
406
|
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
assert_raises(MIME::Type::InvalidContentType) { MIME::Type.new('apps') }
|
466
|
-
begin
|
467
|
-
MIME::Type.new(nil)
|
468
|
-
rescue MIME::Type::InvalidContentType => ex
|
469
|
-
assert_equal('Invalid Content-Type nil', ex.message)
|
407
|
+
describe '#raw_sub_type' do
|
408
|
+
it 'extracts the sub type as case-preserved' do
|
409
|
+
assert_equal 'Plain', mime_type('text/Plain').raw_sub_type
|
410
|
+
end
|
411
|
+
|
412
|
+
it 'does not remove x- prefixes' do
|
413
|
+
assert_equal('x-zip', x_appl_x_zip.raw_sub_type)
|
470
414
|
end
|
471
415
|
end
|
472
416
|
|
473
|
-
|
474
|
-
|
417
|
+
describe '#to_h' do
|
418
|
+
let(:t) { mime_type('a/b') }
|
419
|
+
|
420
|
+
it 'has the required keys (content-type, registered, encoding)' do
|
421
|
+
assert_has_keys t.to_h, %w(content-type registered encoding)
|
422
|
+
end
|
423
|
+
|
424
|
+
it 'has the docs key if there are documents' do
|
425
|
+
assert_has_keys mime_type(t) { |v| v.docs = 'a' }.to_h, %w(docs)
|
426
|
+
end
|
427
|
+
|
428
|
+
it 'has the extensions key if set' do
|
429
|
+
assert_has_keys mime_type(t) { |v| v.extensions = 'a' }.to_h,
|
430
|
+
'extensions'
|
431
|
+
end
|
432
|
+
|
433
|
+
it 'has the preferred-extension key if set' do
|
434
|
+
assert_has_keys mime_type(t) { |v| v.preferred_extension = 'a' }.to_h,
|
435
|
+
'preferred-extension'
|
436
|
+
end
|
437
|
+
|
438
|
+
it 'has the obsolete key if set' do
|
439
|
+
assert_has_keys mime_type(t) { |v| v.obsolete = true }.to_h, 'obsolete'
|
440
|
+
end
|
441
|
+
|
442
|
+
it 'has the obsolete and use-instead keys if set' do
|
443
|
+
assert_has_keys mime_type(t) { |v|
|
444
|
+
v.obsolete = true
|
445
|
+
v.use_instead = 'c/d'
|
446
|
+
}.to_h, %w(obsolete use-instead)
|
447
|
+
end
|
448
|
+
|
449
|
+
it 'has the signature key if set' do
|
450
|
+
assert_has_keys mime_type(t) { |v| v.signature = true }.to_h, 'signature'
|
451
|
+
end
|
475
452
|
end
|
476
453
|
|
477
|
-
|
478
|
-
|
479
|
-
|
454
|
+
describe '#to_json' do
|
455
|
+
let(:expected) {
|
456
|
+
'{"content-type":"a/b","encoding":"base64","registered":false}'
|
457
|
+
}
|
458
|
+
|
459
|
+
it 'converts to JSON when requested' do
|
460
|
+
assert_equal expected, mime_type('a/b').to_json
|
480
461
|
end
|
481
462
|
end
|
482
463
|
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
yaml.references = :anything
|
464
|
+
describe '#to_s, #to_str' do
|
465
|
+
it 'represents itself as a string of the canonical content_type' do
|
466
|
+
assert_equal 'text/plain', "#{text_plain}"
|
487
467
|
end
|
488
|
-
|
489
|
-
|
468
|
+
|
469
|
+
it 'acts like a string of the canonical content_type for comparison' do
|
470
|
+
assert_equal text_plain, 'text/plain'
|
471
|
+
end
|
472
|
+
|
473
|
+
it 'acts like a string for other purposes' do
|
474
|
+
assert_equal 'stringy', 'text/plain'.sub(text_plain, 'stringy')
|
490
475
|
end
|
491
476
|
end
|
492
477
|
|
493
|
-
|
494
|
-
|
478
|
+
describe '#xrefs, #xrefs=' do
|
479
|
+
let(:expected) {
|
495
480
|
{
|
496
|
-
'rfc' =>
|
497
|
-
|
498
|
-
|
499
|
-
make_javascript.xrefs
|
500
|
-
)
|
501
|
-
end
|
481
|
+
'rfc' => Set[*%w(rfc1234 rfc5678)]
|
482
|
+
}
|
483
|
+
}
|
502
484
|
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
'uri' => [ 'http://exmple.org' ],
|
510
|
-
'text' => [ 'text' ]
|
511
|
-
)
|
485
|
+
it 'returns the expected results' do
|
486
|
+
application_javascript.xrefs = {
|
487
|
+
'rfc' => %w(rfc5678 rfc1234 rfc1234)
|
488
|
+
}
|
489
|
+
|
490
|
+
assert_equal expected, application_javascript.xrefs
|
512
491
|
end
|
513
|
-
|
492
|
+
end
|
493
|
+
|
494
|
+
describe '#xref_urls' do
|
495
|
+
let(:expected) {
|
514
496
|
[
|
515
|
-
'http://www.iana.org/go/
|
516
|
-
'http://www.iana.org/assignments/media-types/
|
517
|
-
'http://www.iana.org/
|
518
|
-
'http://www.iana.org/
|
519
|
-
'http://www.rfc-editor.org/errata_search.php?eid=
|
520
|
-
'http://
|
497
|
+
'http://www.iana.org/go/draft1',
|
498
|
+
'http://www.iana.org/assignments/media-types/a/b',
|
499
|
+
'http://www.iana.org/assignments/media-types/media-types.xhtml#p-1',
|
500
|
+
'http://www.iana.org/go/rfc-1',
|
501
|
+
'http://www.rfc-editor.org/errata_search.php?eid=err-1',
|
502
|
+
'http://example.org',
|
521
503
|
'text'
|
522
|
-
]
|
523
|
-
|
524
|
-
)
|
525
|
-
end
|
504
|
+
]
|
505
|
+
}
|
526
506
|
|
527
|
-
|
528
|
-
|
529
|
-
|
507
|
+
let(:type) {
|
508
|
+
mime_type('a/b').tap do |t|
|
509
|
+
t.xrefs = {
|
510
|
+
'draft' => [ 'RFC1' ],
|
511
|
+
'template' => [ 'a/b' ],
|
512
|
+
'person' => [ 'p-1' ],
|
513
|
+
'rfc' => [ 'rfc-1' ],
|
514
|
+
'rfc-errata' => [ 'err-1' ],
|
515
|
+
'uri' => [ 'http://example.org' ],
|
516
|
+
'text' => [ 'text' ]
|
517
|
+
}
|
518
|
+
end
|
519
|
+
}
|
520
|
+
|
521
|
+
it 'translates according to given rules' do
|
522
|
+
assert_equal expected, type.xref_urls
|
530
523
|
end
|
531
524
|
end
|
532
525
|
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
yaml.url = 'IANA'
|
526
|
+
describe '#use_instead' do
|
527
|
+
it 'is nil unless the type is obsolete' do
|
528
|
+
assert_nil text_plain.use_instead
|
537
529
|
end
|
538
|
-
|
539
|
-
|
530
|
+
|
531
|
+
it 'is nil if not set and the type is obsolete' do
|
532
|
+
text_plain.obsolete = true
|
533
|
+
assert_nil text_plain.use_instead
|
540
534
|
end
|
541
|
-
end
|
542
535
|
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
536
|
+
it 'is a different type if set and the type is obsolete' do
|
537
|
+
text_plain.obsolete = true
|
538
|
+
text_plain.use_instead = 'text/html'
|
539
|
+
assert_equal 'text/html', text_plain.use_instead
|
547
540
|
end
|
548
541
|
end
|
549
542
|
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
assert_equal('t/2', t.use_instead)
|
555
|
-
end
|
543
|
+
describe '#preferred_extension, #preferred_extension=' do
|
544
|
+
it 'is nil when not set and there are no extensions' do
|
545
|
+
assert_nil text_plain.preferred_extension
|
546
|
+
end
|
556
547
|
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
t.use_instead = 't/2'
|
561
|
-
assert_equal('t/2', t.use_instead)
|
562
|
-
end
|
548
|
+
it 'is the first extension when not set but there are extensions' do
|
549
|
+
assert_equal 'yaml', text_x_yaml.preferred_extension
|
550
|
+
end
|
563
551
|
|
564
|
-
|
565
|
-
|
566
|
-
|
552
|
+
it 'is the extension provided when set' do
|
553
|
+
text_x_yaml.preferred_extension = 'yml'
|
554
|
+
assert_equal 'yml', text_x_yaml.preferred_extension
|
555
|
+
end
|
567
556
|
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
557
|
+
it 'is adds the preferred extension if it does not exist' do
|
558
|
+
text_x_yaml.preferred_extension = 'yz'
|
559
|
+
assert_equal 'yz', text_x_yaml.preferred_extension
|
560
|
+
assert_includes text_x_yaml.extensions, 'yz'
|
561
|
+
end
|
573
562
|
end
|
574
563
|
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
564
|
+
describe '#friendly' do
|
565
|
+
it 'returns English by default' do
|
566
|
+
assert_equal 'YAML Structured Document', text_x_yaml.friendly
|
567
|
+
end
|
579
568
|
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
569
|
+
it 'returns English when requested' do
|
570
|
+
assert_equal 'YAML Structured Document', text_x_yaml.friendly('en')
|
571
|
+
assert_equal 'YAML Structured Document', text_x_yaml.friendly(:en)
|
572
|
+
end
|
573
|
+
|
574
|
+
it 'returns nothing for an unknown language' do
|
575
|
+
assert_nil text_x_yaml.friendly('zz')
|
576
|
+
end
|
586
577
|
|
587
|
-
|
588
|
-
|
589
|
-
|
578
|
+
it 'merges new values from an array parameter' do
|
579
|
+
expected = { 'en' => 'Text files' }
|
580
|
+
assert_equal expected, text_plain.friendly([ 'en', 'Text files' ])
|
581
|
+
expected.update('fr' => 'des fichiers texte')
|
582
|
+
assert_equal expected,
|
583
|
+
text_plain.friendly([ 'fr', 'des fichiers texte' ])
|
590
584
|
end
|
591
|
-
|
592
|
-
|
585
|
+
|
586
|
+
it 'merges new values from a hash parameter' do
|
587
|
+
expected = { 'en' => 'Text files' }
|
588
|
+
assert_equal expected, text_plain.friendly(expected)
|
589
|
+
french = { 'fr' => 'des fichiers texte' }
|
590
|
+
expected.update(french)
|
591
|
+
assert_equal expected, text_plain.friendly(french)
|
592
|
+
end
|
593
|
+
|
594
|
+
it 'raises an ArgumentError if an unknown value is provided' do
|
595
|
+
exception = assert_raises ArgumentError do
|
596
|
+
text_plain.friendly(1)
|
597
|
+
end
|
598
|
+
|
599
|
+
assert_equal 'Expected a language or translation set, not 1',
|
600
|
+
exception.message
|
593
601
|
end
|
594
|
-
assert_raises(NameError) { MIME::Foo }
|
595
602
|
end
|
596
603
|
end
|