home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / pythonwin / python.exe / TEST_MIMETYPES.PY < prev    next >
Encoding:
Python Source  |  2003-07-18  |  2.3 KB  |  68 lines

  1. import mimetypes
  2. import StringIO
  3. import unittest
  4.  
  5. from test import test_support
  6.  
  7. # Tell it we don't know about external files:
  8. mimetypes.knownfiles = []
  9. mimetypes.inited = False
  10.  
  11.  
  12. class MimeTypesTestCase(unittest.TestCase):
  13.     def setUp(self):
  14.         self.db = mimetypes.MimeTypes()
  15.  
  16.     def test_default_data(self):
  17.         eq = self.assertEqual
  18.         eq(self.db.guess_type("foo.html"), ("text/html", None))
  19.         eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
  20.         eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
  21.         eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
  22.  
  23.     def test_data_urls(self):
  24.         eq = self.assertEqual
  25.         guess_type = self.db.guess_type
  26.         eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
  27.         eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
  28.         eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
  29.  
  30.     def test_file_parsing(self):
  31.         eq = self.assertEqual
  32.         sio = StringIO.StringIO("x-application/x-unittest pyunit\n")
  33.         self.db.readfp(sio)
  34.         eq(self.db.guess_type("foo.pyunit"),
  35.            ("x-application/x-unittest", None))
  36.         eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
  37.  
  38.     def test_non_standard_types(self):
  39.         eq = self.assertEqual
  40.         # First try strict
  41.         eq(self.db.guess_type('foo.xul', strict=True), (None, None))
  42.         eq(self.db.guess_extension('image/jpg', strict=True), None)
  43.         # And then non-strict
  44.         eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
  45.         eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
  46.  
  47.     def test_guess_all_types(self):
  48.         eq = self.assertEqual
  49.         # First try strict
  50.         all = self.db.guess_all_extensions('text/plain', strict=True)
  51.         all.sort()
  52.         eq(all, ['.bat', '.c', '.h', '.ksh', '.pl', '.txt'])
  53.         # And now non-strict
  54.         all = self.db.guess_all_extensions('image/jpg', strict=False)
  55.         all.sort()
  56.         eq(all, ['.jpg'])
  57.         # And now for no hits
  58.         all = self.db.guess_all_extensions('image/jpg', strict=True)
  59.         eq(all, [])
  60.  
  61.  
  62. def test_main():
  63.     test_support.run_unittest(MimeTypesTestCase)
  64.  
  65.  
  66. if __name__ == "__main__":
  67.     test_main()
  68.