PageRenderTime 112ms CodeModel.GetById 104ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/web/test_expires.py

https://bitbucket.org/prologic/circuits/
Python | 48 lines | 32 code | 15 blank | 1 comment | 0 complexity | 079e9efb1e6a3d563534e522cd69f949 MD5 | raw file
 1#!/usr/bin/env python
 2
 3from datetime import datetime
 4from time import mktime
 5from email.utils import parsedate
 6
 7from circuits.web import Controller
 8
 9from .helpers import urlopen
10
11
12class Root(Controller):
13
14    def index(self):
15        self.expires(60)
16        return "Hello World!"
17
18    def nocache(self):
19        self.expires(0)
20        return "Hello World!"
21
22
23def test(webapp):
24    f = urlopen(webapp.server.http.base)
25    s = f.read()
26    assert s == b"Hello World!"
27    expires = f.headers["Expires"]
28    diff = (mktime(parsedate(expires)) - mktime(datetime.utcnow().timetuple()))
29    assert 60 - (60 * 0.1) < diff < 60 + (60 * 0.1)  # diff is about 60 +- 10%
30
31
32def test_nocache(webapp):
33    f = urlopen("%s/nocache" % webapp.server.http.base)
34    s = f.read()
35    assert s == b"Hello World!"
36
37    expires = f.headers["Expires"]
38    pragma = f.headers["Pragma"]
39    cacheControl = f.headers["Cache-Control"]
40
41    now = datetime.utcnow()
42    lastyear = now.replace(year=now.year-1)
43
44    diff = (mktime(parsedate(expires)) - mktime(lastyear.utctimetuple()))
45    assert diff < 1.0
46
47    assert pragma == "no-cache"
48    assert cacheControl == "no-cache, must-revalidate"