Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/test/controller/mime_type_test.rb

Revision 8677, 2.2 kB (checked in by nzkoz, 11 months ago)

Ensure mime types can be compared with symbols. Closes #10796 [bscofield]

Line 
1 require 'abstract_unit'
2
3 class MimeTypeTest < Test::Unit::TestCase
4   Mime::Type.register "image/png", :png
5   Mime::Type.register "application/pdf", :pdf
6
7   def test_parse_single
8     Mime::LOOKUP.keys.each do |mime_type|
9       assert_equal [Mime::Type.lookup(mime_type)], Mime::Type.parse(mime_type)
10     end
11   end
12
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)
17   end
18
19   def test_parse_with_q
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)
23   end
24  
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 }
30   end
31  
32   def test_custom_type
33     Mime::Type.register("image/gif", :gif)
34     assert_nothing_raised do
35       Mime::GIF
36       assert_equal Mime::GIF, Mime::SET.last
37     end
38   ensure
39     Mime.module_eval { remove_const :GIF if const_defined?(:GIF) }
40   end
41  
42   def test_type_should_be_equal_to_symbol
43     assert_equal Mime::HTML, 'application/xhtml+xml'
44     assert_equal Mime::HTML, :html
45   end
46
47   def test_type_convenience_methods
48     types = [:html, :xml, :png, :pdf, :yaml, :url_encoded_form]
49     types.each do |type|
50       mime = Mime.const_get(type.to_s.upcase)
51       assert mime.send("#{type}?"), "Mime::#{type.to_s.upcase} is not #{type}?"
52       (types - [type]).each { |t| assert !mime.send("#{t}?"), "Mime::#{t.to_s.upcase} is #{t}?" }
53     end
54   end
55  
56   def test_mime_all_is_html
57     assert Mime::ALL.all?,  "Mime::ALL is not all?"
58     assert Mime::ALL.html?, "Mime::ALL is not html?"
59   end
60 end
Note: See TracBrowser for help on using the browser.