[web] allow /data/ url again (closes #2464798) stable
authorDavid Douard <david.douard@logilab.fr>
Thu, 19 Sep 2013 18:03:09 +0200
branchstable
changeset 9233 7d3316bfa4ff
parent 9232 430a7dc5e2cf
child 9234 ce504d23edf7
[web] allow /data/ url again (closes #2464798) This feature was broken by 65fecbeb9c3a (which fixed another one itself).
web/test/unittest_views_staticcontrollers.py
web/views/staticcontrollers.py
--- a/web/test/unittest_views_staticcontrollers.py	Mon Sep 16 10:59:59 2013 +0200
+++ b/web/test/unittest_views_staticcontrollers.py	Thu Sep 19 18:03:09 2013 +0200
@@ -1,3 +1,4 @@
+from logilab.common import tempattr
 from logilab.common.testlib import tag, Tags
 from cubicweb.devtools.testlib import CubicWebTC
 
@@ -29,6 +30,46 @@
         self.assertEqual(304, req.status_out)
 
 
+
+class DataControllerTC(CubicWebTC):
+
+    tags = CubicWebTC.tags | Tags('static_controller', 'data', '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 _check_datafile_ok(self, fname):
+        _, req = self._publish_static_files(fname)
+        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(fname, next_headers)
+        self.assertEqual(304, req.status_out)
+
+    def _check_no_datafile(self, fname):
+        _, req = self._publish_static_files(fname)
+        self.assertEqual(404, req.status_out)
+
+    def test_static_data_mode(self):
+        hash = self.vreg.config.instance_md5_version()
+        self.assertEqual(32, len(hash))
+
+        with tempattr(self.vreg.config, 'mode', 'test'):
+            self._check_datafile_ok('data/cubicweb.css')
+            self._check_no_datafile('data/does/not/exist')
+            self._check_no_datafile('data/%s/cubicweb.css' % ('0'*len(hash)))
+
+        with tempattr(self.vreg.config, 'mode', 'notest'):
+            self._check_datafile_ok('data/cubicweb.css')
+            self._check_datafile_ok('data/%s/cubicweb.css' % hash)
+            self._check_no_datafile('data/does/not/exist')
+            self._check_no_datafile('data/%s/cubicweb.css' % ('0'*len(hash)))
+
+
 class ConcatFilesTC(CubicWebTC):
 
     tags = CubicWebTC.tags | Tags('static_controller', 'concat')
--- a/web/views/staticcontrollers.py	Mon Sep 16 10:59:59 2013 +0200
+++ b/web/views/staticcontrollers.py	Thu Sep 19 18:03:09 2013 +0200
@@ -199,7 +199,12 @@
             filepath = self.concat_files_registry.concat_cached_filepath(paths)
         else:
             # skip leading '/data/' and url params
-            relpath = relpath[len(self.base_datapath):].split('?', 1)[0]
+            if relpath.startswith(self.base_datapath):
+                prefix = self.base_datapath
+            else:
+                prefix = 'data/'
+            relpath = relpath[len(prefix):]
+            relpath = relpath.split('?', 1)[0]
             dirpath, rid = config.locate_resource(relpath)
             if dirpath is None:
                 raise NotFound()