def test_iteration(self): from otto.router import Router from otto.router import Route router = Router() route1 = Route('/test') route2 = Route('/:test') route3 = Route('/no-match/:test') route4 = Route('/te:match') for route in (route1, route2, route3, route4): router.connect(route) matches = tuple(router('/test')) self.assertEqual(len(matches), 3) self.assertEqual(matches[0].route, route1) self.assertEqual(matches[1].route, route2) self.assertEqual(matches[2].route, route4)
class Publisher(object): """HTTP publisher. The ``mapper`` argument is optional; if provided, it will be used as the default mapper. Route definitions are added using the ``route`` method. It takes an optional ``mapper`` argument which is then used in place of the default value. """ def __init__(self, mapper=None): """The optional ``mapper`` argument specifies the default route mapper.""" self._router = Router() self._mapper = mapper def match(self, path): """Match ``path`` with routing table and return route controller.""" gen = self._router(path) try: match = next(gen) except StopIteration: return route = match.route return route.dispatch(match.dict) def connect(self, path, controller=None, mapper=None): """Use this method to add routes.""" if mapper is None: mapper = self._mapper route = Dispatcher(path, controller=controller, mapper=mapper) self._router.connect(route) return route
def __init__(self, mapper=None): """The optional ``mapper`` argument specifies the default route mapper.""" self._router = Router() self._mapper = mapper
def test_empty_routes(self): from otto.router import Router router = Router() gen = router('/path') self.assertRaises(StopIteration, next, gen)