blob: fed0bf581e0479867cd0826af968f89a6a6dcc77 [file] [log] [blame]
Fred Drake37918382001-08-16 18:36:59 +00001import mimetypes
2import StringIO
3import unittest
4
Barry Warsaw04f357c2002-07-23 19:04:11 +00005from test import test_support
Fred Drake37918382001-08-16 18:36:59 +00006
7# Tell it we don't know about external files:
8mimetypes.knownfiles = []
Jeremy Hylton969a7002003-07-18 15:13:37 +00009mimetypes.inited = False
Fred Drake37918382001-08-16 18:36:59 +000010
11
12class MimeTypesTestCase(unittest.TestCase):
13 def setUp(self):
14 self.db = mimetypes.MimeTypes()
15
16 def test_default_data(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000017 eq = self.assertEqual
18 eq(self.db.guess_type("foo.html"), ("text/html", None))
19 eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
20 eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
21 eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
Fred Drake37918382001-08-16 18:36:59 +000022
23 def test_data_urls(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000024 eq = self.assertEqual
25 guess_type = self.db.guess_type
26 eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
27 eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
28 eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
Fred Drake37918382001-08-16 18:36:59 +000029
30 def test_file_parsing(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000031 eq = self.assertEqual
Fred Drake37918382001-08-16 18:36:59 +000032 sio = StringIO.StringIO("x-application/x-unittest pyunit\n")
33 self.db.readfp(sio)
Barry Warsaw9caa0d12003-06-09 22:27:41 +000034 eq(self.db.guess_type("foo.pyunit"),
35 ("x-application/x-unittest", None))
36 eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
Fred Drake37918382001-08-16 18:36:59 +000037
Barry Warsaw107771a2001-10-25 21:49:18 +000038 def test_non_standard_types(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000039 eq = self.assertEqual
Tim Peters1633a2e2001-10-30 05:56:40 +000040 # First try strict
Barry Warsaw9caa0d12003-06-09 22:27:41 +000041 eq(self.db.guess_type('foo.xul', strict=True), (None, None))
42 eq(self.db.guess_extension('image/jpg', strict=True), None)
Barry Warsaw107771a2001-10-25 21:49:18 +000043 # And then non-strict
Barry Warsaw9caa0d12003-06-09 22:27:41 +000044 eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
45 eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
46
47 def test_guess_all_types(self):
48 eq = self.assertEqual
49 # First try strict
50 all = self.db.guess_all_extensions('text/plain', strict=True)
51 all.sort()
52 eq(all, ['.bat', '.c', '.h', '.ksh', '.pl', '.txt'])
53 # And now non-strict
54 all = self.db.guess_all_extensions('image/jpg', strict=False)
55 all.sort()
56 eq(all, ['.jpg'])
57 # And now for no hits
58 all = self.db.guess_all_extensions('image/jpg', strict=True)
59 eq(all, [])
Barry Warsaw107771a2001-10-25 21:49:18 +000060
Fred Drake37918382001-08-16 18:36:59 +000061
Fred Drake2e2be372001-09-20 21:33:42 +000062def test_main():
63 test_support.run_unittest(MimeTypesTestCase)
64
65
66if __name__ == "__main__":
67 test_main()