Back to index

plone3  3.1.7
test_FSFile.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 FSFile module.
00014 
00015 $Id: test_FSFile.py 68496 2006-06-05 16:30:18Z yuppie $
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 DummyCachingManagerWithPolicy
00024 from Products.CMFCore.tests.base.dummy import DummyCachingManager
00025 from Products.CMFCore.tests.base.testcase import FSDVTest
00026 from Products.CMFCore.tests.base.testcase import RequestTest
00027 
00028 
00029 class FSFileTests(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.FSFile import FSFile
00042         from Products.CMFCore.FSMetadata import FSMetadata
00043 
00044         full_path = path_join(self.skin_path_name, filename)
00045         metadata = FSMetadata(full_path)
00046         metadata.read()
00047         fsfile_ob = FSFile(id, full_path, properties=metadata.getProperties())
00048 
00049         return fsfile_ob
00050 
00051     def _extractFile( self, filename ):
00052 
00053         path = path_join(self.skin_path_name, filename)
00054         f = open( path, 'rb' )
00055         try:
00056             data = f.read()
00057         finally:
00058             f.close()
00059 
00060         return path, data
00061 
00062     def test_ctor( self ):
00063 
00064         path, ref = self._extractFile('test_file.swf')
00065 
00066         file = self._makeOne( 'test_file', 'test_file.swf' )
00067         file = file.__of__( self.root )
00068 
00069         self.assertEqual( file.get_size(), len( ref ) )
00070         self.assertEqual( file._readFile(0), ref )
00071 
00072     def test_str( self ):
00073         path, ref = self._extractFile('test_file.swf')
00074         file = self._makeOne( 'test_file', 'test_file.swf' )
00075         file = file.__of__( self.root )
00076         self.assertEqual( len(str(file)), len( ref ) )
00077 
00078     def test_index_html( self ):
00079 
00080         path, ref = self._extractFile('test_file.swf')
00081 
00082         import os
00083         from webdav.common import rfc1123_date
00084 
00085         mod_time = os.stat( path )[ 8 ]
00086 
00087         file = self._makeOne( 'test_file', 'test_file.swf' )
00088         file = file.__of__( self.root )
00089 
00090         data = file.index_html( self.REQUEST, self.RESPONSE )
00091 
00092         self.assertEqual( len( data ), len( ref ) )
00093         self.assertEqual( data, ref )
00094         # ICK!  'HTTPResponse.getHeader' doesn't case-flatten the key!
00095         self.assertEqual( self.RESPONSE.getHeader( 'Content-Length'.lower() )
00096                         , str(len(ref)) )
00097         self.assertEqual( self.RESPONSE.getHeader( 'Content-Type'.lower() )
00098                         , 'application/octet-stream' )
00099         self.assertEqual( self.RESPONSE.getHeader( 'Last-Modified'.lower() )
00100                         , rfc1123_date( mod_time ) )
00101 
00102     def test_index_html_with_304( self ):
00103 
00104         path, ref = self._extractFile('test_file.swf')
00105 
00106         import os
00107         from webdav.common import rfc1123_date
00108 
00109         mod_time = os.stat( path )[ 8 ]
00110 
00111         file = self._makeOne( 'test_file', 'test_file.swf' )
00112         file = file.__of__( self.root )
00113 
00114         self.REQUEST.environ[ 'IF_MODIFIED_SINCE'
00115                             ] = '%s;' % rfc1123_date( mod_time+3600 )
00116 
00117         data = file.index_html( self.REQUEST, self.RESPONSE )
00118 
00119         self.assertEqual( data, '' )
00120         # test that we don't supply a content-length
00121         self.assertEqual( self.RESPONSE.getHeader('Content-Length'.lower()),
00122                                                None )
00123         self.assertEqual( self.RESPONSE.getStatus(), 304 )
00124 
00125     def test_index_html_without_304( self ):
00126 
00127         path, ref = self._extractFile('test_file.swf')
00128 
00129         import os
00130         from webdav.common import rfc1123_date
00131 
00132         mod_time = os.stat( path )[ 8 ]
00133 
00134         file = self._makeOne( 'test_file', 'test_file.swf' )
00135         file = file.__of__( self.root )
00136 
00137         self.REQUEST.environ[ 'IF_MODIFIED_SINCE'
00138                             ] = '%s;' % rfc1123_date( mod_time-3600 )
00139 
00140         data = file.index_html( self.REQUEST, self.RESPONSE )
00141 
00142         self.failUnless( data, '' )
00143         self.assertEqual( self.RESPONSE.getStatus(), 200 )
00144 
00145     def test_index_html_with_304_from_cpm( self ):
00146         self.root.caching_policy_manager = DummyCachingManagerWithPolicy()
00147         path, ref = self._extractFile('test_file.swf')
00148 
00149         import os
00150         from webdav.common import rfc1123_date
00151         from base.dummy import FAKE_ETAG
00152         
00153         file = self._makeOne( 'test_file', 'test_file.swf' )
00154         file = file.__of__( self.root )
00155 
00156         mod_time = os.stat( path )[ 8 ]
00157 
00158         self.REQUEST.environ[ 'IF_MODIFIED_SINCE'
00159                             ] = '%s;' % rfc1123_date( mod_time )
00160         self.REQUEST.environ[ 'IF_NONE_MATCH'
00161                             ] = '%s;' % FAKE_ETAG
00162 
00163         data = file.index_html( self.REQUEST, self.RESPONSE )
00164         self.assertEqual( len(data), 0 )
00165         self.assertEqual( self.RESPONSE.getStatus(), 304 )
00166 
00167     def test_index_html_200_with_cpm( self ):
00168         # should behave the same as without cpm installed
00169         self.root.caching_policy_manager = DummyCachingManager()
00170         path, ref = self._extractFile('test_file.swf')
00171 
00172         import os
00173         from webdav.common import rfc1123_date
00174         
00175         file = self._makeOne( 'test_file', 'test_file.swf' )
00176         file = file.__of__( self.root )
00177 
00178         mod_time = os.stat( path )[ 8 ]
00179 
00180         data = file.index_html( self.REQUEST, self.RESPONSE )
00181 
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                         , 'application/octet-stream' )
00189         self.assertEqual( self.RESPONSE.getHeader( 'Last-Modified'.lower() )
00190                         , rfc1123_date( mod_time ) )
00191 
00192     def test_caching( self ):
00193         self.root.caching_policy_manager = DummyCachingManager()
00194         original_len = len(self.RESPONSE.headers)
00195         file = self._makeOne('test_file', 'test_file.swf')
00196         file = file.__of__(self.root)
00197         file.index_html(self.REQUEST, self.RESPONSE)
00198         headers = self.RESPONSE.headers
00199         self.failUnless(len(headers) >= original_len + 3)
00200         self.failUnless('foo' in headers.keys())
00201         self.failUnless('bar' in headers.keys())
00202         self.assertEqual(headers['test_path'], '/test_file')
00203 
00204     def test_forced_content_type( self ):
00205 
00206         path, ref = self._extractFile('test_file_two.swf')
00207 
00208         import os
00209         from webdav.common import rfc1123_date
00210 
00211         mod_time = os.stat( path )[ 8 ]
00212 
00213         file = self._makeOne( 'test_file', 'test_file_two.swf' )
00214         file = file.__of__( self.root )
00215 
00216         data = file.index_html( self.REQUEST, self.RESPONSE )
00217 
00218         self.assertEqual( len( data ), len( ref ) )
00219         self.assertEqual( data, ref )
00220         # ICK!  'HTTPResponse.getHeader' doesn't case-flatten the key!
00221         self.assertEqual( self.RESPONSE.getHeader( 'Content-Length'.lower() )
00222                         , str(len(ref)) )
00223         self.assertEqual( self.RESPONSE.getHeader( 'Content-Type'.lower() )
00224                         , 'application/x-shockwave-flash' )
00225         self.assertEqual( self.RESPONSE.getHeader( 'Last-Modified'.lower() )
00226                         , rfc1123_date( mod_time ) )
00227 
00228     def test_utf8charset_detection( self ):
00229         file_name = 'testUtf8.js'
00230         file = self._makeOne(file_name, file_name)
00231         file = file.__of__(self.root)
00232         data = file.index_html(self.REQUEST, self.RESPONSE)
00233         self.assertEqual(self.RESPONSE.getHeader('content-type'),
00234                          'application/x-javascript; charset=utf-8')
00235 
00236 
00237 def test_suite():
00238     return unittest.TestSuite((
00239         unittest.makeSuite(FSFileTests),
00240         ))
00241 
00242 if __name__ == '__main__':
00243     unittest.main(defaultTest='test_suite')