利用cookie 测试浏览器的缓存机制

来源:互联网 发布:asp网络程序员招聘 编辑:程序博客网 时间:2024/06/06 10:41

利用是否去请求时,至少response header是会存在请求不请求的区别

这样的话,如果去请求的时候,就带上返回新的cookie

然后和老的cookie比较

如果是跟新了,说明,没有去读cache,而是发送请求了


例子测试页面:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><script src="http://10.1.146.174/case/js/jquery.js" type="text/javascript"></script><script src="http://10.1.146.174/case/js/browsertool.js" type="text/javascript"></script><script src="http://10.1.146.174/case/js/assert.js" type="text/javascript"></script><script src="http://10.1.146.174/case/JS/BlueFairyCTS.js" type="text/javascript"></script><script src="http://10.1.146.174/case/JS/BlueFairyCTS_Report.js" type="text/javascript"></script</head><body><a>response is max-age=10, so when request this url in the new window in 10 seconds, browser should not send request to server, but local cache</a><iframe id="testpage" src="http://10.1.146.174/service/cachecontrol.py?method=maxage" height="100%" width="100%"></iframe><script type="text/javascript">$("#testpage").load(function(){var ckie = getcookie('time');if(ckie==''){setCookie('time', '2', false, false, false, false); //differce between 2 times,setTimeout("window.open('http://10.1.146.174/case/front/cache/testcache011-maxage-newwindow.html', '_blank');",2000);var rs = new CloseBrowser("","",5,null);rs.Call();//setTimeout("document.execCommand('Refresh');",2000);}else if(ckie=='2'){var content = parent.frames['testpage'].document.body.innerText;assertEquals('maxage1 ',content,'expect the content is maxage1,if request,the server will return maxage2');var ckie1 = getcookie('maxage')assertNotEquals('2',ckie1,'if not request server , the cookie should still be 1');var casename = "testcachemaxage011";var casetype = "CACHE";var description = "test browser open a new window to request this url when server return cache-control: no-cache, and browser will request the server and load new resource";delcookie('time');var debugInfo = ListToStr(ASSERTLIST);var add = new AddTestResult("",casetype,casename,RESULT,description,debugInfo,null);add.Call();var rs = new CloseBrowser("","",5,null);rs.Call();}});</script></body></html>


服务器(用python cgi 实现的,其实我认为用jsp之类可以直接在页面内嵌代码的方式会更好):

def maxage():try:if(os.environ.has_key('HTTP_COOKIE')):if(os.environ['HTTP_COOKIE'].find('request=first') >= 0):logger.info('maxage: has cookie maxage=1')ckie = Cookie.SimpleCookie()ckie['request']='first'ckie['maxage']='2'ckie['maxage']['path']='/case'print('Status: 200 OK')print('Content-Type: text/html')print('Cache-Control: max-age=10')print(ckie)print('')print('maxage2')returnlogger.info('maxage: has no cookie')ckie = Cookie.SimpleCookie()ckie['request']='first'ckie['maxage']='1'ckie['maxage']['path']='/case'print('Status: 200 OK')print('Content-Type: text/html')print('Cache-Control: max-age=10')print(ckie)print('')print('maxage1')returnexcept:logger.error(sys.exc_info())return httplib.error()




原创粉丝点击