Back to index

plone3  3.1.7
testHTTPHeaders.py
Go to the documentation of this file.
00001 #
00002 # CSSRegistry Tests
00003 #
00004 
00005 from App.Common import rfc1123_date
00006 from DateTime import DateTime
00007 from AccessControl import Unauthorized
00008 from Interface.Verify import verifyObject
00009 
00010 from Products.CMFCore.utils import getToolByName
00011 
00012 from Products.PloneTestCase.PloneTestCase import PLONE21
00013 
00014 from Products.ResourceRegistries.config import CSSTOOLNAME
00015 from Products.ResourceRegistries.interfaces import ICSSRegistry
00016 from Products.ResourceRegistries.tests.RegistryTestCase import FunctionalRegistryTestCase
00017 
00018 class TestHTTPHeaders(FunctionalRegistryTestCase):
00019 
00020     def afterSetUp(self):
00021         self.tool = getattr(self.portal, CSSTOOLNAME)
00022         self.tool.clearResources()
00023         self.toolpath = '/' + self.tool.absolute_url(1)
00024         self.portalpath = '/' + getToolByName(self.portal, 'portal_url')(1)
00025 
00026     def testContentTypeHeaders(self):
00027         # Test that the main page retains its content-type
00028         self.setRoles(['Manager'])
00029         # we make a method that tries to set its own Content-Type headers
00030         self.portal.addDTMLMethod('testmethod', file="""<dtml-call "REQUEST.RESPONSE.setHeader('Content-Type', 'text/plain')">""")
00031         self.tool.registerStylesheet('testmethod')
00032         response = self.publish(self.toolpath+'/testmethod')
00033         self.assertEqual(response.getHeader('Content-Type'), 'text/css;charset=utf-8')
00034         self.assertEqual(response.getStatus(), 200)
00035 
00036     def testIfModifiedSinceHeaders(self):
00037         # Test that the main page returns the proper status code and content type for a conditional get
00038         self.setRoles(['Manager'])
00039         request = self.portal.REQUEST
00040         request.environ['IF_MODIFIED_SINCE'] = rfc1123_date((DateTime() - 60.0/(24.0*3600.0))) # resend if modified since one minute ago (assumes client clock is a little slow)
00041         assert request.get_header('If-Modified-Since')
00042         self.portal.addDTMLMethod('testmethod', file="""/* YES WE ARE RENDERED */""")
00043         self.tool.registerStylesheet('testmethod')
00044         response = self.publish(self.toolpath+'/testmethod', env={'IF_MODIFIED_SINCE': rfc1123_date((DateTime() - 60.0/(24.0*3600.0)))})
00045         #response = self.publish(self.toolpath+'/testmethod')
00046         self.assertEqual(response.getHeader('Content-Type'), 'text/css;charset=utf-8')
00047         self.assertEqual(response.getStatus(), 200) # this should in fact send a 200
00048 
00049         # we also add an fsfile for good measure
00050         self.tool.registerStylesheet('test_rr_2.css')
00051         rs = self.tool.getEvaluatedResources(self.portal)
00052         response = self.publish(self.toolpath+'/test_rr_2.css', env={'IF_MODIFIED_SINCE': rfc1123_date((DateTime() - 60.0/(24.0*3600.0)))})
00053         self.assertEqual(response.getStatus(), 200)  # this should send a 200 when things are fixed, but right now should send a 304
00054         self.assertEqual(response.getHeader('Content-Type'), 'text/css;charset=utf-8')
00055 
00056         #response = self.publish(self.toolpath+'/test_rr_2.css')
00057         #self.assertEqual(response.getStatus(), 200)  # this should send a 200 when things are fixed, but right now should send a 304
00058 
00059 
00060     def testContentLengthHeaders(self):
00061         # Test that the main page retains its content-type
00062         self.setRoles(['Manager'])
00063 
00064         request = self.portal.REQUEST
00065         request.environ['IF_MODIFIED_SINCE'] = rfc1123_date((DateTime() - 60.0/(24.0*3600.0))) # if modified in the last minute
00066         #print self.portal.REQUEST.get_header('If-Modified-Since')
00067         self.tool.registerStylesheet('test_rr_1.css')
00068         self.portal.addDTMLMethod('testmethod', file="""/* YES WE ARE RENDERED */""")
00069         self.tool.registerStylesheet('testmethod')
00070         self.portal.addDTMLMethod('testmethod2', file="""/* YES WE ARE RENDERED 2 */""")
00071         self.tool.registerStylesheet('testmethod2')
00072         # we also add an fsfile for good measure
00073         self.tool.registerStylesheet('test_rr_2.css')
00074         rs = self.tool.getEvaluatedResources(self.portal)
00075         self.assertEqual(len(rs),1)
00076         response = self.publish(self.toolpath+'/'+rs[0].getId())
00077         self.assertEqual(response.getHeader('Content-Type'), 'text/css;charset=utf-8')
00078         #print str(response)
00079         self.assertEqual(int(response.getHeader('content-length')), len(response.getBody()))
00080         self.assertEqual(response.getStatus(), 200)
00081 
00082 
00083 def test_suite():
00084     from unittest import TestSuite, makeSuite
00085     suite = TestSuite()
00086     suite.addTest(makeSuite(TestHTTPHeaders))
00087 
00088     return suite