blob: 9651a60132c18aa7fcd2a69b82ed63fdf27a251b [file] [log] [blame]
Fred Drake37918382001-08-16 18:36:59 +00001import mimetypes
Guido van Rossum34d19282007-08-09 01:03:29 +00002import io
Fred Drake37918382001-08-16 18:36:59 +00003import unittest
4
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import 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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010mimetypes._default_mime_types()
Fred Drake37918382001-08-16 18:36:59 +000011
12
13class MimeTypesTestCase(unittest.TestCase):
14 def setUp(self):
15 self.db = mimetypes.MimeTypes()
16
17 def test_default_data(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000018 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 Drake37918382001-08-16 18:36:59 +000023
24 def test_data_urls(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000025 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 Drake37918382001-08-16 18:36:59 +000030
31 def test_file_parsing(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000032 eq = self.assertEqual
Guido van Rossum34d19282007-08-09 01:03:29 +000033 sio = io.StringIO("x-application/x-unittest pyunit\n")
Fred Drake37918382001-08-16 18:36:59 +000034 self.db.readfp(sio)
Barry Warsaw9caa0d12003-06-09 22:27:41 +000035 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 Drake37918382001-08-16 18:36:59 +000038
Barry Warsaw107771a2001-10-25 21:49:18 +000039 def test_non_standard_types(self):
Barry Warsaw9caa0d12003-06-09 22:27:41 +000040 eq = self.assertEqual
Tim Peters1633a2e2001-10-30 05:56:40 +000041 # First try strict
Barry Warsaw9caa0d12003-06-09 22:27:41 +000042 eq(self.db.guess_type('foo.xul', strict=True), (None, None))
43 eq(self.db.guess_extension('image/jpg', strict=True), None)
Barry Warsaw107771a2001-10-25 21:49:18 +000044 # And then non-strict
Barry Warsaw9caa0d12003-06-09 22:27:41 +000045 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 Warsawceca5d22003-11-23 16:21:55 +000050 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 Wouters0e3f5912006-08-11 14:57:12 +000054 all = set(self.db.guess_all_extensions('text/plain', strict=True))
55 unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
Barry Warsaw9caa0d12003-06-09 22:27:41 +000056 # 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 Warsaw107771a2001-10-25 21:49:18 +000063
Fred Drake37918382001-08-16 18:36:59 +000064
Fred Drake2e2be372001-09-20 21:33:42 +000065def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000066 support.run_unittest(MimeTypesTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +000067
68
69if __name__ == "__main__":
70 test_main()