# HG changeset patch # User Ludovic Chabant # Date 1431243261 25200 # Node ID f33712c4cfabe68fd3e84ff1c5dc626cea48a979 # Parent d40b744a9d998c63d098722f77467fcae11b391e 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. diff -r d40b744a9d99 -r f33712c4cfab piecrust/routing.py --- 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) diff -r d40b744a9d99 -r f33712c4cfab piecrust/serving/server.py --- 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): diff -r d40b744a9d99 -r f33712c4cfab tests/test_routing.py --- 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('/') + '/'