Back to index

plone3  3.1.7
test_mimetypes.py
Go to the documentation of this file.
00001 from Testing import ZopeTestCase
00002 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00003 
00004 from Products.MimetypesRegistry.mime_types import text_plain
00005 from Products.MimetypesRegistry.mime_types import text_xml
00006 from Products.MimetypesRegistry.mime_types import application_octet_stream
00007 from utils import input_file_path
00008 
00009 class TestMimeTypesclass(ATSiteTestCase):
00010 
00011     def afterSetUp(self):
00012         ATSiteTestCase.afterSetUp(self)
00013         self.registry = self.portal.mimetypes_registry
00014 
00015     def testClassify(self):
00016         reg = self.registry
00017         c = reg._classifiers()
00018         self.failUnless(c[0].name().startswith("Extensible Markup Language"),
00019                         c[0].name())
00020 
00021         #Real XML
00022         data = "<?xml version='1.0'?><foo>bar</foo>"
00023         mt = reg.classify(data)
00024         self.failUnless(isinstance(mt, text_xml), str(mt))
00025 
00026         # with leading whitespace (http://dev.plone.org/archetypes/ticket/622)
00027         # still valid xml
00028         data = " <?xml version='1.0'?><foo>bar</foo>"
00029         mt = reg.classify(data)
00030         self.failUnless(isinstance(mt, text_xml), str(mt))
00031         
00032         # also #622: this is not xml
00033         data = 'xml > plain text'
00034         mt = reg.classify(data)
00035         self.failUnless(str(mt) != 'text/xml')
00036 
00037         #Passed in MT
00038         mt = reg.classify(data, mimetype="text/plain")
00039         self.failUnless(isinstance(mt, text_plain), str(mt))
00040 
00041         #Passed in filename
00042         mt = reg.classify(data, filename="test.xml")
00043         self.failUnless(isinstance(mt, text_xml), str(mt))
00044         mt = reg.classify(data, filename="test.jpg")
00045         self.failUnlessEqual(str(mt), 'image/jpeg')
00046 
00047         # use xml classifier
00048         mt = reg.classify('<?xml ?>')
00049         self.failUnless(isinstance(mt, text_xml), str(mt))
00050 
00051         # test no data return default
00052         mt = reg.classify('')
00053         self.failUnless(isinstance(mt, text_plain), str(mt))
00054         reg.defaultMimetype = 'text/xml'
00055         mt = reg.classify('')
00056         self.failUnless(isinstance(mt, text_xml), str(mt))
00057 
00058         # test unclassifiable data and no stream flag (filename)
00059         mt = reg.classify('xxx')
00060         self.failUnless(isinstance(mt, text_plain), str(mt))
00061 
00062         # test unclassifiable data and file flag
00063         mt = reg.classify('baz', filename='xxx')
00064         self.failUnless(isinstance(mt, application_octet_stream), str(mt))
00065 
00066     def testExtension(self):
00067         reg = self.registry
00068         data = "<foo>bar</foo>"
00069         mt = reg.lookupExtension(filename="test.xml")
00070         self.failUnless(isinstance(mt, text_xml), str(mt))
00071 
00072         mt = reg.classify(data, filename="test.foo")
00073         self.failUnless(isinstance(mt, application_octet_stream), str(mt))
00074 
00075         mt = reg.classify(data, filename="test.tgz")
00076         self.failUnlessEqual(str(mt), 'application/x-tar')
00077 
00078         mt = reg.classify(data, filename="test.tar.gz")
00079         self.failUnlessEqual(str(mt), 'application/x-tar')
00080 
00081         mt = reg.classify(data, filename="test.pdf.gz")
00082         self.failUnlessEqual(str(mt), 'application/pdf')
00083 
00084     def testFDOGlobs(self):
00085         # The mime types here might only match if they match a glob on
00086         # the freedesktop.org registry.
00087         data = ''
00088         reg = self.registry
00089         mt = reg.classify(data, filename="test.anim1")
00090         self.failUnlessEqual(str(mt), 'video/x-anim')
00091 
00092         mt = reg.classify(data, filename="test.ini~")
00093         self.failUnlessEqual(str(mt), 'application/x-trash')
00094 
00095         mt = reg.classify(data, filename="test.ini%")
00096         self.failUnlessEqual(str(mt), 'application/x-trash')
00097 
00098         mt = reg.classify(data, filename="test.ini.bak")
00099         self.failUnlessEqual(str(mt), 'application/x-trash')
00100 
00101         mt = reg.classify(data, filename="test.f90")
00102         self.failUnlessEqual(str(mt), 'text/x-fortran')
00103 
00104         mt = reg.classify(data, filename="test.f95")
00105         self.failUnlessEqual(str(mt), 'text/x-fortran')
00106 
00107         mt = reg.classify(data, filename="makefile")
00108         self.failUnlessEqual(str(mt), 'text/x-makefile')
00109 
00110         mt = reg.classify(data, filename="Makefile")
00111         self.failUnlessEqual(str(mt), 'text/x-makefile')
00112 
00113         mt = reg.classify(data, filename="makefile.ac")
00114         self.failUnlessEqual(str(mt), 'text/x-makefile')
00115 
00116         mt = reg.classify(data, filename="makefile.in")
00117         self.failUnlessEqual(str(mt), 'text/x-makefile')
00118 
00119         mt = reg.classify(data, filename="AUTHORS")
00120         self.failUnlessEqual(str(mt), 'text/x-authors')
00121 
00122         mt = reg.classify(data, filename="INSTALL")
00123         self.failUnlessEqual(str(mt), 'text/x-install')
00124 
00125     def testLookup(self):
00126         reg = self.registry
00127         mt = reg.lookup('text/plain')
00128         self.failUnless(isinstance(mt[0], text_plain), str(mt[0]))
00129 
00130         # Test lookup of aliases in SMI database (see smi_mimetypes)
00131         mt1 = reg.lookup('application/vnd.wordperfect')
00132         mt2 = reg.lookup('application/wordperfect')
00133         self.assertEqual(mt1, mt2)
00134 
00135         mt = reg.lookup('text/notexistent')
00136         self.failUnlessEqual(mt, ())
00137 
00138     def testAdaptMt(self):
00139         data, filename, mt = self.registry('bar', mimetype='text/xml')
00140         # this test that data has been adaped and file seeked to 0
00141         self.failUnlessEqual(data, 'bar')
00142         self.failUnlessEqual(filename, None)
00143         self.failUnless(isinstance(mt, text_xml), str(mt))
00144 
00145     def testAdaptFile(self):
00146         file = open(input_file_path("rest1.rst"))
00147         data, filename, mt = self.registry(file)
00148         # this test that data has been adaped and file seeked to 0
00149         self.failUnlessEqual(data, file.read())
00150         file.close()
00151         self.failUnlessEqual(filename, "rest1.rst")
00152         self.assertEqual(str(mt), 'text/x-rst')
00153 
00154     def testAdaptData(self):
00155         data, filename, mt = self.registry('<?xml ?>')
00156         # this test that data has been adaped and file seeked to 0
00157         self.failUnlessEqual(data, '<?xml ?>')
00158         self.failUnlessEqual(filename, None)
00159         self.failUnless(isinstance(mt, text_xml), str(mt))
00160 
00161 
00162 def test_suite():
00163     from unittest import TestSuite, makeSuite
00164     suite = TestSuite()
00165     suite.addTest(makeSuite(TestMimeTypesclass))
00166     return suite