1import mimetypes
2import StringIO
3import unittest
4import sys
5
6from test import test_support
7
8# Tell it we don't know about external files:
9mimetypes.knownfiles = []
10mimetypes.inited = False
11mimetypes._default_mime_types()
12
13
14class 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        eq(self.db.guess_type("foo.tar.bz2"), ("application/x-tar", "bzip2"))
25        eq(self.db.guess_type("foo.tar.xz"), ("application/x-tar", "xz"))
26
27    def test_data_urls(self):
28        eq = self.assertEqual
29        guess_type = self.db.guess_type
30        eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
31        eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
32        eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
33
34    def test_file_parsing(self):
35        eq = self.assertEqual
36        sio = StringIO.StringIO("x-application/x-unittest pyunit\n")
37        self.db.readfp(sio)
38        eq(self.db.guess_type("foo.pyunit"),
39           ("x-application/x-unittest", None))
40        eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
41
42    def test_non_standard_types(self):
43        eq = self.assertEqual
44        # First try strict
45        eq(self.db.guess_type('foo.xul', strict=True), (None, None))
46        eq(self.db.guess_extension('image/jpg', strict=True), None)
47        # And then non-strict
48        eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
49        eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
50
51    def test_guess_all_types(self):
52        eq = self.assertEqual
53        unless = self.assertTrue
54        # First try strict.  Use a set here for testing the results because if
55        # test_urllib2 is run before test_mimetypes, global state is modified
56        # such that the 'all' set will have more items in it.
57        all = set(self.db.guess_all_extensions('text/plain', strict=True))
58        unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
59        # And now non-strict
60        all = self.db.guess_all_extensions('image/jpg', strict=False)
61        all.sort()
62        eq(all, ['.jpg'])
63        # And now for no hits
64        all = self.db.guess_all_extensions('image/jpg', strict=True)
65        eq(all, [])
66
67
68@unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
69class Win32MimeTypesTestCase(unittest.TestCase):
70    def setUp(self):
71        # ensure all entries actually come from the Windows registry
72        self.original_types_map = mimetypes.types_map.copy()
73        mimetypes.types_map.clear()
74        mimetypes.init()
75        self.db = mimetypes.MimeTypes()
76
77    def tearDown(self):
78        # restore default settings
79        mimetypes.types_map.clear()
80        mimetypes.types_map.update(self.original_types_map)
81
82    def test_registry_parsing(self):
83        # the original, minimum contents of the MIME database in the
84        # Windows registry is undocumented AFAIK.
85        # Use file types that should *always* exist:
86        eq = self.assertEqual
87        eq(self.db.guess_type("foo.txt"), ("text/plain", None))
88
89def test_main():
90    test_support.run_unittest(MimeTypesTestCase,
91        Win32MimeTypesTestCase
92        )
93
94
95if __name__ == "__main__":
96    test_main()
97