author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 03 May 2012 15:50:23 +0200 | |
changeset 8386 | 249b21722e5e |
parent 8323 | fe60a77ae4a7 |
child 8396 | 8d58fcf68539 |
permissions | -rw-r--r-- |
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 import StatusResponse |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
12 |
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
|
13 |
|
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
|
14 |
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
|
15 |
|
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 |
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
|
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 |
|
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 |
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
|
20 |
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
|
21 |
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
|
22 |
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
|
23 |
|
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 |
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
|
25 |
_, 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
|
26 |
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
|
27 |
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
|
28 |
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
|
29 |
'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
|
30 |
} |
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 |
_, 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
|
32 |
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
|
33 |
|
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
|
34 |
|
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
35 |
class ConcatFilesTC(CubicWebTC): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
36 |
|
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
|
37 |
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
|
38 |
|
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
39 |
def tearDown(self): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
40 |
super(ConcatFilesTC, self).tearDown() |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
41 |
self._cleanup_concat_cache() |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
42 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
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
|
47 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
48 |
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
|
49 |
req = self.request() |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
50 |
head = HTMLHead(req) |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
55 |
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
|
56 |
content = u'' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
content += f.read() + '\n' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
62 |
return content |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
63 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
64 |
def test_cache(self): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
# check result content |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
69 |
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
|
70 |
# 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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
75 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
76 |
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
|
77 |
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
|
78 |
# 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
|
79 |
self.config.debugmode = True |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
80 |
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
|
81 |
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
|
82 |
#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
|
83 |
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
|
84 |
finally: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
85 |
self.config.debugmode = False |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
86 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
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
|
91 |
# check result content |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
92 |
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
|
93 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
94 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
95 |
if __name__ == '__main__': |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
96 |
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
|
97 |
unittest_main() |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
98 |