Пример #1
0
class StubController(object):

    def __init__(self):
        self.path = ''
        self._router = Router()
        self._router.install_routes(self)
        self.children = {}

    def render(self, request):
        return self._router.dispatch(self, request)

    def get_register_path(self):
        return self.path

    @decoroute('/test/<int:user_id>')
    def test(self, request, user_id, **kwargs):
        return 'User ID : {}'.format(user_id)

    @decoroute('/defer')
    @defer.inlineCallbacks
    def deferred(self, request, **kwargs):
        val1 = yield 'Hello '
        val2 = yield 'Defer!'
        defer.returnValue(val1 + val2)

    @decoroute('/209')
    def unknown(self, request, **kwargs):
        pass
Пример #2
0
class StubController(object):
    def __init__(self):
        self.path = ''
        self._router = Router()
        self._router.install_routes(self)

    def render(self, request):
        return self._router.dispatch(self, request)

    def get_register_path(self):
        return self.path

    @decoroute('/test/<int:user_id>')
    def test(self, request, user_id, **kwargs):
        return 'User ID : {}'.format(user_id)

    @decoroute('/defer')
    @defer.inlineCallbacks
    def deferred(self, request, **kwargs):
        val1 = yield 'Hello '
        val2 = yield 'Defer!'
        defer.returnValue(val1 + val2)

    @decoroute('/209')
    def unknown(self, request, **kwargs):
        pass
Пример #3
0
    def test_lookup_returns_not_implemented_on_valid_url_invalid_method(self):

        controller = StubController()
        request = request_generator(['/test/102'], method='POST')
        router = Router()
        router.install_routes(controller)
        route_dispatcher = RouteDispatcher(router, controller, request)

        self.assertEqual(route_dispatcher.lookup()[0], 'NotImplemented')
Пример #4
0
    def test_lookup_returns_route(self):

        controller = StubController()
        request = request_generator(['/test/102'])
        router = Router()
        router.install_routes(controller)
        route_dispatcher = RouteDispatcher(router, controller, request)

        self.assertIsInstance(route_dispatcher.lookup()[0], Route)
Пример #5
0
    def test_lookup_returns_not_implemented_on_valid_url_invalid_method(self):

        controller = StubController()
        request = request_generator(['/test/102'], method='POST')
        router = Router()
        router.install_routes(controller)
        route_dispatcher = RouteDispatcher(router, controller, request)

        self.assertEqual(route_dispatcher.lookup(), 'NotImplemented')
Пример #6
0
    def test_lookup_returns_route(self):

        controller = StubController()
        request = request_generator(['/test/102'])
        router = Router()
        router.install_routes(controller)
        route_dispatcher = RouteDispatcher(router, controller, request)

        self.assertIsInstance(route_dispatcher.lookup(), Route)
Пример #7
0
    def test_dispatch_returns_unknown_209_on_no_return_from_method(self):

        controller = StubController()
        request = request_generator(['/209'], method='GET')
        router = Router()
        router.install_routes(controller)

        self.assertIsInstance(
            router.dispatch(controller, request).result, response.Unknown)
Пример #8
0
    def test_dispatch_returns_unknown_209_on_no_return_from_method(self):

        controller = StubController()
        request = request_generator(['/209'], method='GET')
        router = Router()
        router.install_routes(controller)

        self.assertIsInstance(
            router.dispatch(controller, request).result, response.Unknown
        )
Пример #9
0
    def test_install_router_fails_when_give_wrong_arguments(self):

        router = Router()

        @decoroute('/test')
        def test(self, request, user_id, **kwargs):
            return 'I will fail'

        StubController.test2 = test
        router.install_routes(StubController())

        # only one of the routes should be installed
        self.assertTrue(len(router.routes['GET']) == 3)
Пример #10
0
    def test_install_router_fails_when_give_wrong_arguments(self):

        router = Router()

        @decoroute('/test')
        def test(self, request, user_id, **kwargs):
            return 'I will fail'

        StubController.test2 = test
        router.install_routes(StubController())

        # only one of the routes should be installed
        self.assertTrue(len(router.routes['GET']) == 3)
Пример #11
0
    def test_install_several_HTTP_methods_per_route_decorator(self):

        router = Router()

        class MyStubController(StubController):
            @decoroute('/multimethod_test', method=['GET', 'POST'])
            def multimethod_test(self, request, **kwargs):
                return None

        router.install_routes(MyStubController())

        self.assertTrue(len(router.routes['GET']) == 4)
        self.assertTrue(len(router.routes['POST']) == 1)
        self.assertEqual(router.routes['GET']['/multimethod_test'],
                         router.routes['POST']['/multimethod_test'])
Пример #12
0
    def test_install_several_HTTP_methods_per_route_decorator(self):

        router = Router()

        class MyStubController(StubController):
            @decoroute('/multimethod_test', method=['GET', 'POST'])
            def multimethod_test(self, request, **kwargs):
                return None

        router.install_routes(MyStubController())

        self.assertTrue(len(router.routes['GET']) == 4)
        self.assertTrue(len(router.routes['POST']) == 1)
        self.assertEqual(
            router.routes['GET']['/multimethod_test'],
            router.routes['POST']['/multimethod_test']
        )
Пример #13
0
    def test_install_routes_register_route(self):

        router = Router()
        router.install_routes(StubController())

        self.assertTrue(len(router.routes['GET']) == 3)

        del router
        router = Router()

        class MyStubController(StubController):
            @decoroute('/another_test')
            def another_test(self, request, **kwargs):
                return None

        router.install_routes(MyStubController())

        self.assertTrue(len(router.routes['GET']) == 4)
Пример #14
0
    def test_install_routes_register_route(self):

        router = Router()
        router.install_routes(StubController())

        self.assertTrue(len(router.routes['GET']) == 3)

        del router
        router = Router()

        class MyStubController(StubController):
            @decoroute('/another_test')
            def another_test(self, request, **kwargs):
                return None

        router.install_routes(MyStubController())

        self.assertTrue(len(router.routes['GET']) == 4)