Debian Codegraph @

cpython27/Lib/test/test_mimetypes.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 import mimetypes
4 import StringIO
5 import unittest
6 import sys
7
8 from test import test_support
9
10 # Tell it we don't know about external files:
11 mimetypes.knownfiles = []
12 mimetypes.inited = False
13 mimetypes._default_mime_types()
14
15
16 class MimeTypesTestCase(unittest.TestCase):
17 def setUp(self):
18 self.db = mimetypes.MimeTypes()
19
20 def test_default_data(self):
21 eq = self.assertEqual
22 eq(self.db.guess_type("foo.html"), ("text/html", None))
23 eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
24 eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
25 eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
26 eq(self.db.guess_type("foo.tar.bz2"), ("application/x-tar", "bzip2"))
27 eq(self.db.guess_type("foo.tar.xz"), ("application/x-tar", "xz"))
28
29 def test_data_urls(self):
30 eq = self.assertEqual
31 guess_type = self.db.guess_type
32 eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
33 eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
34 eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
35
36 def test_file_parsing(self):
37 eq = self.assertEqual
38 sio = StringIO.StringIO("x-application/x-unittest pyunit\n")
39 self.db.readfp(sio)
40 eq(self.db.guess_type("foo.pyunit"),
41 ("x-application/x-unittest", None))
42 eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
43
44 def test_non_standard_types(self):
45 eq = self.assertEqual
46 # First try strict
47 eq(self.db.guess_type('foo.xul', strict=True), (None, None))
48 eq(self.db.guess_extension('image/jpg', strict=True), None)
49 # And then non-strict
50 eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
51 eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
52
53 def test_guess_all_types(self):
54 eq = self.assertEqual
55 unless = self.assertTrue
56 # First try strict. Use a set here for testing the results because if
57 # test_urllib2 is run before test_mimetypes, global state is modified
58 # such that the 'all' set will have more items in it.
59 all = set(self.db.guess_all_extensions('text/plain', strict=True))
60 unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
61 # And now non-strict
62 all = self.db.guess_all_extensions('image/jpg', strict=False)
63 all.sort()
64 eq(all, ['.jpg'])
65 # And now for no hits
66 all = self.db.guess_all_extensions('image/jpg', strict=True)
67 eq(all, [])
68
69
70 @unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
71 class Win32MimeTypesTestCase(unittest.TestCase):
72 def setUp(self):
73 # ensure all entries actually come from the Windows registry
74 self.original_types_map = mimetypes.types_map.copy()
75 mimetypes.types_map.clear()
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 mimetypes.init()
88 db = mimetypes.MimeTypes()
89 eq(db.guess_type("foo.txt"), ("text/plain", None))
90 eq(db.guess_type("image.jpg"), ("image/jpeg", None))
91 eq(db.guess_type("image.png"), ("image/png", None))
92
93 def test_non_latin_extension(self):
94 import _winreg
95
96 class MockWinreg(object):
97 def __getattr__(self, name):
98 if name == 'EnumKey':
99 return lambda key, i: _winreg.EnumKey(key, i) + "\xa3"
100 elif name == 'OpenKey':
101 return lambda key, name: _winreg.OpenKey(key, name.rstrip("\xa3"))
102 elif name == 'QueryValueEx':
103 return lambda subkey, label: (u'текст/простой' , _winreg.REG_SZ)
104 return getattr(_winreg, name)
105
106 mimetypes._winreg = MockWinreg()
107 try:
108 # this used to throw an exception if registry contained non-Latin
109 # characters in extensions (issue #9291)
110 mimetypes.init()
111 finally:
112 mimetypes._winreg = _winreg
113
114 def test_non_latin_type(self):
115 import _winreg
116
117 class MockWinreg(object):
118 def __getattr__(self, name):
119 if name == 'QueryValueEx':
120 return lambda subkey, label: (u'текст/простой', _winreg.REG_SZ)
121 return getattr(_winreg, name)
122
123 mimetypes._winreg = MockWinreg()
124 try:
125 # this used to throw an exception if registry contained non-Latin
126 # characters in content types (issue #9291)
127 mimetypes.init()
128 finally:
129 mimetypes._winreg = _winreg
130
131 def test_type_map_values(self):
132 import _winreg
133
134 class MockWinreg(object):
135 def __getattr__(self, name):
136 if name == 'QueryValueEx':
137 return lambda subkey, label: (u'text/plain', _winreg.REG_SZ)
138 return getattr(_winreg, name)
139
140 mimetypes._winreg = MockWinreg()
141 try:
142 mimetypes.init()
143 self.assertTrue(isinstance(mimetypes.types_map.values()[0], str))
144 finally:
145 mimetypes._winreg = _winreg
146
147 def test_registry_read_error(self):
148 import _winreg
149
150 class MockWinreg(object):
151 def OpenKey(self, key, name):
152 if key != _winreg.HKEY_CLASSES_ROOT:
153 raise WindowsError(5, "Access is denied")
154 return _winreg.OpenKey(key, name)
155 def __getattr__(self, name):
156 return getattr(_winreg, name)
157
158 mimetypes._winreg = MockWinreg()
159 try:
160 mimetypes.init()
161 finally:
162 mimetypes._winreg = _winreg
163
164 def test_main():
165 test_support.run_unittest(MimeTypesTestCase,
166 Win32MimeTypesTestCase
167 )
168
169
170 if __name__ == "__main__":
171 test_main()