Caching browser tests
=====================

Setup
-----

  >>> from kotti.resources import get_root
  >>> from kotti import testing
  >>> tools = testing.setUpFunctional()
  >>> browser = tools['Browser']()
  >>> root = get_root()

Some convenience functions:

  >>> import datetime, time
  >>> def parse_expires(date_string):
  ...     return datetime.datetime(*
  ...         (time.strptime(date_string,
  ...          "%a, %d %b %Y %H:%M:%S GMT")[0:6]))
  >>> def delta(date_string):
  ...     now = datetime.datetime.utcnow()
  ...     return parse_expires(date_string) - now

Add a file and an image:

  >>> from os import path
  >>> from kotti import tests
  >>> from kotti.resources import File, Image
  >>> image_path = path.join(path.dirname(tests.__file__), 'sendeschluss.jpg')
  >>> image = open(image_path, 'rb').read()
  >>> root['textfile'] = File("file contents", u"mytext.txt", u"text/plain")
  >>> root['image'] = Image(image, u"sendeschluss.jpg", u"image/jpeg")

---------
Anonymous
---------

Cache HTML
----------

We define different caching policies for different kinds of items.  A
document:

  >>> browser.open(testing.BASE_URL)
  >>> browser.headers['X-Caching-Policy']
  'Cache HTML'
  >>> browser.headers['Cache-Control']
  'max-age=0,s-maxage=3600'
  >>> d = delta(browser.headers['Expires'])
  >>> (d.days, d.seconds) < (0, 0)
  True

Cache Media Content
-------------------

  >>> browser.open(testing.BASE_URL + '/textfile/inline-view')
  >>> browser.headers['X-Caching-Policy']
  'Cache Media Content'
  >>> browser.headers['Cache-Control']
  'max-age=14400'
  >>> d = delta(browser.headers['Expires'])
  >>> (d.days, d.seconds) > (0, 14000)
  True
  >>> browser.open(testing.BASE_URL + '/image/inline-view')
  >>> browser.headers['X-Caching-Policy']
  'Cache Media Content'

Cache Resource
--------------

  >>> browser.open(testing.BASE_URL + '/static-kotti/base.css')
  >>> browser.headers['X-Caching-Policy']
  'Cache Resource'
  >>> browser.headers['Cache-Control']
  'max-age=2764800,public'
  >>> d = delta(browser.headers['Expires'])
  >>> (d.days, d.seconds) > (30, 0)
  True
  >>> 'Last-Modified' in browser.headers
  True

POST request
------------

  >>> browser.post(testing.BASE_URL, '')
  >>> 'X-Caching-Policy' in browser.headers
  False

---------
Logged in
---------

  >>> browser.open(testing.BASE_URL + '/edit')
  >>> "Log in" in browser.contents
  True
  >>> browser.getControl("Username or email").value = "admin"
  >>> browser.getControl("Password").value = "secret"
  >>> browser.getControl(name="submit").click()
  >>> "Welcome, Administrator" in browser.contents
  True

Cache HTML
----------

  >>> browser.open(testing.BASE_URL)
  >>> browser.headers['X-Caching-Policy']
  'No Cache'

Cache Media Content
-------------------

  >>> browser.open(testing.BASE_URL + '/textfile/inline-view')
  >>> browser.headers['X-Caching-Policy']
  'No Cache'
  >>> browser.open(testing.BASE_URL + '/image/inline-view')
  >>> browser.headers['X-Caching-Policy']
  'No Cache'

Cache Resource
--------------

  >>> browser.open(testing.BASE_URL + '/static-kotti/base.css')
  >>> browser.headers['X-Caching-Policy']
  'Cache Resource'

===
404
===

  >>> browser.open(testing.BASE_URL + '/this-isnt-here')
  Traceback (most recent call last):
  HTTPError: HTTP Error 404: Not Found
  >>> 'X-Caching-Policy' in browser.headers
  False
