[request] Ensure base_url() always has a trailing '/' (closes #3955093)
authorRémi Cardona <remi.cardona@logilab.fr>
Fri, 23 May 2014 18:17:44 +0200
changeset 9818 e3d2012adcd0
parent 9816 af6e3db801fc
child 9819 95902c0b991b
[request] Ensure base_url() always has a trailing '/' (closes #3955093) Just as an example, cubicweb.ajax.js is clearly written under the assumption that BASE_URL/baseuri() - which simply contains req.base_url() - has a trailing /.
req.py
test/unittest_dbapi.py
web/request.py
--- a/req.py	Mon Jun 02 12:23:18 2014 +0200
+++ b/req.py	Fri May 23 18:17:44 2014 +0200
@@ -472,12 +472,16 @@
             raise ValueError(self._('can\'t parse %(value)r (expected %(format)s)')
                              % {'value': value, 'format': format})
 
+    def _base_url(self, secure=None):
+        if secure:
+            return self.vreg.config.get('https-url') or self.vreg.config['base-url']
+        return self.vreg.config['base-url']
+
     def base_url(self, secure=None):
         """return the root url of the instance
         """
-        if secure:
-            return self.vreg.config.get('https-url') or self.vreg.config['base-url']
-        return self.vreg.config['base-url']
+        url = self._base_url(secure=secure)
+        return url if url is None else url.rstrip('/') + '/'
 
     # abstract methods to override according to the web front-end #############
 
--- a/test/unittest_dbapi.py	Mon Jun 02 12:23:18 2014 +0200
+++ b/test/unittest_dbapi.py	Fri May 23 18:17:44 2014 +0200
@@ -78,7 +78,7 @@
         with tempattr(cnx.vreg, 'config', config):
             cnx.use_web_compatible_requests('http://perdu.com')
             req = cnx.request()
-            self.assertEqual(req.base_url(), 'http://perdu.com')
+            self.assertEqual(req.base_url(), 'http://perdu.com/')
             self.assertEqual(req.from_controller(), 'view')
             self.assertEqual(req.relative_path(), '')
             req.ajax_replace_url('domid') # don't crash
--- a/web/request.py	Mon Jun 02 12:23:18 2014 +0200
+++ b/web/request.py	Fri May 23 18:17:44 2014 +0200
@@ -162,7 +162,7 @@
         self.ajax_request = value
     json_request = property(_get_json_request, _set_json_request)
 
-    def base_url(self, secure=None):
+    def _base_url(self, secure=None):
         """return the root url of the instance
 
         secure = False -> base-url
@@ -175,7 +175,7 @@
         if secure:
             base_url = self.vreg.config.get('https-url')
         if base_url is None:
-            base_url = super(_CubicWebRequestBase, self).base_url()
+            base_url = super(_CubicWebRequestBase, self)._base_url()
         return base_url
 
     @property