1 require 'abstract_unit'
3 class MimeTypeTest
< Test
::Unit::TestCase
4 Mime
::Type.register
"image/png", :png
5 Mime
::Type.register
"application/pdf", :pdf
8 Mime
::LOOKUP.keys
.each
do |mime_type
|
9 assert_equal
[Mime
::Type.lookup(mime_type
)], Mime
::Type.parse(mime_type
)
13 def test_parse_without_q
14 accept
= "text/xml,application/xhtml+xml,text/yaml,application/xml,text/html,image/png,text/plain,application/pdf,*/*"
15 expect
= [Mime
::HTML, Mime
::XML, Mime
::YAML, Mime
::PNG, Mime
::TEXT, Mime
::PDF, Mime
::ALL]
16 assert_equal expect
, Mime
::Type.parse(accept
)
20 accept
= "text/xml,application/xhtml+xml,text/yaml; q=0.3,application/xml,text/html; q=0.8,image/png,text/plain; q=0.5,application/pdf,*/*; q=0.2"
21 expect
= [Mime
::HTML, Mime
::XML, Mime
::PNG, Mime
::PDF, Mime
::TEXT, Mime
::YAML, Mime
::ALL]
22 assert_equal expect
, Mime
::Type.parse(accept
)
25 # Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP)
26 def test_parse_crappy_broken_acceptlines
27 accept
= "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5"
28 expect
= [Mime
::HTML, Mime
::XML, "image/*", Mime
::TEXT, Mime
::ALL]
29 assert_equal expect
, Mime
::Type.parse(accept
).collect
{ |c
| c
.to_s
}
32 # Accept header send with user HTTP_USER_AGENT: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1)
33 def test_parse_crappy_broken_acceptlines2
34 accept
= "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, , pronto/1.00.00, sslvpn/1.00.00.00, */*"
35 expect
= ['image/gif', 'image/x-xbitmap', 'image/jpeg','image/pjpeg', 'application/x-shockwave-flash', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/msword', 'pronto/1.00.00', 'sslvpn/1.00.00.00', Mime
::ALL ]
36 assert_equal expect
, Mime
::Type.parse(accept
).collect
{ |c
| c
.to_s
}
40 Mime
::Type.register("image/gif", :gif)
41 assert_nothing_raised
do
43 assert_equal Mime
::GIF, Mime
::SET.last
46 Mime
.module_eval
{ remove_const
:GIF if const_defined
?(:GIF) }
49 def test_type_should_be_equal_to_symbol
50 assert_equal Mime
::HTML, 'application/xhtml+xml'
51 assert_equal Mime
::HTML, :html
54 def test_type_convenience_methods
55 # Don't test Mime::ALL, since it Mime::ALL#html? == true
56 types
= Mime
::SET.to_a
.map(&:to_sym).uniq
- [:all]
58 # Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE
59 types
.delete_if
{ |type
| !Mime
.const_defined
?(type
.to_s
.upcase
) }
62 mime
= Mime
.const_get(type
.to_s
.upcase
)
63 assert mime
.send("#{type}?"), "#{mime.inspect} is not #{type}?"
64 invalid_types
= types
- [type
]
65 invalid_types
.delete(:html) if Mime
::Type.html_types
.include?(type
)
66 invalid_types
.each
{ |other_type
| assert
!mime
.send("#{other_type}?"), "#{mime.inspect} is #{other_type}?" }
70 def test_mime_all_is_html
71 assert Mime
::ALL.all
?, "Mime::ALL is not all?"
72 assert Mime
::ALL.html
?, "Mime::ALL is not html?"
75 def test_verifiable_mime_types
76 all_types
= Mime
::SET.to_a
.map(&:to_sym)
78 # Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE
79 all_types
.delete_if
{ |type
| !Mime
.const_defined
?(type
.to_s
.upcase
) }
80 verified
, unverified
= all_types
.partition
{ |type
| Mime
::Type.browser_generated_types
.include? type
}
81 assert verified
.each
{ |type
| assert Mime
.const_get(type
.to_s
.upcase
).verify_request
?, "Verifiable Mime Type is not verified: #{type.inspect}" }
82 assert unverified
.each
{ |type
| assert
!Mime
.const_get(type
.to_s
.upcase
).verify_request
?, "Nonverifiable Mime Type is verified: #{type.inspect}" }
85 def test_regexp_matcher
86 assert Mime
::JS =~
"text/javascript"
87 assert Mime
::JS =~
"application/javascript"
88 assert Mime
::JS !~
"text/html"
89 assert
!(Mime
::JS !~
"text/javascript")
90 assert
!(Mime
::JS !~
"application/javascript")
91 assert Mime
::HTML =~
'application/xhtml+xml'