Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 1 | import mimetypes |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 2 | import io |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 6 | |
| 7 | # Tell it we don't know about external files: |
| 8 | mimetypes.knownfiles = [] |
Jeremy Hylton | 969a700 | 2003-07-18 15:13:37 +0000 | [diff] [blame] | 9 | mimetypes.inited = False |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10 | mimetypes._default_mime_types() |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 11 | |
| 12 | |
| 13 | class MimeTypesTestCase(unittest.TestCase): |
| 14 | def setUp(self): |
| 15 | self.db = mimetypes.MimeTypes() |
| 16 | |
| 17 | def test_default_data(self): |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 18 | eq = self.assertEqual |
| 19 | eq(self.db.guess_type("foo.html"), ("text/html", None)) |
| 20 | eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip")) |
| 21 | eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip")) |
| 22 | eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress")) |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 23 | |
| 24 | def test_data_urls(self): |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 25 | eq = self.assertEqual |
| 26 | guess_type = self.db.guess_type |
| 27 | eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None)) |
| 28 | eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None)) |
| 29 | eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None)) |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 30 | |
| 31 | def test_file_parsing(self): |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 32 | eq = self.assertEqual |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 33 | sio = io.StringIO("x-application/x-unittest pyunit\n") |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 34 | self.db.readfp(sio) |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 35 | eq(self.db.guess_type("foo.pyunit"), |
| 36 | ("x-application/x-unittest", None)) |
| 37 | eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit") |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 38 | |
Barry Warsaw | 107771a | 2001-10-25 21:49:18 +0000 | [diff] [blame] | 39 | def test_non_standard_types(self): |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 40 | eq = self.assertEqual |
Tim Peters | 1633a2e | 2001-10-30 05:56:40 +0000 | [diff] [blame] | 41 | # First try strict |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 42 | eq(self.db.guess_type('foo.xul', strict=True), (None, None)) |
| 43 | eq(self.db.guess_extension('image/jpg', strict=True), None) |
Barry Warsaw | 107771a | 2001-10-25 21:49:18 +0000 | [diff] [blame] | 44 | # And then non-strict |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 45 | eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None)) |
| 46 | eq(self.db.guess_extension('image/jpg', strict=False), '.jpg') |
| 47 | |
| 48 | def test_guess_all_types(self): |
| 49 | eq = self.assertEqual |
Barry Warsaw | ceca5d2 | 2003-11-23 16:21:55 +0000 | [diff] [blame] | 50 | unless = self.failUnless |
| 51 | # First try strict. Use a set here for testing the results because if |
| 52 | # test_urllib2 is run before test_mimetypes, global state is modified |
| 53 | # such that the 'all' set will have more items in it. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 54 | all = set(self.db.guess_all_extensions('text/plain', strict=True)) |
| 55 | unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt'])) |
Barry Warsaw | 9caa0d1 | 2003-06-09 22:27:41 +0000 | [diff] [blame] | 56 | # And now non-strict |
| 57 | all = self.db.guess_all_extensions('image/jpg', strict=False) |
| 58 | all.sort() |
| 59 | eq(all, ['.jpg']) |
| 60 | # And now for no hits |
| 61 | all = self.db.guess_all_extensions('image/jpg', strict=True) |
| 62 | eq(all, []) |
Barry Warsaw | 107771a | 2001-10-25 21:49:18 +0000 | [diff] [blame] | 63 | |
Fred Drake | 3791838 | 2001-08-16 18:36:59 +0000 | [diff] [blame] | 64 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 65 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 66 | support.run_unittest(MimeTypesTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 67 | |
| 68 | |
| 69 | if __name__ == "__main__": |
| 70 | test_main() |