web/test/unittest_views_staticcontrollers.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Tue, 25 Jun 2013 11:06:57 +0200
changeset 9052 4cba5f2cd57b
parent 8694 d901c36bcfce
child 9233 7d3316bfa4ff
permissions -rw-r--r--
[repoapi] introduce a basic ClientConnection class This is the new official way to access the repo from client side. It still access Session object directly as the server side connection is not up yet (and it's not up because it would have no user). Multiple follow up commit will install compatibility with the DBAPI. This will ease the migration from dbapi to repoapi. ClientConnection has no user yet but later commit will use it in the whole Web stack. related to #2503918

from logilab.common.testlib import tag, Tags
from cubicweb.devtools.testlib import CubicWebTC

import os
import os.path as osp
import glob

from cubicweb.utils import HTMLHead
from cubicweb.web.views.staticcontrollers import ConcatFilesHandler

class StaticControllerCacheTC(CubicWebTC):

    tags = CubicWebTC.tags | Tags('static_controller', 'cache', 'http')


    def _publish_static_files(self, url, header={}):
        req = self.request(headers=header)
        req._url = url
        return self.app_handle_request(req, url), req

    def test_static_file_are_cached(self):
        _, req = self._publish_static_files('data/cubicweb.css')
        self.assertEqual(200, req.status_out)
        self.assertIn('last-modified', req.headers_out)
        next_headers = {
            'if-modified-since': req.get_response_header('last-modified', raw=True),
        }
        _, req = self._publish_static_files('data/cubicweb.css', next_headers)
        self.assertEqual(304, req.status_out)


class ConcatFilesTC(CubicWebTC):

    tags = CubicWebTC.tags | Tags('static_controller', 'concat')

    def tearDown(self):
        super(ConcatFilesTC, self).tearDown()
        self._cleanup_concat_cache()

    def _cleanup_concat_cache(self):
        uicachedir = osp.join(self.config.apphome, 'uicache')
        for fname in glob.glob(osp.join(uicachedir, 'cache_concat_*')):
            os.unlink(osp.join(uicachedir, fname))

    def _publish_js_files(self, js_files):
        req = self.request()
        head = HTMLHead(req)
        url = head.concat_urls([req.data_url(js_file) for js_file in js_files])[len(req.base_url()):]
        req._url = url
        return self.app_handle_request(req, url), req

    def expected_content(self, js_files):
        content = u''
        for js_file in js_files:
            dirpath, rid = self.config.locate_resource(js_file)
            if dirpath is not None: # ignore resources not found
                with open(osp.join(dirpath, rid)) as f:
                    content += f.read() + '\n'
        return content

    def test_cache(self):
        js_files = ('cubicweb.ajax.js', 'jquery.js')
        result, req = self._publish_js_files(js_files)
        self.assertNotEqual(404, req.status_out)
        # check result content
        self.assertEqual(result, self.expected_content(js_files))
        # make sure we kept a cached version on filesystem
        concat_hander = ConcatFilesHandler(self.config)
        filepath = concat_hander.build_filepath(js_files)
        self.assertTrue(osp.isfile(filepath))


    def test_invalid_file_in_debug_mode(self):
        js_files = ('cubicweb.ajax.js', 'dummy.js')
        # in debug mode, an error is raised
        self.config.debugmode = True
        try:
            result, req = self._publish_js_files(js_files)
            #print result
            self.assertEqual(404, req.status_out)
        finally:
            self.config.debugmode = False

    def test_invalid_file_in_production_mode(self):
        js_files = ('cubicweb.ajax.js', 'dummy.js')
        result, req = self._publish_js_files(js_files)
        self.assertNotEqual(404, req.status_out)
        # check result content
        self.assertEqual(result, self.expected_content(js_files))


if __name__ == '__main__':
    from logilab.common.testlib import unittest_main
    unittest_main()