Last active
December 15, 2015 01:48
-
-
Save knzm/5182144 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wsgiref.simple_server import make_server | |
from webob.dec import wsgify | |
class Traverser(object): | |
def __init__(self, root): | |
self.root = root | |
@wsgify | |
def __call__(self, request): | |
node = self.root | |
while request.path_info_peek(): | |
name = request.path_info_pop() | |
child = getattr(node, name, None) | |
if child is None: | |
break | |
node = child | |
return request.send(node) | |
@wsgify | |
def node2(request): | |
return "OK. I'm node2" | |
@wsgify | |
def node3(request): | |
return "OK. I'm node3" | |
class Root(object): | |
a = staticmethod(node2) | |
def __init__(self): | |
self.b = node3 | |
@wsgify | |
def __call__(self, request): | |
return "OK. I'm node1" | |
node1 = Root() | |
node3.a = node1 | |
node3.b = node2 | |
if __name__ == '__main__': | |
httpd = make_server('', 8080, Traverser(node1)) | |
httpd.serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest import TestCase, SkipTest, main | |
from webtest import TestApp | |
class TestTraversal(TestCase): | |
def _get_application(self): | |
from traversal import Traverser, node1 | |
return TestApp(Traverser(node1)) | |
def test_root(self): | |
app = self._get_application() | |
res = app.get('/') | |
self.assertEqual(res.status_code, 200) | |
self.assertEqual(res.body, "OK. I'm node1") | |
def test_child(self): | |
app = self._get_application() | |
res = app.get('/a') | |
self.assertEqual(res.status_code, 200) | |
self.assertEqual(res.body, "OK. I'm node2") | |
def test_child_with_trailing_slash(self): | |
app = self._get_application() | |
res = app.get('/a/') | |
self.assertEqual(res.status_code, 200) | |
self.assertEqual(res.body, "OK. I'm node2") | |
def test_unknown_child(self): | |
app = self._get_application() | |
res = app.get('/c') | |
self.assertEqual(res.status_code, 200) | |
self.assertEqual(res.body, "OK. I'm node1") | |
def test_unknown_child_with_trailing_slash(self): | |
app = self._get_application() | |
res = app.get('/c/') | |
self.assertEqual(res.status_code, 200) | |
self.assertEqual(res.body, "OK. I'm node1") | |
def test_grandchild(self): | |
app = self._get_application() | |
res = app.get('/b/a') | |
self.assertEqual(res.status_code, 200) | |
self.assertEqual(res.body, "OK. I'm node1") | |
def test_grandchild_with_trailing_slash(self): | |
app = self._get_application() | |
res = app.get('/b/a/') | |
self.assertEqual(res.status_code, 200) | |
self.assertEqual(res.body, "OK. I'm node1") | |
def test_intermediate_node(self): | |
app = self._get_application() | |
res = app.get('/b') | |
self.assertEqual(res.status_code, 200) | |
self.assertEqual(res.body, "OK. I'm node3") | |
def test_intermediate_node_with_trailing_slash(self): | |
app = self._get_application() | |
res = app.get('/b/') | |
self.assertEqual(res.status_code, 200) | |
self.assertEqual(res.body, "OK. I'm node3") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment