|
1 from __future__ import with_statement |
|
2 |
|
3 from cubicweb.devtools.testlib import CubicWebTC |
|
4 |
|
5 import os |
|
6 import os.path as osp |
|
7 import glob |
|
8 |
|
9 from cubicweb.utils import HTMLHead |
|
10 from cubicweb.web import StatusResponse |
|
11 from cubicweb.web.views.staticcontrollers import ConcatFilesHandler |
|
12 |
|
13 class ConcatFilesTC(CubicWebTC): |
|
14 |
|
15 def tearDown(self): |
|
16 super(ConcatFilesTC, self).tearDown() |
|
17 self._cleanup_concat_cache() |
|
18 |
|
19 def _cleanup_concat_cache(self): |
|
20 uicachedir = osp.join(self.config.apphome, 'uicache') |
|
21 for fname in glob.glob(osp.join(uicachedir, 'cache_concat_*')): |
|
22 os.unlink(osp.join(uicachedir, fname)) |
|
23 |
|
24 def _publish_js_files(self, js_files): |
|
25 req = self.request() |
|
26 head = HTMLHead(req) |
|
27 url = head.concat_urls([req.data_url(js_file) for js_file in js_files])[len(req.base_url()):] |
|
28 req._url = url |
|
29 return self.app_publish(req, url) |
|
30 |
|
31 def expected_content(self, js_files): |
|
32 content = u'' |
|
33 for js_file in js_files: |
|
34 dirpath, rid = self.config.locate_resource(js_file) |
|
35 if dirpath is not None: # ignore resources not found |
|
36 with open(osp.join(dirpath, rid)) as f: |
|
37 content += f.read() + '\n' |
|
38 return content |
|
39 |
|
40 def test_cache(self): |
|
41 js_files = ('cubicweb.ajax.js', 'jquery.js') |
|
42 try: |
|
43 result = self._publish_js_files(js_files) |
|
44 except StatusResponse, exc: |
|
45 if exc.status == 404: |
|
46 self.fail('unable to serve cubicweb.js+jquery.js') |
|
47 # let the exception propagate for any other status (e.g 500) |
|
48 raise |
|
49 # check result content |
|
50 self.assertEqual(result, self.expected_content(js_files)) |
|
51 # make sure we kept a cached version on filesystem |
|
52 concat_hander = ConcatFilesHandler(self.config) |
|
53 filepath = concat_hander.build_filepath(js_files) |
|
54 self.assertTrue(osp.isfile(filepath)) |
|
55 |
|
56 |
|
57 def test_invalid_file_in_debug_mode(self): |
|
58 js_files = ('cubicweb.ajax.js', 'dummy.js') |
|
59 # in debug mode, an error is raised |
|
60 self.config.debugmode = True |
|
61 try: |
|
62 result = self._publish_js_files(js_files) |
|
63 self.fail('invalid concat js should return a 404 in debug mode') |
|
64 except StatusResponse, exc: |
|
65 if exc.status != 404: |
|
66 self.fail('invalid concat js should return a 404 in debug mode') |
|
67 finally: |
|
68 self.config.debugmode = False |
|
69 |
|
70 def test_invalid_file_in_production_mode(self): |
|
71 js_files = ('cubicweb.ajax.js', 'dummy.js') |
|
72 try: |
|
73 result = self._publish_js_files(js_files) |
|
74 except StatusResponse, exc: |
|
75 if exc.status == 404: |
|
76 self.fail('invalid concat js should NOT return a 404 in debug mode') |
|
77 # let the exception propagate for any other status (e.g 500) |
|
78 raise |
|
79 # check result content |
|
80 self.assertEqual(result, self.expected_content(js_files)) |
|
81 |
|
82 |
|
83 if __name__ == '__main__': |
|
84 from logilab.common.testlib import unittest_main |
|
85 unittest_main() |
|
86 |