Issue #7051: Clarify behaviour of 'g' and 'G'-style formatting.
[python.git] / Lib / test / test_mimetypes.py
blobcc1790e0dffa8a0ab1857452f58d0b91c02ba20c
1 import mimetypes
2 import StringIO
3 import unittest
5 from test import test_support
7 # Tell it we don't know about external files:
8 mimetypes.knownfiles = []
9 mimetypes.inited = False
10 mimetypes._default_mime_types()
13 class MimeTypesTestCase(unittest.TestCase):
14 def setUp(self):
15 self.db = mimetypes.MimeTypes()
17 def test_default_data(self):
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"))
24 def test_data_urls(self):
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))
31 def test_file_parsing(self):
32 eq = self.assertEqual
33 sio = StringIO.StringIO("x-application/x-unittest pyunit\n")
34 self.db.readfp(sio)
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")
39 def test_non_standard_types(self):
40 eq = self.assertEqual
41 # First try strict
42 eq(self.db.guess_type('foo.xul', strict=True), (None, None))
43 eq(self.db.guess_extension('image/jpg', strict=True), None)
44 # And then non-strict
45 eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
46 eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
48 def test_guess_all_types(self):
49 eq = self.assertEqual
50 unless = self.assertTrue
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.
54 all = set(self.db.guess_all_extensions('text/plain', strict=True))
55 unless(all >= set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt']))
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, [])
65 def test_main():
66 test_support.run_unittest(MimeTypesTestCase)
69 if __name__ == "__main__":
70 test_main()