web/test/unittest_views_staticcontrollers.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Mon, 02 Jun 2014 18:31:09 +0200
changeset 9812 122c0b6a82a3
parent 9233 7d3316bfa4ff
child 10398 bd00b58b00d9
permissions -rw-r--r--
[webtests/views_staticcontrollers] use the new connection api
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     1
# -*- coding: utf-8 -*-
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     2
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     4
#
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     5
# This file is part of CubicWeb.
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     6
#
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    10
# any later version.
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    11
#
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    15
# details.
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    16
#
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    19
from contextlib import contextmanager
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    20
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    21
from logilab.common import tempattr
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    22
from logilab.common.testlib import Tags
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
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
    24
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
import os
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
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
    27
import glob
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
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
    30
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
    31
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    32
class staticfilespublishermixin(object):
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
    33
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    34
    @contextmanager
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    35
    def _publish_static_files(self, url, header={}):
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    36
        with self.admin_access.web_request(headers=header) as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    37
            req._url = url
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    38
            self.app_handle_request(req, url)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    39
            yield req
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    40
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    41
class StaticControllerCacheTC(staticfilespublishermixin, CubicWebTC):
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
    42
    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
    43
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
    44
    def test_static_file_are_cached(self):
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    45
        with self._publish_static_files('data/cubicweb.css') as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    46
            self.assertEqual(200, req.status_out)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    47
            self.assertIn('last-modified', req.headers_out)
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
    48
        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
    49
            '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
    50
        }
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    51
        with self._publish_static_files('data/cubicweb.css', next_headers) as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    52
            self.assertEqual(304, req.status_out)
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
    53
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
    54
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    55
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    56
class DataControllerTC(staticfilespublishermixin, CubicWebTC):
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    57
    tags = CubicWebTC.tags | Tags('static_controller', 'data', 'http')
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    58
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    59
    def _check_datafile_ok(self, fname):
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    60
        with self._publish_static_files(fname) as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    61
            self.assertEqual(200, req.status_out)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    62
            self.assertIn('last-modified', req.headers_out)
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    63
        next_headers = {
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    64
            'if-modified-since': req.get_response_header('last-modified', raw=True),
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    65
        }
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    66
        with self._publish_static_files(fname, next_headers) as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    67
            self.assertEqual(304, req.status_out)
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    68
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    69
    def _check_no_datafile(self, fname):
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    70
        with self._publish_static_files(fname) as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    71
            self.assertEqual(404, req.status_out)
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    72
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    73
    def test_static_data_mode(self):
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    74
        hash = self.vreg.config.instance_md5_version()
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    75
        self.assertEqual(32, len(hash))
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    76
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    77
        with tempattr(self.vreg.config, 'mode', 'test'):
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    78
            self._check_datafile_ok('data/cubicweb.css')
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    79
            self._check_no_datafile('data/does/not/exist')
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    80
            self._check_no_datafile('data/%s/cubicweb.css' % ('0'*len(hash)))
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    81
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    82
        with tempattr(self.vreg.config, 'mode', 'notest'):
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    83
            self._check_datafile_ok('data/cubicweb.css')
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    84
            self._check_datafile_ok('data/%s/cubicweb.css' % hash)
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    85
            self._check_no_datafile('data/does/not/exist')
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    86
            self._check_no_datafile('data/%s/cubicweb.css' % ('0'*len(hash)))
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    87
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    88
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
class ConcatFilesTC(CubicWebTC):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
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
    91
    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
    92
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
    def tearDown(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        super(ConcatFilesTC, self).tearDown()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        self._cleanup_concat_cache()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
    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
    98
        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
    99
        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
   100
            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
   101
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   102
    @contextmanager
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
    def _publish_js_files(self, js_files):
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   104
        with self.admin_access.web_request() as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   105
            head = HTMLHead(req)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   106
            url = head.concat_urls([req.data_url(js_file)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   107
                                    for js_file in js_files])[len(req.base_url()):]
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   108
            req._url = url
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   109
            res = self.app_handle_request(req, url)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   110
            yield res, req
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
    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
   113
        content = u''
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
        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
   115
            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
   116
            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
   117
                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
   118
                    content += f.read() + '\n'
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
        return content
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
    def test_cache(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
        js_files = ('cubicweb.ajax.js', 'jquery.js')
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   123
        with self._publish_js_files(js_files) as (result, req):
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   124
            self.assertNotEqual(404, req.status_out)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   125
            # check result content
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   126
            self.assertEqual(result, self.expected_content(js_files))
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   127
            # make sure we kept a cached version on filesystem
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   128
            concat_hander = ConcatFilesHandler(self.config)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   129
            filepath = concat_hander.build_filepath(js_files)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   130
            self.assertTrue(osp.isfile(filepath))
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
    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
   134
        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
   135
        # 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
   136
        self.config.debugmode = True
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
        try:
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   138
            with self._publish_js_files(js_files) as (result, req):
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   139
                #print result
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   140
                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
   141
        finally:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
            self.config.debugmode = False
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
    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
   145
        js_files = ('cubicweb.ajax.js', 'dummy.js')
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   146
        with self._publish_js_files(js_files) as (result, req):
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   147
            self.assertNotEqual(404, req.status_out)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   148
            # check result content
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   149
            self.assertEqual(result, self.expected_content(js_files))
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
if __name__ == '__main__':
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
    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
   154
    unittest_main()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155