PageRenderTime 314ms CodeModel.GetById 191ms app.highlight 8ms RepoModel.GetById 113ms app.codeStats 0ms

/Lib/test/test_mimetypes.py

http://unladen-swallow.googlecode.com/
Python | 70 lines | 49 code | 13 blank | 8 comment | 1 complexity | 01835e46680fa27401ac368d81fbd717 MD5 | raw file
 1import mimetypes
 2import StringIO
 3import unittest
 4
 5from test import test_support
 6
 7# Tell it we don't know about external files:
 8mimetypes.knownfiles = []
 9mimetypes.inited = False
10mimetypes._default_mime_types()
11
12
13class MimeTypesTestCase(unittest.TestCase):
14    def setUp(self):
15        self.db = mimetypes.MimeTypes()
16
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"))
23
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))
30
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")
38
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')
47
48    def test_guess_all_types(self):
49        eq = self.assertEqual
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.
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, [])
63
64
65def test_main():
66    test_support.run_unittest(MimeTypesTestCase)
67
68
69if __name__ == "__main__":
70    test_main()