1 import io 2 import locale 3 import mimetypes 4 import sys 5 import unittest 6 7 from test import support 8 9 # Tell it we don't know about external files: 10 mimetypes.knownfiles = [] 11 mimetypes.inited = False 12 mimetypes._default_mime_types() 13 14 15 class MimeTypesTestCase(unittest.TestCase): 16 def setUp(self): 17 self.db = mimetypes.MimeTypes() 18 19 def test_default_data(self): 20 eq = self.assertEqual 21 eq(self.db.guess_type("foo.html"), ("text/html", None)) 22 eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip")) 23 eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip")) 24 eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress")) 25 eq(self.db.guess_type("foo.tar.bz2"), ("application/x-tar", "bzip2")) 26 eq(self.db.guess_type("foo.tar.xz"), ("application/x-tar", "xz")) 27 28 def test_data_urls(self): 29 eq = self.assertEqual 30 guess_type = self.db.guess_type 31 eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None)) 32 eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None)) 33 eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None)) 34 35 def test_file_parsing(self): 36 eq = self.assertEqual 37 sio = io.StringIO("x-application/x-unittest pyunit\n") 38 self.db.readfp(sio) 39 eq(self.db.guess_type("foo.pyunit"), 40 ("x-application/x-unittest", None)) 41 eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit") 42 43 def test_non_standard_types(self): 44 eq = self.assertEqual 45 # First try strict 46 eq(self.db.guess_type('foo.xul', strict=True), (None, None)) 47 eq(self.db.guess_extension('image/jpg', strict=True), None) 48 # And then non-strict 49 eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None)) 50 eq(self.db.guess_extension('image/jpg', strict=False), '.jpg') 51 52 def test_guess_all_types(self): 53 eq = self.assertEqual 54 unless = self.assertTrue 55 # First try strict. Use a set here for testing the results because if 56 # test_urllib2 is run before test_mimetypes, global state is modified 57 # such that the 'all' set will have more items in it. 58 all = set(self.db.guess_all_extensions('text/plain', strict=True)) 59 unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt'])) 60 # And now non-strict 61 all = self.db.guess_all_extensions('image/jpg', strict=False) 62 all.sort() 63 eq(all, ['.jpg']) 64 # And now for no hits 65 all = self.db.guess_all_extensions('image/jpg', strict=True) 66 eq(all, []) 67 68 def test_encoding(self): 69 getpreferredencoding = locale.getpreferredencoding 70 self.addCleanup(setattr, locale, 'getpreferredencoding', 71 getpreferredencoding) 72 locale.getpreferredencoding = lambda: 'ascii' 73 74 filename = support.findfile("mime.types") 75 mimes = mimetypes.MimeTypes([filename]) 76 exts = mimes.guess_all_extensions('application/vnd.geocube+xml', 77 strict=True) 78 self.assertEqual(exts, ['.g3', '.g\xb3']) 79 80 81 @unittest.skipUnless(sys.platform.startswith("win"), "Windows only") 82 class Win32MimeTypesTestCase(unittest.TestCase): 83 def setUp(self): 84 # ensure all entries actually come from the Windows registry 85 self.original_types_map = mimetypes.types_map.copy() 86 mimetypes.types_map.clear() 87 mimetypes.init() 88 self.db = mimetypes.MimeTypes() 89 90 def tearDown(self): 91 # restore default settings 92 mimetypes.types_map.clear() 93 mimetypes.types_map.update(self.original_types_map) 94 95 def test_registry_parsing(self): 96 # the original, minimum contents of the MIME database in the 97 # Windows registry is undocumented AFAIK. 98 # Use file types that should *always* exist: 99 eq = self.assertEqual 100 eq(self.db.guess_type("foo.txt"), ("text/plain", None)) 101 eq(self.db.guess_type("image.jpg"), ("image/jpeg", None)) 102 eq(self.db.guess_type("image.png"), ("image/png", None)) 103 104 105 class MiscTestCase(unittest.TestCase): 106 def test__all__(self): 107 support.check__all__(self, mimetypes) 108 109 110 if __name__ == "__main__": 111 unittest.main() 112