web/test/unittest_views_staticcontrollers.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 23 Aug 2012 11:43:43 +0200
branchstable
changeset 8532 28a346df670e
parent 8396 8d58fcf68539
child 8694 d901c36bcfce
permissions -rw-r--r--
[cwctl] show a better diagnostic message on version violation (closes #2464800)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
from __future__ import with_statement
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
8323
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
     3
from logilab.common.testlib import tag, Tags
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
from cubicweb.devtools.testlib import CubicWebTC
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
import os
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
import os.path as osp
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
import glob
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
from cubicweb.utils import HTMLHead
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
from cubicweb.web.views.staticcontrollers import ConcatFilesHandler
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
8323
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    13
class StaticControllerCacheTC(CubicWebTC):
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    14
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    15
    tags = CubicWebTC.tags | Tags('static_controller', 'cache', 'http')
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    16
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    17
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    18
    def _publish_static_files(self, url, header={}):
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    19
        req = self.request(headers=header)
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    20
        req._url = url
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    21
        return self.app_handle_request(req, url), req
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    22
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    23
    def test_static_file_are_cached(self):
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    24
        _, req = self._publish_static_files('data/cubicweb.css')
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    25
        self.assertEqual(200, req.status_out)
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    26
        self.assertIn('last-modified', req.headers_out)
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    27
        next_headers = {
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    28
            'if-modified-since': req.get_response_header('last-modified', raw=True),
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    29
        }
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    30
        _, req = self._publish_static_files('data/cubicweb.css', next_headers)
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    31
        self.assertEqual(304, req.status_out)
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    32
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    33
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
class ConcatFilesTC(CubicWebTC):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
8323
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    36
    tags = CubicWebTC.tags | Tags('static_controller', 'concat')
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    37
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
    def tearDown(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
        super(ConcatFilesTC, self).tearDown()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
        self._cleanup_concat_cache()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    def _cleanup_concat_cache(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
        uicachedir = osp.join(self.config.apphome, 'uicache')
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
        for fname in glob.glob(osp.join(uicachedir, 'cache_concat_*')):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
            os.unlink(osp.join(uicachedir, fname))
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
    def _publish_js_files(self, js_files):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        req = self.request()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
        head = HTMLHead(req)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
        url = head.concat_urls([req.data_url(js_file) for js_file in js_files])[len(req.base_url()):]
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        req._url = url
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    52
        return self.app_handle_request(req, url), req
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
    def expected_content(self, js_files):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
        content = u''
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        for js_file in js_files:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
            dirpath, rid = self.config.locate_resource(js_file)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
            if dirpath is not None: # ignore resources not found
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
                with open(osp.join(dirpath, rid)) as f:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
                    content += f.read() + '\n'
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
        return content
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
    def test_cache(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
        js_files = ('cubicweb.ajax.js', 'jquery.js')
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    65
        result, req = self._publish_js_files(js_files)
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    66
        self.assertNotEqual(404, req.status_out)
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
        # check result content
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
        self.assertEqual(result, self.expected_content(js_files))
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
        # make sure we kept a cached version on filesystem
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
        concat_hander = ConcatFilesHandler(self.config)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
        filepath = concat_hander.build_filepath(js_files)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        self.assertTrue(osp.isfile(filepath))
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
    def test_invalid_file_in_debug_mode(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
        js_files = ('cubicweb.ajax.js', 'dummy.js')
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
        # in debug mode, an error is raised
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
        self.config.debugmode = True
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
        try:
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    80
            result, req = self._publish_js_files(js_files)
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    81
            #print result
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    82
            self.assertEqual(404, req.status_out)
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
        finally:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
            self.config.debugmode = False
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
    def test_invalid_file_in_production_mode(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
        js_files = ('cubicweb.ajax.js', 'dummy.js')
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    88
        result, req = self._publish_js_files(js_files)
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    89
        self.assertNotEqual(404, req.status_out)
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
        # check result content
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
        self.assertEqual(result, self.expected_content(js_files))
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
if __name__ == '__main__':
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
    from logilab.common.testlib import unittest_main
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
    unittest_main()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97