changeset 380:f33712c4cfab

routing: Fix bugs with matching URLs with correct route but missing metadata. When matching a route like `/foo/%slug%` against an URL like `/foo`, the route will (correctly) return a match, but it will be completely missing the `slug` metadata, resulting in problems elsewhere. This change makes it so that any missing route metadata will be filled in with an empty string. And because this means generated URLs may differ from the incoming URL when using trailing slashes (`/foo/` _vs._ `/foo`), we make the assert in the chef server handle those discrepancies.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 10 May 2015 00:34:21 -0700
parents d40b744a9d99
children 4c9eab0e283b
files piecrust/routing.py piecrust/serving/server.py tests/test_routing.py
diffstat 3 files changed, 21 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/routing.py	Sat May 09 08:48:58 2015 -0700
+++ b/piecrust/routing.py	Sun May 10 00:34:21 2015 -0700
@@ -90,7 +90,7 @@
     def matchesMetadata(self, route_metadata):
         return self.required_route_metadata.issubset(route_metadata.keys())
 
-    def matchUri(self, uri):
+    def matchUri(self, uri, strict=False):
         if not uri.startswith(self.uri_root):
             raise Exception("The given URI is not absolute: %s" % uri)
         uri = uri[len(self.uri_root):]
@@ -100,14 +100,28 @@
         elif self.trailing_slash:
             uri = uri.rstrip('/')
 
+        route_metadata = None
         m = self.uri_re.match(uri)
         if m:
-            return m.groupdict()
+            route_metadata = m.groupdict()
         if self.uri_re_no_path:
             m = self.uri_re_no_path.match(uri)
             if m:
-                return m.groupdict()
-        return None
+                route_metadata = m.groupdict()
+        if route_metadata is None:
+            return None
+
+        if not strict:
+            # When matching URIs, if the URI is a match but is missing some
+            # metadata, fill those up with empty strings. This can happen if,
+            # say, a route's pattern is `/foo/%slug%`, and we're matching an
+            # URL like `/foo`.
+            matched_keys = set(route_metadata.keys())
+            missing_keys = self.required_route_metadata - matched_keys
+            for k in missing_keys:
+                route_metadata[k] = ''
+
+        return route_metadata
 
     def getUri(self, route_metadata, *, sub_num=1, provider=None):
         route_metadata = copy.deepcopy(route_metadata)
--- a/piecrust/serving/server.py	Sat May 09 08:48:58 2015 -0700
+++ b/piecrust/serving/server.py	Sun May 10 00:34:21 2015 -0700
@@ -341,7 +341,7 @@
         # just don't use cached rendered segments for that page (but still
         # use them for pages that are included in it).
         uri = qp.getUri()
-        assert uri == req_path
+        assert uri.rstrip(' /') == req_path.rstrip(' /')
         entry = self._page_record.getEntry(uri, page_num)
         if (taxonomy_info is not None or entry is None or
                 entry.used_source_names):
--- a/tests/test_routing.py	Sat May 09 08:48:58 2015 -0700
+++ b/tests/test_routing.py	Sun May 10 00:34:21 2015 -0700
@@ -68,7 +68,7 @@
                 {'foo': ''}),
             ('/', {'url': '/prefix/%path:foo%'},
                 'prefix',
-                {}),
+                {'foo': ''}),
 
             ('/blah', {'url': '/%foo%'},
                 'something',
@@ -90,7 +90,7 @@
                 {'foo': ''}),
             ('/blah', {'url': '/prefix/%path:foo%'},
                 'prefix',
-                {}),
+                {'foo': ''}),
             ])
 def test_match_uri(site_root, config, uri, expected_match):
     site_root = site_root.rstrip('/') + '/'