Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 1 | import io |
Victor Stinner | 82ac9bc | 2011-10-14 03:03:35 +0200 | [diff] [blame] | 2 | import locale |
| 3 | import mimetypes |
Antoine Pitrou | b8108e2 | 2009-11-15 14:25:16 +0000 | [diff] [blame] | 4 | import sys |
Victor Stinner | 82ac9bc | 2011-10-14 03:03:35 +0200 | [diff] [blame] | 5 | import unittest |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 6 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 8 | |
| 9 | # Tell it we don't know about external files: |
| 10 | mimetypes.knownfiles = [] |
Jeremy Hylton | 969a700 | 2003-07-18 15:13:37 +0000 | [diff] [blame] | 11 | mimetypes.inited = False |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12 | mimetypes._default_mime_types() |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 13 | |
| 14 | |
| 15 | class MimeTypesTestCase(unittest.TestCase): |
| 16 | def setUp(self): |
| 17 | self.db = mimetypes.MimeTypes() |
| 18 | |
| 19 | def test_default_data(self): |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 20 | eq = self.assertEqual |
| 21 | eq(self.db.guess_type("foo.html"), ("text/html", None)) |
| 22 | eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip")) |
| 23 | eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip")) |
| 24 | eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress")) |
Serhiy Storchaka | 59115aa | 2013-05-04 15:12:55 +0300 | [diff] [blame] | 25 | eq(self.db.guess_type("foo.tar.bz2"), ("application/x-tar", "bzip2")) |
| 26 | eq(self.db.guess_type("foo.tar.xz"), ("application/x-tar", "xz")) |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 27 | |
| 28 | def test_data_urls(self): |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 29 | eq = self.assertEqual |
| 30 | guess_type = self.db.guess_type |
| 31 | eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None)) |
| 32 | eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None)) |
| 33 | eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None)) |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 34 | |
| 35 | def test_file_parsing(self): |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 36 | eq = self.assertEqual |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 37 | sio = io.StringIO("x-application/x-unittest pyunit\n") |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 38 | self.db.readfp(sio) |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 39 | eq(self.db.guess_type("foo.pyunit"), |
| 40 | ("x-application/x-unittest", None)) |
| 41 | eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit") |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 42 | |
Barry Warsaw | 107771a | 2001-10-25 21:49:18 +0000 | [diff] [blame] | 43 | def test_non_standard_types(self): |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 44 | eq = self.assertEqual |
Tim Peters | 1633a2e | 2001-10-30 05:56:40 +0000 | [diff] [blame] | 45 | # First try strict |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 46 | eq(self.db.guess_type('foo.xul', strict=True), (None, None)) |
| 47 | eq(self.db.guess_extension('image/jpg', strict=True), None) |
Barry Warsaw | 107771a | 2001-10-25 21:49:18 +0000 | [diff] [blame] | 48 | # And then non-strict |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 49 | eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None)) |
| 50 | eq(self.db.guess_extension('image/jpg', strict=False), '.jpg') |
| 51 | |
| 52 | def test_guess_all_types(self): |
| 53 | eq = self.assertEqual |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 54 | unless = self.assertTrue |
Barry Warsaw | ceca5d2 | 2003-11-23 16:21:55 +0000 | [diff] [blame] | 55 | # First try strict. Use a set here for testing the results because if |
| 56 | # test_urllib2 is run before test_mimetypes, global state is modified |
| 57 | # such that the 'all' set will have more items in it. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 58 | all = set(self.db.guess_all_extensions('text/plain', strict=True)) |
| 59 | unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt'])) |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 60 | # And now non-strict |
| 61 | all = self.db.guess_all_extensions('image/jpg', strict=False) |
| 62 | all.sort() |
| 63 | eq(all, ['.jpg']) |
| 64 | # And now for no hits |
| 65 | all = self.db.guess_all_extensions('image/jpg', strict=True) |
| 66 | eq(all, []) |
Barry Warsaw | 107771a | 2001-10-25 21:49:18 +0000 | [diff] [blame] | 67 | |
Victor Stinner | 82ac9bc | 2011-10-14 03:03:35 +0200 | [diff] [blame] | 68 | def test_encoding(self): |
| 69 | getpreferredencoding = locale.getpreferredencoding |
| 70 | self.addCleanup(setattr, locale, 'getpreferredencoding', |
| 71 | getpreferredencoding) |
| 72 | locale.getpreferredencoding = lambda: 'ascii' |
| 73 | |
| 74 | filename = support.findfile("mime.types") |
| 75 | mimes = mimetypes.MimeTypes([filename]) |
| 76 | exts = mimes.guess_all_extensions('application/vnd.geocube+xml', |
| 77 | strict=True) |
| 78 | self.assertEqual(exts, ['.g3', '.g\xb3']) |
| 79 | |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | b8108e2 | 2009-11-15 14:25:16 +0000 | [diff] [blame] | 81 | @unittest.skipUnless(sys.platform.startswith("win"), "Windows only") |
| 82 | class Win32MimeTypesTestCase(unittest.TestCase): |
| 83 | def setUp(self): |
| 84 | # ensure all entries actually come from the Windows registry |
| 85 | self.original_types_map = mimetypes.types_map.copy() |
| 86 | mimetypes.types_map.clear() |
| 87 | mimetypes.init() |
| 88 | self.db = mimetypes.MimeTypes() |
| 89 | |
| 90 | def tearDown(self): |
| 91 | # restore default settings |
| 92 | mimetypes.types_map.clear() |
| 93 | mimetypes.types_map.update(self.original_types_map) |
| 94 | |
| 95 | def test_registry_parsing(self): |
| 96 | # the original, minimum contents of the MIME database in the |
| 97 | # Windows registry is undocumented AFAIK. |
| 98 | # Use file types that should *always* exist: |
| 99 | eq = self.assertEqual |
| 100 | eq(self.db.guess_type("foo.txt"), ("text/plain", None)) |
Tim Golden | 27a8564 | 2013-10-22 19:27:34 +0100 | [diff] [blame] | 101 | eq(self.db.guess_type("image.jpg"), ("image/jpeg", None)) |
| 102 | eq(self.db.guess_type("image.png"), ("image/png", None)) |
Antoine Pitrou | b8108e2 | 2009-11-15 14:25:16 +0000 | [diff] [blame] | 103 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 104 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame^] | 105 | unittest.main() |