ยปCore Development>Code coverage>Lib/test/test_mimetypes.py

Python code coverage for Lib/test/test_mimetypes.py

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