Back to index

plone3  3.1.7
test_FSImage.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Unit tests for FSImage module.
00014 
00015 $Id: test_FSImage.py 81543 2007-11-05 17:32:33Z tseaver $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from os.path import join as path_join
00022 
00023 from Products.CMFCore.tests.base.dummy import DummyCachingManager
00024 from Products.CMFCore.tests.base.dummy import DummyCachingManagerWithPolicy
00025 from Products.CMFCore.tests.base.testcase import FSDVTest
00026 from Products.CMFCore.tests.base.testcase import RequestTest
00027 
00028 
00029 class FSImageTests(RequestTest, FSDVTest):
00030 
00031     def setUp(self):
00032         FSDVTest.setUp(self)
00033         RequestTest.setUp(self)
00034 
00035     def tearDown(self):
00036         RequestTest.tearDown(self)
00037         FSDVTest.tearDown(self)
00038 
00039     def _makeOne( self, id, filename ):
00040 
00041         from Products.CMFCore.FSImage import FSImage
00042 
00043         return FSImage( id, path_join(self.skin_path_name, filename) )
00044 
00045     def _extractFile( self ):
00046 
00047         path = path_join(self.skin_path_name, 'test_image.gif')
00048         f = open( path, 'rb' )
00049         try:
00050             data = f.read()
00051         finally:
00052             f.close()
00053 
00054         return path, data
00055 
00056     def test_ctor( self ):
00057 
00058         path, ref = self._extractFile()
00059 
00060         image = self._makeOne( 'test_image', 'test_image.gif' )
00061         image = image.__of__( self.root )
00062 
00063         self.assertEqual( image.get_size(), len( ref ) )
00064         self.assertEqual( image._data, ref )
00065 
00066     def test_index_html( self ):
00067 
00068         path, ref = self._extractFile()
00069 
00070         import os
00071         from webdav.common import rfc1123_date
00072 
00073         mod_time = os.stat( path )[ 8 ]
00074 
00075         image = self._makeOne( 'test_image', 'test_image.gif' )
00076         image = image.__of__( self.root )
00077 
00078         data = image.index_html( self.REQUEST, self.RESPONSE )
00079 
00080         self.assertEqual( len( data ), len( ref ) )
00081         self.assertEqual( data, ref )
00082         # ICK!  'HTTPResponse.getHeader' doesn't case-flatten the key!
00083         self.assertEqual( self.RESPONSE.getHeader( 'Content-Length'.lower() )
00084                         , str(len(ref)) )
00085         self.assertEqual( self.RESPONSE.getHeader( 'Content-Type'.lower() )
00086                         , 'image/gif' )
00087         self.assertEqual( self.RESPONSE.getHeader( 'Last-Modified'.lower() )
00088                         , rfc1123_date( mod_time ) )
00089 
00090     def test_index_html_with_304( self ):
00091 
00092         path, ref = self._extractFile()
00093 
00094         import os
00095         from webdav.common import rfc1123_date
00096 
00097         mod_time = os.stat( path )[ 8 ]
00098 
00099         image = self._makeOne( 'test_image', 'test_image.gif' )
00100         image = image.__of__( self.root )
00101 
00102         self.REQUEST.environ[ 'IF_MODIFIED_SINCE'
00103                             ] = '%s;' % rfc1123_date( mod_time+3600 )
00104 
00105         data = image.index_html( self.REQUEST, self.RESPONSE )
00106 
00107         self.assertEqual( data, '' )
00108         # test that we don't supply a content-length
00109         self.assertEqual( self.RESPONSE.getHeader('Content-Length'.lower()),
00110                                                   None )
00111         self.assertEqual( self.RESPONSE.getStatus(), 304 )
00112 
00113     def test_index_html_without_304( self ):
00114 
00115         path, ref = self._extractFile()
00116 
00117         import os
00118         from webdav.common import rfc1123_date
00119 
00120         mod_time = os.stat( path )[ 8 ]
00121 
00122         image = self._makeOne( 'test_image', 'test_image.gif' )
00123         image = image.__of__( self.root )
00124 
00125         self.REQUEST.environ[ 'IF_MODIFIED_SINCE'
00126                             ] = '%s;' % rfc1123_date( mod_time-3600 )
00127 
00128         data = image.index_html( self.REQUEST, self.RESPONSE )
00129 
00130         self.failUnless( data, '' )
00131         self.assertEqual( self.RESPONSE.getStatus(), 200 )
00132 
00133     def test_index_html_with_304_from_cpm( self ):
00134         self.root.caching_policy_manager = DummyCachingManagerWithPolicy()
00135         path, ref = self._extractFile()
00136 
00137         import os
00138         from webdav.common import rfc1123_date
00139         from base.dummy import FAKE_ETAG
00140         
00141         file = self._makeOne( 'test_file', 'test_image.gif' )
00142         file = file.__of__( self.root )
00143 
00144         mod_time = os.stat( path )[ 8 ]
00145 
00146         self.REQUEST.environ[ 'IF_MODIFIED_SINCE'
00147                             ] = '%s;' % rfc1123_date( mod_time )
00148         self.REQUEST.environ[ 'IF_NONE_MATCH'
00149                             ] = '%s;' % FAKE_ETAG
00150 
00151         data = file.index_html( self.REQUEST, self.RESPONSE )
00152         self.assertEqual( len(data), 0 )
00153         self.assertEqual( self.RESPONSE.getStatus(), 304 )
00154 
00155     def test_caching( self ):
00156         self.root.caching_policy_manager = DummyCachingManager()
00157         original_len = len(self.RESPONSE.headers)
00158         image = self._makeOne('test_image', 'test_image.gif')
00159         image = image.__of__(self.root)
00160         image.index_html(self.REQUEST, self.RESPONSE)
00161         headers = self.RESPONSE.headers
00162         self.failUnless(len(headers) >= original_len + 3)
00163         self.failUnless('foo' in headers.keys())
00164         self.failUnless('bar' in headers.keys())
00165         self.assertEqual(headers['test_path'], '/test_image')
00166 
00167     def test_index_html_200_with_cpm( self ):
00168         self.root.caching_policy_manager = DummyCachingManagerWithPolicy()
00169         path, ref = self._extractFile()
00170 
00171         import os
00172         from webdav.common import rfc1123_date
00173         
00174         file = self._makeOne( 'test_file', 'test_image.gif' )
00175         file = file.__of__( self.root )
00176 
00177         mod_time = os.stat( path )[ 8 ]
00178 
00179         data = file.index_html( self.REQUEST, self.RESPONSE )
00180 
00181         # should behave the same as without cpm
00182         self.assertEqual( len( data ), len( ref ) )
00183         self.assertEqual( data, ref )
00184         # ICK!  'HTTPResponse.getHeader' doesn't case-flatten the key!
00185         self.assertEqual( self.RESPONSE.getHeader( 'Content-Length'.lower() )
00186                         , str(len(ref)) )
00187         self.assertEqual( self.RESPONSE.getHeader( 'Content-Type'.lower() )
00188                         , 'image/gif' )
00189         self.assertEqual( self.RESPONSE.getHeader( 'Last-Modified'.lower() )
00190                         , rfc1123_date( mod_time ) )
00191 
00192     def test_index_html_with_304_and_caching( self ):
00193 
00194         # See collector #355
00195         self.root.caching_policy_manager = DummyCachingManager()
00196         original_len = len(self.RESPONSE.headers)
00197         path, ref = self._extractFile()
00198 
00199         import os
00200         from webdav.common import rfc1123_date
00201 
00202         mod_time = os.stat( path )[ 8 ]
00203 
00204         image = self._makeOne( 'test_image', 'test_image.gif' )
00205         image = image.__of__( self.root )
00206 
00207         self.REQUEST.environ[ 'IF_MODIFIED_SINCE'
00208                             ] = '%s;' % rfc1123_date( mod_time+3600 )
00209 
00210         data = image.index_html( self.REQUEST, self.RESPONSE )
00211 
00212         self.assertEqual( data, '' )
00213         self.assertEqual( self.RESPONSE.getStatus(), 304 )
00214 
00215         headers = self.RESPONSE.headers
00216         self.failUnless(len(headers) >= original_len + 3)
00217         self.failUnless('foo' in headers.keys())
00218         self.failUnless('bar' in headers.keys())
00219         self.assertEqual(headers['test_path'], '/test_image')
00220 
00221     def test_tag_with_acquired_clashing_attrs(self):
00222         # See http://www.zope.org/Collectors/CMF/507
00223         class Clash:
00224             def __str__(self):
00225                 raise NotImplementedError
00226 
00227         self.root.alt = Clash()
00228         self.root.height = Clash()
00229         self.root.width = Clash()
00230 
00231         image = self._makeOne( 'test_image', 'test_image.gif' )
00232         image = image.__of__( self.root )
00233 
00234         tag = image.tag()
00235         self.failUnless('alt=""' in tag)
00236 
00237 
00238 def test_suite():
00239     return unittest.TestSuite((
00240         unittest.makeSuite(FSImageTests),
00241         ))
00242 
00243 if __name__ == '__main__':
00244     unittest.main(defaultTest='test_suite')