def locate_resource(self, request): """ Locate the resource at the path in request URL by traversing the resource hierarchy. """ # Calculate the path segments relative to the application, # special-casing requests for the the root segment (because we already # have a reference to the root resource). try: segments = url.split_path(request.environ['PATH_INFO']) except UnicodeDecodeError: return http.bad_request() if segments == ['']: segments = [] # Recurse into the resource hierarchy until we run out of segments or # find a Response. resource = self.root while segments and not isinstance(resource, http.Response): resource_child = getattr(resource, 'resource_child', None) # No resource_child method? 404. if resource_child is None: raise http.NotFoundError() result = resource_child(request, segments) # No result returned? 404. if result is None: raise http.NotFoundError() # Either a (resource, remaining segments) tuple or an object to # forward the lookup to is acceptable. if isinstance(result, tuple): resource, segments = result else: resource = result return resource
def test_split_path(self): self.assertEquals(url.split_path(''), []) self.assertEquals(url.split_path('/'), ['']) self.assertEquals(url.split_path('//'), ['', '']) self.assertEquals(url.split_path('/foo'), ['foo']) self.assertEquals(url.split_path('/foo/bar'), ['foo', 'bar']) self.assertEquals(url.split_path('/%2F'), ['/']) self.assertEquals(url.split_path('/%C2%A3'), [POUND])
def test_split_path(self): self.assertEquals(url.split_path(""), []) self.assertEquals(url.split_path("/"), [""]) self.assertEquals(url.split_path("//"), ["", ""]) self.assertEquals(url.split_path("/foo"), ["foo"]) self.assertEquals(url.split_path("/foo/bar"), ["foo", "bar"]) self.assertEquals(url.split_path("/%2F"), ["/"]) self.assertEquals(url.split_path("/%C2%A3"), [POUND])