importtesttest.prefer_parent_path()importoscurdir=os.path.join(os.getcwd(),os.path.dirname(__file__))importcherrypydefsetup_server():classRoot:_cp_config={'tools.caching.on':True}def__init__(self):cherrypy.counter=0defindex(self):cherrypy.counter+=1msg="visit #%s"%cherrypy.counterreturnmsgindex.exposed=TrueclassUnCached(object):_cp_config={'tools.expires.on':True,'tools.staticdir.on':True,'tools.staticdir.dir':'static','tools.staticdir.root':curdir,}defforce(self):self._cp_config['tools.expires.force']=Truereturn"being forceful"force.exposed=Truedefdynamic(self):cherrypy.response.headers['Cache-Control']='private'return"D-d-d-dynamic!"dynamic.exposed=Truedefcacheable(self):cherrypy.response.headers['Etag']='bibbitybobbityboo'return"Hi, I'm cacheable."cacheable.exposed=Truedefspecific(self):return"I am being specific"specific.exposed=Truespecific._cp_config={'tools.expires.secs':86400}classFoo(object):passdefwrongtype(self):return"Woops"wrongtype.exposed=Truewrongtype._cp_config={'tools.expires.secs':Foo()}cherrypy.tree.mount(Root())cherrypy.tree.mount(UnCached(),"/expires")cherrypy.config.update({'log_to_screen':False,'environment':'production','show_tracebacks':True,})importhelperclassCacheTest(helper.CPWebCase):deftestCaching(self):elapsed=0.0fortrialinxrange(10):self.getPage("/")# The response should be the same every time,# except for the Age response header.self.assertBody('visit #1')iftrial!=0:age=int(self.assertHeader("Age"))self.assert_(age>=elapsed)elapsed=age# POST, PUT, DELETE should not be cached.self.getPage("/",method="POST")self.assertBody('visit #2')self.getPage("/",method="GET")self.assertBody('visit #2')self.getPage("/",method="DELETE")self.assertBody('visit #3')deftestExpiresTool(self):# test setting an expires headerself.getPage("/expires/specific")self.assertStatus("200 OK")self.assertHeader("Expires")# test exceptions for bad time valuesself.getPage("/expires/wrongtype")self.assertStatus(500)self.assertInBody("TypeError")# static content should not have "cache prevention" headersself.getPage("/expires/index.html")self.assertStatus("200 OK")self.assertNoHeader("Pragma")self.assertNoHeader("Cache-Control")# dynamic content that sets indicators should not have# "cache prevention" headersself.getPage("/expires/cacheable")self.assertStatus("200 OK")self.assertNoHeader("Pragma")self.assertNoHeader("Cache-Control")self.getPage('/expires/dynamic')self.assertBody("D-d-d-dynamic!")# the Cache-Control header should be untouchedself.assertHeader("Cache-Control","private")# configure the tool to ignore indicators and replace existing headersself.getPage("/expires/force")self.assertStatus("200 OK")# This also gives us a chance to test 0 expiry with no other headersself.assertHeader("Pragma","no-cache")self.assertHeader("Cache-Control","no-cache")d=self.assertHeader("Date")self.assertHeader("Expires",d)# static content should now have "cache prevention" headersself.getPage("/expires/index.html")self.assertStatus("200 OK")self.assertHeader("Pragma","no-cache")self.assertHeader("Cache-Control","no-cache")d=self.assertHeader("Date")self.assertHeader("Expires",d)# the cacheable handler should now have "cache prevention" headersself.getPage("/expires/cacheable")self.assertStatus("200 OK")self.assertHeader("Pragma","no-cache")self.assertHeader("Cache-Control","no-cache")d=self.assertHeader("Date")self.assertHeader("Expires",d)self.getPage('/expires/dynamic')self.assertBody("D-d-d-dynamic!")# dynamic sets Cache-Control to private but it should be# overwritten here ...self.assertHeader("Pragma","no-cache")self.assertHeader("Cache-Control","no-cache")d=self.assertHeader("Date")self.assertHeader("Expires",d)if__name__=='__main__':setup_server()helper.testmain()