0001 import mimetypes 0002 import StringIO 0003 import unittest 0004 from sets import Set 0005 0006 from test import test_support 0007 0008 # Tell it we don't know about external files: 0009 mimetypes.knownfiles = [] 0010 mimetypes.inited = False 0011 0012 0013 class MimeTypesTestCase(unittest.TestCase): 0014 def setUp(self): 0015 self.db = mimetypes.MimeTypes() 0016 0017 def test_default_data(self): 0018 eq = self.assertEqual 0019 eq(self.db.guess_type("foo.html"), ("text/html", None)) 0020 eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip")) 0021 eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip")) 0022 eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress")) 0023 0024 def test_data_urls(self): 0025 eq = self.assertEqual 0026 guess_type = self.db.guess_type 0027 eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None)) 0028 eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None)) 0029 eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None)) 0030 0031 def test_file_parsing(self): 0032 eq = self.assertEqual 0033 sio = StringIO.StringIO("x-application/x-unittest pyunit\n") 0034 self.db.readfp(sio) 0035 eq(self.db.guess_type("foo.pyunit"), 0036 ("x-application/x-unittest", None)) 0037 eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit") 0038 0039 def test_non_standard_types(self): 0040 eq = self.assertEqual 0041 # First try strict 0042 eq(self.db.guess_type('foo.xul', strict=True), (None, None)) 0043 eq(self.db.guess_extension('image/jpg', strict=True), None) 0044 # And then non-strict 0045 eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None)) 0046 eq(self.db.guess_extension('image/jpg', strict=False), '.jpg') 0047 0048 def test_guess_all_types(self): 0049 eq = self.assertEqual 0050 unless = self.failUnless 0051 # First try strict. Use a set here for testing the results because if 0052 # test_urllib2 is run before test_mimetypes, global state is modified 0053 # such that the 'all' set will have more items in it. 0054 all = Set(self.db.guess_all_extensions('text/plain', strict=True)) 0055 unless(all >= Set(['.bat', '.c', '.h', '.ksh', '.pl', '.txt'])) 0056 # And now non-strict 0057 all = self.db.guess_all_extensions('image/jpg', strict=False) 0058 all.sort() 0059 eq(all, ['.jpg']) 0060 # And now for no hits 0061 all = self.db.guess_all_extensions('image/jpg', strict=True) 0062 eq(all, []) 0063 0064 0065 def test_main(): 0066 test_support.run_unittest(MimeTypesTestCase) 0067 0068 0069 if __name__ == "__main__": 0070 test_main() 0071
Generated by PyXR 0.9.4