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