def test_it_traverses_resources(self): root = LineDelimiterResource() request = Request(url=URL(path=[u"foo", u"bar", u"baz"])) renderer = traversal.traverse( path=b"/foo/bar/baz", resource=root, request=request, ) self.assertEqual(renderer.render(request), b"foo\nbar\nbaz")
def test_zero_levels(self): root = LineDelimiterResource() request = Request(url=URL(path=[u""])) renderer = traversal.traverse( path=b"/", resource=root, request=request, ) self.assertIs(renderer, root)
def test_single_level(self): root = LineDelimiterResource() request = Request(url=URL(path=[u"foo"])) renderer = traversal.traverse( path=b"/foo", resource=root, request=request, ) self.assertEqual(renderer.render(request), b"foo")
def test_leaf_resources_are_not_traversed(self): class Resource(object): def __init__(self, name=b""): self.name = name def get_child(self, name, request): resource = Resource(name=name) if name == b"2": resource.is_leaf = True return resource def render(self, name): return self.name root = Resource() request = Request(url=URL(path=[u"0", u"1", u"2", u"3", u"4", u"5"])) renderer = traversal.traverse( path=b"/0/1/2/3/4/5", resource=root, request=request, ) self.assertEqual(renderer.render(request), b"2")
def map(self, request, path): static_render = self.static_mapper.map(request=request, path=path) if static_render is not None: return static_render resource = traverse(path=path, request=request, resource=self.root) return resource.render