blob: 91da28927dc9f9523182262bcaf947a3aac805f6 [file] [log] [blame]
Guido van Rossum34d19282007-08-09 01:03:29 +00001import io
Victor Stinner82ac9bc2011-10-14 03:03:35 +02002import locale
3import mimetypes
Antoine Pitroub8108e22009-11-15 14:25:16 +00004import sys
Victor Stinner82ac9bc2011-10-14 03:03:35 +02005import unittest
Fred Drake37918382001-08-16 18:36:59 +00006
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Fred Drake37918382001-08-16 18:36:59 +00008
9# Tell it we don't know about external files:
10mimetypes.knownfiles = []
Jeremy Hylton969a7002003-07-18 15:13:37 +000011mimetypes.inited = False
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000012mimetypes._default_mime_types()
Fred Drake37918382001-08-16 18:36:59 +000013
14
15class MimeTypesTestCase(unittest.TestCase):
16 def setUp(self):
17 self.db = mimetypes.MimeTypes()
18
19 def test_default_data(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000020 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"))
Fred Drake37918382001-08-16 18:36:59 +000025
26 def test_data_urls(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000027 eq = self.assertEqual
28 guess_type = self.db.guess_type
29 eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
30 eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
31 eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
Fred Drake37918382001-08-16 18:36:59 +000032
33 def test_file_parsing(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000034 eq = self.assertEqual
Guido van Rossum34d19282007-08-09 01:03:29 +000035 sio = io.StringIO("x-application/x-unittest pyunit\n")
Fred Drake37918382001-08-16 18:36:59 +000036 self.db.readfp(sio)
Barry Warsaw9caa0d12003-06-09 22:27:41 +000037 eq(self.db.guess_type("foo.pyunit"),
38 ("x-application/x-unittest", None))
39 eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
Fred Drake37918382001-08-16 18:36:59 +000040
Barry Warsaw107771a2001-10-25 21:49:18 +000041 def test_non_standard_types(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000042 eq = self.assertEqual
Tim Peters1633a2e2001-10-30 05:56:40 +000043 # First try strict
Barry Warsaw9caa0d12003-06-09 22:27:41 +000044 eq(self.db.guess_type('foo.xul', strict=True), (None, None))
45 eq(self.db.guess_extension('image/jpg', strict=True), None)
Barry Warsaw107771a2001-10-25 21:49:18 +000046 # And then non-strict
Barry Warsaw9caa0d12003-06-09 22:27:41 +000047 eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
48 eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
49
50 def test_guess_all_types(self):
51 eq = self.assertEqual
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000052 unless = self.assertTrue
Barry Warsawceca5d22003-11-23 16:21:55 +000053 # First try strict. Use a set here for testing the results because if
54 # test_urllib2 is run before test_mimetypes, global state is modified
55 # such that the 'all' set will have more items in it.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056 all = set(self.db.guess_all_extensions('text/plain', strict=True))
57 unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
Barry Warsaw9caa0d12003-06-09 22:27:41 +000058 # And now non-strict
59 all = self.db.guess_all_extensions('image/jpg', strict=False)
60 all.sort()
61 eq(all, ['.jpg'])
62 # And now for no hits
63 all = self.db.guess_all_extensions('image/jpg', strict=True)
64 eq(all, [])
Barry Warsaw107771a2001-10-25 21:49:18 +000065
Victor Stinner82ac9bc2011-10-14 03:03:35 +020066 def test_encoding(self):
67 getpreferredencoding = locale.getpreferredencoding
68 self.addCleanup(setattr, locale, 'getpreferredencoding',
69 getpreferredencoding)
70 locale.getpreferredencoding = lambda: 'ascii'
71
72 filename = support.findfile("mime.types")
73 mimes = mimetypes.MimeTypes([filename])
74 exts = mimes.guess_all_extensions('application/vnd.geocube+xml',
75 strict=True)
76 self.assertEqual(exts, ['.g3', '.g\xb3'])
77
Fred Drake37918382001-08-16 18:36:59 +000078
Antoine Pitroub8108e22009-11-15 14:25:16 +000079@unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
80class Win32MimeTypesTestCase(unittest.TestCase):
81 def setUp(self):
82 # ensure all entries actually come from the Windows registry
83 self.original_types_map = mimetypes.types_map.copy()
84 mimetypes.types_map.clear()
85 mimetypes.init()
86 self.db = mimetypes.MimeTypes()
87
88 def tearDown(self):
89 # restore default settings
90 mimetypes.types_map.clear()
91 mimetypes.types_map.update(self.original_types_map)
92
93 def test_registry_parsing(self):
94 # the original, minimum contents of the MIME database in the
95 # Windows registry is undocumented AFAIK.
96 # Use file types that should *always* exist:
97 eq = self.assertEqual
98 eq(self.db.guess_type("foo.txt"), ("text/plain", None))
99
100
Fred Drake2e2be372001-09-20 21:33:42 +0000101def test_main():
Antoine Pitroub8108e22009-11-15 14:25:16 +0000102 support.run_unittest(MimeTypesTestCase,
103 Win32MimeTypesTestCase
104 )
Fred Drake2e2be372001-09-20 21:33:42 +0000105
106
107if __name__ == "__main__":
108 test_main()