mime_type_test.rb 2.0 KB
Newer Older
1 2 3
require File.dirname(__FILE__) + '/../abstract_unit'

class MimeTypeTest < Test::Unit::TestCase
4
  Mime::Type.register "image/png", :png
5
  Mime::Type.register "application/pdf", :pdf
6 7 8 9 10 11 12 13

  def test_parse_single
    Mime::LOOKUP.keys.each do |mime_type|
      assert_equal [Mime::Type.lookup(mime_type)], Mime::Type.parse(mime_type)
    end
  end

  def test_parse_without_q
14 15
    accept = "text/xml,application/xhtml+xml,text/yaml,application/xml,text/html,image/png,text/plain,application/pdf,*/*"
    expect = [Mime::HTML, Mime::XML, Mime::YAML, Mime::PNG, Mime::TEXT, Mime::PDF, Mime::ALL]
16 17 18 19
    assert_equal expect, Mime::Type.parse(accept)
  end

  def test_parse_with_q
20 21
    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"
    expect = [Mime::HTML, Mime::XML, Mime::PNG, Mime::PDF, Mime::TEXT, Mime::YAML, Mime::ALL]
22 23
    assert_equal expect, Mime::Type.parse(accept)
  end
24
  
25 26 27 28 29 30 31
  # Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP)
  def test_parse_crappy_broken_acceptlines
    accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5"
    expect = [Mime::HTML, Mime::XML, "image/*", Mime::TEXT, Mime::ALL]
    assert_equal expect, Mime::Type.parse(accept).collect { |c| c.to_s }
  end
  
32 33
  def test_custom_type
    Mime::Type.register("image/gif", :gif)
34 35 36 37
    assert_nothing_raised do 
      Mime::GIF
      assert_equal Mime::GIF, Mime::SET.last
    end
38 39
    Mime.send :remove_const, :GIF
  end
40 41
  
  def test_type_convenience_methods
42
    types = [:html, :xml, :png, :pdf, :yaml, :url_encoded_form]
43 44 45 46 47 48 49 50 51 52 53
    types.each do |type|
      mime = Mime.const_get(type.to_s.upcase)
      assert mime.send("#{type}?"), "Mime::#{type.to_s.upcase} is not #{type}?"
      (types - [type]).each { |t| assert !mime.send("#{t}?"), "Mime::#{t.to_s.upcase} is #{t}?" }
    end
  end
  
  def test_mime_all_is_html
    assert Mime::ALL.all?,  "Mime::ALL is not all?"
    assert Mime::ALL.html?, "Mime::ALL is not html?"
  end
54
end