Пример #1
0
    def get_app(self):

        dispatcher = Dispatcher()

        def error_listener(event):
            event.get('request_handler').write('An unexpected error occurred')

        def not_found_listener(event):
            event.get('request_handler').write('Not Found')

        dispatcher.add_listener('handler.not_found', not_found_listener)
        dispatcher.add_listener('handler.exception', error_listener)

        router = Router()
        router.add("hello", "/hello/<string:name>", view, methods=['GET'])
        router.add("exception", "/exception", error, methods=['GET'])

        return Application([("/.*", RouterHandler, dict(router=router, event_dispatcher=dispatcher))])
Пример #2
0
    def setUp(self):

        self.router = Router()
Пример #3
0
class RouterTest(unittest.TestCase):
    def setUp(self):

        self.router = Router()

    def test_add_and_match_routes(self):
        self.router.add("homepage", "/", view)

        self.assertEquals(('homepage', {}, view), self.router.match("/"))

        self.router.add("blog_post", "/blog/<string:slug>", view, methods=['GET'])

        self.assertEquals(('blog_post', {'slug': 'hello'}, view), self.router.match("/blog/hello"))

    def test_add_and_generate_routes(self):

        self.router.add("homepage", "/", view)
        self.router.add("blog_post", "/blog/<string:slug>", view)

        self.assertEquals("/", self.router.generate("homepage"))
        self.assertEquals("/?panel=user", self.router.generate("homepage", panel="user"))
        self.assertEquals("/blog/hello", self.router.generate("blog_post", slug="hello"))

        self.assertEquals("http://localhost/blog/hello", self.router.generate("blog_post", slug="hello", force_external=True))