Debian Codegraph @

Lib/test/test_mimetypes.py

raw
1 import io
2 import mimetypes
3 import os
4 import shlex
5 import sys
6 import unittest.mock
7 from platform import win32_edition
8 from test import support
9 from test.support import cpython_only, force_not_colorized, os_helper
10 from test.support.import_helper import ensure_lazy_imports
11
12 try:
13 import _winapi
14 except ImportError:
15 _winapi = None
16
17
18 def setUpModule():
19 global knownfiles
20 knownfiles = mimetypes.knownfiles
21
22 # Tell it we don't know about external files:
23 mimetypes.knownfiles = []
24 mimetypes.inited = False
25 mimetypes._default_mime_types()
26
27
28 def tearDownModule():
29 # Restore knownfiles to its initial state
30 mimetypes.knownfiles = knownfiles
31
32
33 class MimeTypesTestCase(unittest.TestCase):
34 def setUp(self):
35 self.db = mimetypes.MimeTypes()
36
37 def test_case_sensitivity(self):
38 eq = self.assertEqual
39 eq(self.db.guess_file_type("foobar.html"), ("text/html", None))
40 eq(self.db.guess_type("scheme:foobar.html"), ("text/html", None))
41 eq(self.db.guess_file_type("foobar.HTML"), ("text/html", None))
42 eq(self.db.guess_type("scheme:foobar.HTML"), ("text/html", None))
43 eq(self.db.guess_file_type("foobar.tgz"), ("application/x-tar", "gzip"))
44 eq(self.db.guess_type("scheme:foobar.tgz"), ("application/x-tar", "gzip"))
45 eq(self.db.guess_file_type("foobar.TGZ"), ("application/x-tar", "gzip"))
46 eq(self.db.guess_type("scheme:foobar.TGZ"), ("application/x-tar", "gzip"))
47 eq(self.db.guess_file_type("foobar.tar.Z"), ("application/x-tar", "compress"))
48 eq(self.db.guess_type("scheme:foobar.tar.Z"), ("application/x-tar", "compress"))
49 eq(self.db.guess_file_type("foobar.tar.z"), (None, None))
50 eq(self.db.guess_type("scheme:foobar.tar.z"), (None, None))
51
52 def test_default_data(self):
53 eq = self.assertEqual
54 eq(self.db.guess_file_type("foo.html"), ("text/html", None))
55 eq(self.db.guess_file_type("foo.HTML"), ("text/html", None))
56 eq(self.db.guess_file_type("foo.tgz"), ("application/x-tar", "gzip"))
57 eq(self.db.guess_file_type("foo.tar.gz"), ("application/x-tar", "gzip"))
58 eq(self.db.guess_file_type("foo.tar.Z"), ("application/x-tar", "compress"))
59 eq(self.db.guess_file_type("foo.tar.bz2"), ("application/x-tar", "bzip2"))
60 eq(self.db.guess_file_type("foo.tar.xz"), ("application/x-tar", "xz"))
61
62 def test_data_urls(self):
63 eq = self.assertEqual
64 guess_type = self.db.guess_type
65 eq(guess_type("data:invalidDataWithoutComma"), (None, None))
66 eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
67 eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
68 eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
69
70 def test_file_parsing(self):
71 eq = self.assertEqual
72 sio = io.StringIO("x-application/x-unittest pyunit\n")
73 self.db.readfp(sio)
74 eq(self.db.guess_file_type("foo.pyunit"),
75 ("x-application/x-unittest", None))
76 eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
77
78 def test_read_mime_types(self):
79 eq = self.assertEqual
80
81 # Unreadable file returns None
82 self.assertIsNone(mimetypes.read_mime_types("non-existent"))
83
84 with os_helper.temp_dir() as directory:
85 data = "x-application/x-unittest pyunit\n"
86 file = os.path.join(directory, "sample.mimetype")
87 with open(file, 'w', encoding="utf-8") as f:
88 f.write(data)
89 mime_dict = mimetypes.read_mime_types(file)
90 eq(mime_dict[".pyunit"], "x-application/x-unittest")
91
92 data = "x-application/x-unittest2 pyunit2\n"
93 file = os.path.join(directory, "sample2.mimetype")
94 with open(file, 'w', encoding="utf-8") as f:
95 f.write(data)
96 mime_dict = mimetypes.read_mime_types(os_helper.FakePath(file))
97 eq(mime_dict[".pyunit2"], "x-application/x-unittest2")
98
99 # bpo-41048: read_mime_types should read the rule file with 'utf-8' encoding.
100 # Not with locale encoding. _bootlocale has been imported because io.open(...)
101 # uses it.
102 data = "application/no-mans-land Fran\u00E7ais"
103 filename = "filename"
104 fp = io.StringIO(data)
105 with unittest.mock.patch.object(mimetypes, 'open',
106 return_value=fp) as mock_open:
107 mime_dict = mimetypes.read_mime_types(filename)
108 mock_open.assert_called_with(filename, encoding='utf-8')
109 eq(mime_dict[".Français"], "application/no-mans-land")
110
111 def test_non_standard_types(self):
112 eq = self.assertEqual
113 # First try strict
114 eq(self.db.guess_file_type('foo.xul', strict=True), (None, None))
115 eq(self.db.guess_extension('image/jpg', strict=True), None)
116 # And then non-strict
117 eq(self.db.guess_file_type('foo.xul', strict=False), ('text/xul', None))
118 eq(self.db.guess_file_type('foo.XUL', strict=False), ('text/xul', None))
119 eq(self.db.guess_file_type('foo.invalid', strict=False), (None, None))
120 eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
121 eq(self.db.guess_extension('image/JPG', strict=False), '.jpg')
122
123 def test_filename_with_url_delimiters(self):
124 # bpo-38449: URL delimiters cases should be handled also.
125 # They would have different mime types if interpreted as URL as
126 # compared to when interpreted as filename because of the semicolon.
127 eq = self.assertEqual
128 gzip_expected = ('application/x-tar', 'gzip')
129 for name in (
130 ';1.tar.gz',
131 '?1.tar.gz',
132 '#1.tar.gz',
133 '#1#.tar.gz',
134 ';1#.tar.gz',
135 ';&1=123;?.tar.gz',
136 '?k1=v1&k2=v2.tar.gz',
137 ):
138 for prefix in ('', '/', '\\',
139 'c:', 'c:/', 'c:\\', 'c:/d/', 'c:\\d\\',
140 '//share/server/', '\\\\share\\server\\'):
141 path = prefix + name
142 with self.subTest(path=path):
143 eq(self.db.guess_file_type(path), gzip_expected)
144 eq(self.db.guess_type(path), gzip_expected)
145 expected = (None, None) if os.name == 'nt' else gzip_expected
146 for prefix in ('//', '\\\\', '//share/', '\\\\share\\'):
147 path = prefix + name
148 with self.subTest(path=path):
149 eq(self.db.guess_file_type(path), expected)
150 eq(self.db.guess_type(path), expected)
151 eq(self.db.guess_file_type(r" \"\`;b&b&c |.tar.gz"), gzip_expected)
152 eq(self.db.guess_type(r" \"\`;b&b&c |.tar.gz"), gzip_expected)
153
154 eq(self.db.guess_file_type(r'foo/.tar.gz'), (None, 'gzip'))
155 eq(self.db.guess_type(r'foo/.tar.gz'), (None, 'gzip'))
156 expected = (None, 'gzip') if os.name == 'nt' else gzip_expected
157 eq(self.db.guess_file_type(r'foo\.tar.gz'), expected)
158 eq(self.db.guess_type(r'foo\.tar.gz'), expected)
159 eq(self.db.guess_type(r'scheme:foo\.tar.gz'), gzip_expected)
160
161 def test_url(self):
162 result = self.db.guess_type('http://example.com/host.html')
163 result = self.db.guess_type('http://host.html')
164 msg = 'URL only has a host name, not a file'
165 self.assertSequenceEqual(result, (None, None), msg)
166 result = self.db.guess_type('http://example.com/host.html')
167 msg = 'Should be text/html'
168 self.assertSequenceEqual(result, ('text/html', None), msg)
169 result = self.db.guess_type('http://example.com/host.html#x.tar')
170 self.assertSequenceEqual(result, ('text/html', None))
171 result = self.db.guess_type('http://example.com/host.html?q=x.tar')
172 self.assertSequenceEqual(result, ('text/html', None))
173
174 def test_guess_all_types(self):
175 # First try strict. Use a set here for testing the results because if
176 # test_urllib2 is run before test_mimetypes, global state is modified
177 # such that the 'all' set will have more items in it.
178 all = self.db.guess_all_extensions('text/plain', strict=True)
179 self.assertTrue(set(all) >= {'.bat', '.c', '.h', '.ksh', '.pl', '.txt'})
180 self.assertEqual(len(set(all)), len(all)) # no duplicates
181 # And now non-strict
182 all = self.db.guess_all_extensions('image/jpg', strict=False)
183 self.assertEqual(all, ['.jpg'])
184 # And now for no hits
185 all = self.db.guess_all_extensions('image/jpg', strict=True)
186 self.assertEqual(all, [])
187 # And now for type existing in both strict and non-strict mappings.
188 self.db.add_type('test-type', '.strict-ext')
189 self.db.add_type('test-type', '.non-strict-ext', strict=False)
190 all = self.db.guess_all_extensions('test-type', strict=False)
191 self.assertEqual(all, ['.strict-ext', '.non-strict-ext'])
192 all = self.db.guess_all_extensions('test-type')
193 self.assertEqual(all, ['.strict-ext'])
194 # Test that changing the result list does not affect the global state
195 all.append('.no-such-ext')
196 all = self.db.guess_all_extensions('test-type')
197 self.assertNotIn('.no-such-ext', all)
198
199 def test_encoding(self):
200 filename = support.findfile("mime.types")
201 mimes = mimetypes.MimeTypes([filename])
202 exts = mimes.guess_all_extensions('application/vnd.geocube+xml',
203 strict=True)
204 self.assertEqual(exts, ['.g3', '.g\xb3'])
205
206 def test_init_reinitializes(self):
207 # Issue 4936: make sure an init starts clean
208 # First, put some poison into the types table
209 mimetypes.add_type('foo/bar', '.foobar')
210 self.assertEqual(mimetypes.guess_extension('foo/bar'), '.foobar')
211 # Reinitialize
212 mimetypes.init()
213 # Poison should be gone.
214 self.assertEqual(mimetypes.guess_extension('foo/bar'), None)
215
216 @unittest.skipIf(sys.platform.startswith("win"), "Non-Windows only")
217 def test_guess_known_extensions(self):
218 # Issue 37529
219 # The test fails on Windows because Windows adds mime types from the Registry
220 # and that creates some duplicates.
221 from mimetypes import types_map
222 for v in types_map.values():
223 self.assertIsNotNone(mimetypes.guess_extension(v))
224
225 def test_preferred_extension(self):
226 def check_extensions():
227 for mime_type, ext in (
228 ("application/epub+zip", ".epub"),
229 ("application/octet-stream", ".bin"),
230 ("application/gzip", ".gz"),
231 ("application/ogg", ".ogx"),
232 ("application/postscript", ".ps"),
233 ("application/vnd.apple.mpegurl", ".m3u"),
234 ("application/vnd.ms-excel", ".xls"),
235 ("application/vnd.ms-fontobject", ".eot"),
236 ("application/vnd.ms-powerpoint", ".ppt"),
237 ("application/vnd.oasis.opendocument.graphics", ".odg"),
238 ("application/vnd.oasis.opendocument.presentation", ".odp"),
239 ("application/vnd.oasis.opendocument.spreadsheet", ".ods"),
240 ("application/vnd.oasis.opendocument.text", ".odt"),
241 ("application/vnd.openxmlformats-officedocument.presentationml.presentation", ".pptx"),
242 ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".xlsx"),
243 ("application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".docx"),
244 ("application/vnd.rar", ".rar"),
245 ("application/x-7z-compressed", ".7z"),
246 ("application/x-debian-package", ".deb"),
247 ("application/x-httpd-php", ".php"),
248 ("application/x-rpm", ".rpm"),
249 ("application/x-texinfo", ".texi"),
250 ("application/x-troff", ".roff"),
251 ("application/xml", ".xsl"),
252 ("application/yaml", ".yaml"),
253 ("audio/flac", ".flac"),
254 ("audio/matroska", ".mka"),
255 ("audio/mp4", ".m4a"),
256 ("audio/mpeg", ".mp3"),
257 ("audio/ogg", ".ogg"),
258 ("audio/vnd.wave", ".wav"),
259 ("audio/webm", ".weba"),
260 ("font/otf", ".otf"),
261 ("font/ttf", ".ttf"),
262 ("font/woff", ".woff"),
263 ("font/woff2", ".woff2"),
264 ("image/avif", ".avif"),
265 ("image/emf", ".emf"),
266 ("image/fits", ".fits"),
267 ("image/g3fax", ".g3"),
268 ("image/jp2", ".jp2"),
269 ("image/jpeg", ".jpg"),
270 ("image/jpm", ".jpm"),
271 ("image/t38", ".t38"),
272 ("image/tiff", ".tiff"),
273 ("image/tiff-fx", ".tfx"),
274 ("image/webp", ".webp"),
275 ("image/wmf", ".wmf"),
276 ("message/rfc822", ".eml"),
277 ("model/gltf+json", ".gltf"),
278 ("model/gltf-binary", ".glb"),
279 ("model/stl", ".stl"),
280 ("text/html", ".html"),
281 ("text/plain", ".txt"),
282 ("text/rtf", ".rtf"),
283 ("text/x-rst", ".rst"),
284 ("video/matroska", ".mkv"),
285 ("video/matroska-3d", ".mk3d"),
286 ("video/mpeg", ".mpeg"),
287 ("video/ogg", ".ogv"),
288 ("video/quicktime", ".mov"),
289 ("video/vnd.avi", ".avi"),
290 ("video/x-m4v", ".m4v"),
291 ("video/x-ms-wmv", ".wmv"),
292 ):
293 with self.subTest(mime_type=mime_type, ext=ext):
294 self.assertEqual(mimetypes.guess_extension(mime_type), ext)
295
296 check_extensions()
297 mimetypes.init()
298 check_extensions()
299
300 def test_guess_file_type(self):
301 def check_file_type():
302 for mime_type, ext in (
303 ("application/yaml", ".yaml"),
304 ("application/yaml", ".yml"),
305 ("audio/mpeg", ".mp2"),
306 ("audio/mpeg", ".mp3"),
307 ("video/mpeg", ".m1v"),
308 ("video/mpeg", ".mpe"),
309 ("video/mpeg", ".mpeg"),
310 ("video/mpeg", ".mpg"),
311 ):
312 with self.subTest(mime_type=mime_type, ext=ext):
313 result, _ = mimetypes.guess_file_type(f"filename{ext}")
314 self.assertEqual(result, mime_type)
315
316 check_file_type()
317 mimetypes.init()
318 check_file_type()
319
320 def test_init_stability(self):
321 mimetypes.init()
322
323 suffix_map = mimetypes.suffix_map
324 encodings_map = mimetypes.encodings_map
325 types_map = mimetypes.types_map
326 common_types = mimetypes.common_types
327
328 mimetypes.init()
329 self.assertIsNot(suffix_map, mimetypes.suffix_map)
330 self.assertIsNot(encodings_map, mimetypes.encodings_map)
331 self.assertIsNot(types_map, mimetypes.types_map)
332 self.assertIsNot(common_types, mimetypes.common_types)
333 self.assertEqual(suffix_map, mimetypes.suffix_map)
334 self.assertEqual(encodings_map, mimetypes.encodings_map)
335 self.assertEqual(types_map, mimetypes.types_map)
336 self.assertEqual(common_types, mimetypes.common_types)
337
338 def test_path_like_ob(self):
339 filename = "LICENSE.txt"
340 filepath = os_helper.FakePath(filename)
341 filepath_with_abs_dir = os_helper.FakePath('/dir/'+filename)
342 filepath_relative = os_helper.FakePath('../dir/'+filename)
343 path_dir = os_helper.FakePath('./')
344
345 expected = self.db.guess_file_type(filename)
346
347 self.assertEqual(self.db.guess_file_type(filepath), expected)
348 self.assertEqual(self.db.guess_type(filepath), expected)
349 self.assertEqual(self.db.guess_file_type(
350 filepath_with_abs_dir), expected)
351 self.assertEqual(self.db.guess_type(
352 filepath_with_abs_dir), expected)
353 self.assertEqual(self.db.guess_file_type(filepath_relative), expected)
354 self.assertEqual(self.db.guess_type(filepath_relative), expected)
355
356 self.assertEqual(self.db.guess_file_type(path_dir), (None, None))
357 self.assertEqual(self.db.guess_type(path_dir), (None, None))
358
359 def test_bytes_path(self):
360 self.assertEqual(self.db.guess_file_type(b'foo.html'),
361 self.db.guess_file_type('foo.html'))
362 self.assertEqual(self.db.guess_file_type(b'foo.tar.gz'),
363 self.db.guess_file_type('foo.tar.gz'))
364 self.assertEqual(self.db.guess_file_type(b'foo.tgz'),
365 self.db.guess_file_type('foo.tgz'))
366
367 def test_keywords_args_api(self):
368 self.assertEqual(self.db.guess_file_type(
369 path="foo.html", strict=True), ("text/html", None))
370 self.assertEqual(self.db.guess_type(
371 url="scheme:foo.html", strict=True), ("text/html", None))
372 self.assertEqual(self.db.guess_all_extensions(
373 type='image/jpg', strict=True), [])
374 self.assertEqual(self.db.guess_extension(
375 type='image/jpg', strict=False), '.jpg')
376
377 def test_added_types_are_used(self):
378 mimetypes.add_type('testing/default-type', '')
379 mime_type, _ = mimetypes.guess_type('')
380 self.assertEqual(mime_type, 'testing/default-type')
381
382 mime_type, _ = mimetypes.guess_type('test.myext')
383 self.assertEqual(mime_type, None)
384
385 mimetypes.add_type('testing/type', '.myext')
386 mime_type, _ = mimetypes.guess_type('test.myext')
387 self.assertEqual(mime_type, 'testing/type')
388
389 def test_add_type_with_undotted_extension_deprecated(self):
390 with self.assertWarns(DeprecationWarning):
391 mimetypes.add_type("testing/type", "undotted")
392
393
394 @unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
395 class Win32MimeTypesTestCase(unittest.TestCase):
396 def setUp(self):
397 # ensure all entries actually come from the Windows registry
398 self.original_types_map = mimetypes.types_map.copy()
399 mimetypes.types_map.clear()
400 mimetypes.init()
401 self.db = mimetypes.MimeTypes()
402
403 def tearDown(self):
404 # restore default settings
405 mimetypes.types_map.clear()
406 mimetypes.types_map.update(self.original_types_map)
407
408 @unittest.skipIf(win32_edition() in ('NanoServer', 'WindowsCoreHeadless', 'IoTEdgeOS'),
409 "MIME types registry keys unavailable")
410 def test_registry_parsing(self):
411 # the original, minimum contents of the MIME database in the
412 # Windows registry is undocumented AFAIK.
413 # Use file types that should *always* exist:
414 eq = self.assertEqual
415 eq(self.db.guess_type("foo.txt"), ("text/plain", None))
416 eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
417 eq(self.db.guess_type("image.png"), ("image/png", None))
418
419 @unittest.skipIf(not hasattr(_winapi, "_mimetypes_read_windows_registry"),
420 "read_windows_registry accelerator unavailable")
421 def test_registry_accelerator(self):
422 from_accel = {}
423 from_reg = {}
424 _winapi._mimetypes_read_windows_registry(
425 lambda v, k: from_accel.setdefault(k, set()).add(v)
426 )
427 mimetypes.MimeTypes._read_windows_registry(
428 lambda v, k: from_reg.setdefault(k, set()).add(v)
429 )
430 self.assertEqual(list(from_reg), list(from_accel))
431 for k in from_reg:
432 self.assertEqual(from_reg[k], from_accel[k])
433
434
435 class MiscTestCase(unittest.TestCase):
436 def test__all__(self):
437 support.check__all__(self, mimetypes)
438
439 @cpython_only
440 def test_lazy_import(self):
441 ensure_lazy_imports("mimetypes", {"os", "posixpath", "urllib.parse", "argparse"})
442
443
444 class CommandLineTest(unittest.TestCase):
445 @force_not_colorized
446 def test_parse_args(self):
447 args, help_text = mimetypes._parse_args("-h")
448 self.assertTrue(help_text.startswith("usage: "))
449
450 args, help_text = mimetypes._parse_args("--invalid")
451 self.assertTrue(help_text.startswith("usage: "))
452
453 args, _ = mimetypes._parse_args(shlex.split("-l -e image/jpg"))
454 self.assertTrue(args.extension)
455 self.assertTrue(args.lenient)
456 self.assertEqual(args.type, ["image/jpg"])
457
458 args, _ = mimetypes._parse_args(shlex.split("-e image/jpg"))
459 self.assertTrue(args.extension)
460 self.assertFalse(args.lenient)
461 self.assertEqual(args.type, ["image/jpg"])
462
463 args, _ = mimetypes._parse_args(shlex.split("-l foo.webp"))
464 self.assertFalse(args.extension)
465 self.assertTrue(args.lenient)
466 self.assertEqual(args.type, ["foo.webp"])
467
468 args, _ = mimetypes._parse_args(shlex.split("foo.pic"))
469 self.assertFalse(args.extension)
470 self.assertFalse(args.lenient)
471 self.assertEqual(args.type, ["foo.pic"])
472
473 def test_multiple_inputs(self):
474 result = "\n".join(mimetypes._main(shlex.split("foo.pdf foo.png")))
475 self.assertEqual(
476 result,
477 "type: application/pdf encoding: None\n"
478 "type: image/png encoding: None"
479 )
480
481 def test_multiple_inputs_error(self):
482 result = "\n".join(mimetypes._main(shlex.split("foo.pdf foo.bar_ext")))
483 self.assertEqual(
484 result,
485 "type: application/pdf encoding: None\n"
486 "error: media type unknown for foo.bar_ext"
487 )
488
489
490 def test_invocation(self):
491 for command, expected in [
492 ("-l -e image/jpg", ".jpg"),
493 ("-e image/jpeg", ".jpg"),
494 ("-l foo.webp", "type: image/webp encoding: None"),
495 ]:
496 result = "\n".join(mimetypes._main(shlex.split(command)))
497 self.assertEqual(result, expected)
498
499 def test_invocation_error(self):
500 for command, expected in [
501 ("-e image/jpg", "error: unknown type image/jpg"),
502 ("foo.bar_ext", "error: media type unknown for foo.bar_ext"),
503 ]:
504 with self.subTest(command=command):
505 result = "\n".join(mimetypes._main(shlex.split(command)))
506 self.assertEqual(result, expected)
507
508
509 if __name__ == "__main__":
510 unittest.main()