Home | History | Annotate | Download | only in test
      1 import mimetypes
      2 import StringIO
      3 import unittest
      4 import sys
      5 
      6 from test import test_support
      7 
      8 # Tell it we don't know about external files:

      9 mimetypes.knownfiles = []
     10 mimetypes.inited = False
     11 mimetypes._default_mime_types()
     12 
     13 
     14 class MimeTypesTestCase(unittest.TestCase):
     15     def setUp(self):
     16         self.db = mimetypes.MimeTypes()
     17 
     18     def test_default_data(self):
     19         eq = self.assertEqual
     20         eq(self.db.guess_type("foo.html"), ("text/html", None))
     21         eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
     22         eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
     23         eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
     24 
     25     def test_data_urls(self):
     26         eq = self.assertEqual
     27         guess_type = self.db.guess_type
     28         eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
     29         eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
     30         eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
     31 
     32     def test_file_parsing(self):
     33         eq = self.assertEqual
     34         sio = StringIO.StringIO("x-application/x-unittest pyunit\n")
     35         self.db.readfp(sio)
     36         eq(self.db.guess_type("foo.pyunit"),
     37            ("x-application/x-unittest", None))
     38         eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
     39 
     40     def test_non_standard_types(self):
     41         eq = self.assertEqual
     42         # First try strict

     43         eq(self.db.guess_type('foo.xul', strict=True), (None, None))
     44         eq(self.db.guess_extension('image/jpg', strict=True), None)
     45         # And then non-strict

     46         eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
     47         eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
     48 
     49     def test_guess_all_types(self):
     50         eq = self.assertEqual
     51         unless = self.assertTrue
     52         # First try strict.  Use a set here for testing the results because if

     53         # test_urllib2 is run before test_mimetypes, global state is modified

     54         # such that the 'all' set will have more items in it.

     55         all = set(self.db.guess_all_extensions('text/plain', strict=True))
     56         unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
     57         # And now non-strict

     58         all = self.db.guess_all_extensions('image/jpg', strict=False)
     59         all.sort()
     60         eq(all, ['.jpg'])
     61         # And now for no hits

     62         all = self.db.guess_all_extensions('image/jpg', strict=True)
     63         eq(all, [])
     64 
     65 
     66 @unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
     67 class Win32MimeTypesTestCase(unittest.TestCase):
     68     def setUp(self):
     69         # ensure all entries actually come from the Windows registry

     70         self.original_types_map = mimetypes.types_map.copy()
     71         mimetypes.types_map.clear()
     72         mimetypes.init()
     73         self.db = mimetypes.MimeTypes()
     74 
     75     def tearDown(self):
     76         # restore default settings

     77         mimetypes.types_map.clear()
     78         mimetypes.types_map.update(self.original_types_map)
     79 
     80     def test_registry_parsing(self):
     81         # the original, minimum contents of the MIME database in the

     82         # Windows registry is undocumented AFAIK.

     83         # Use file types that should *always* exist:

     84         eq = self.assertEqual
     85         eq(self.db.guess_type("foo.txt"), ("text/plain", None))
     86 
     87 def test_main():
     88     test_support.run_unittest(MimeTypesTestCase,
     89         Win32MimeTypesTestCase
     90         )
     91 
     92 
     93 if __name__ == "__main__":
     94     test_main()
     95