blob: 0b53032d39325b476279dfae0e0bd5c049cbe426 [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"))
Serhiy Storchaka59115aa2013-05-04 15:12:55 +030025 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"))
Fred Drake37918382001-08-16 18:36:59 +000027
28 def test_data_urls(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000029 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))
Fred Drake37918382001-08-16 18:36:59 +000034
35 def test_file_parsing(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000036 eq = self.assertEqual
Guido van Rossum34d19282007-08-09 01:03:29 +000037 sio = io.StringIO("x-application/x-unittest pyunit\n")
Fred Drake37918382001-08-16 18:36:59 +000038 self.db.readfp(sio)
Barry Warsaw9caa0d12003-06-09 22:27:41 +000039 eq(self.db.guess_type("foo.pyunit"),
40 ("x-application/x-unittest", None))
41 eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
Fred Drake37918382001-08-16 18:36:59 +000042
Barry Warsaw107771a2001-10-25 21:49:18 +000043 def test_non_standard_types(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000044 eq = self.assertEqual
Tim Peters1633a2e2001-10-30 05:56:40 +000045 # First try strict
Barry Warsaw9caa0d12003-06-09 22:27:41 +000046 eq(self.db.guess_type('foo.xul', strict=True), (None, None))
47 eq(self.db.guess_extension('image/jpg', strict=True), None)
Barry Warsaw107771a2001-10-25 21:49:18 +000048 # And then non-strict
Barry Warsaw9caa0d12003-06-09 22:27:41 +000049 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
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000054 unless = self.assertTrue
Barry Warsawceca5d22003-11-23 16:21:55 +000055 # 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.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058 all = set(self.db.guess_all_extensions('text/plain', strict=True))
59 unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
Barry Warsaw9caa0d12003-06-09 22:27:41 +000060 # 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, [])
Barry Warsaw107771a2001-10-25 21:49:18 +000067
Victor Stinner82ac9bc2011-10-14 03:03:35 +020068 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
Fred Drake37918382001-08-16 18:36:59 +000080
Antoine Pitroub8108e22009-11-15 14:25:16 +000081@unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
82class 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))
Tim Golden27a85642013-10-22 19:27:34 +0100101 eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
102 eq(self.db.guess_type("image.png"), ("image/png", None))
Antoine Pitroub8108e22009-11-15 14:25:16 +0000103
Fred Drake2e2be372001-09-20 21:33:42 +0000104def test_main():
Antoine Pitroub8108e22009-11-15 14:25:16 +0000105 support.run_unittest(MimeTypesTestCase,
106 Win32MimeTypesTestCase
107 )
Fred Drake2e2be372001-09-20 21:33:42 +0000108
109
110if __name__ == "__main__":
111 test_main()