blob: 2caa7aa21d69abd898053f435aba223a9f018e8c [file] [log] [blame]
Fred Drake37918382001-08-16 18:36:59 +00001import mimetypes
Guido van Rossum34d19282007-08-09 01:03:29 +00002import io
Fred Drake37918382001-08-16 18:36:59 +00003import unittest
Antoine Pitroub8108e22009-11-15 14:25:16 +00004import sys
Fred Drake37918382001-08-16 18:36:59 +00005
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Fred Drake37918382001-08-16 18:36:59 +00007
8# Tell it we don't know about external files:
9mimetypes.knownfiles = []
Jeremy Hylton969a7002003-07-18 15:13:37 +000010mimetypes.inited = False
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011mimetypes._default_mime_types()
Fred Drake37918382001-08-16 18:36:59 +000012
13
14class MimeTypesTestCase(unittest.TestCase):
15 def setUp(self):
16 self.db = mimetypes.MimeTypes()
17
18 def test_default_data(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000019 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"))
Fred Drake37918382001-08-16 18:36:59 +000024
25 def test_data_urls(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000026 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))
Fred Drake37918382001-08-16 18:36:59 +000031
32 def test_file_parsing(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000033 eq = self.assertEqual
Guido van Rossum34d19282007-08-09 01:03:29 +000034 sio = io.StringIO("x-application/x-unittest pyunit\n")
Fred Drake37918382001-08-16 18:36:59 +000035 self.db.readfp(sio)
Barry Warsaw9caa0d12003-06-09 22:27:41 +000036 eq(self.db.guess_type("foo.pyunit"),
37 ("x-application/x-unittest", None))
38 eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
Fred Drake37918382001-08-16 18:36:59 +000039
Barry Warsaw107771a2001-10-25 21:49:18 +000040 def test_non_standard_types(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000041 eq = self.assertEqual
Tim Peters1633a2e2001-10-30 05:56:40 +000042 # First try strict
Barry Warsaw9caa0d12003-06-09 22:27:41 +000043 eq(self.db.guess_type('foo.xul', strict=True), (None, None))
44 eq(self.db.guess_extension('image/jpg', strict=True), None)
Barry Warsaw107771a2001-10-25 21:49:18 +000045 # And then non-strict
Barry Warsaw9caa0d12003-06-09 22:27:41 +000046 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
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000051 unless = self.assertTrue
Barry Warsawceca5d22003-11-23 16:21:55 +000052 # 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.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000055 all = set(self.db.guess_all_extensions('text/plain', strict=True))
56 unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
Barry Warsaw9caa0d12003-06-09 22:27:41 +000057 # 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, [])
Barry Warsaw107771a2001-10-25 21:49:18 +000064
Fred Drake37918382001-08-16 18:36:59 +000065
Antoine Pitroub8108e22009-11-15 14:25:16 +000066@unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
67class 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
Fred Drake2e2be372001-09-20 21:33:42 +000088def test_main():
Antoine Pitroub8108e22009-11-15 14:25:16 +000089 support.run_unittest(MimeTypesTestCase,
90 Win32MimeTypesTestCase
91 )
Fred Drake2e2be372001-09-20 21:33:42 +000092
93
94if __name__ == "__main__":
95 test_main()