Exemplo n.º 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
Exemplo n.º 2
0
    def test_process_serialize_object_inside_objects(self):

        import decimal

        router = Router()
        request = request_generator('/test')

        class FakeData(object):
            data = 'hdsihas8h9277t27gsj'
            pepe = decimal.Decimal('10.0')

        class FakeResult(object):
            name = 'Test'
            data = FakeData()

        resp = router._process(FakeResult(), request)
        self.assertIsInstance(resp, response.Ok)
        self.assertEqual(
            resp.subject, {
                'data': {
                    'pepe': '10.0',
                    'data': 'hdsihas8h9277t27gsj'
                },
                'name': 'Test'
            })
Exemplo n.º 3
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
Exemplo n.º 4
0
    def test_process_error(self):

        router = Router()
        request = request_generator('/test')

        resp = router._process_error('fake_result', request)
        self.assertIsInstance(resp, response.InternalServerError)
        self.assertEqual(resp.code, 500)
Exemplo n.º 5
0
    def test_process_error(self):

        router = Router()
        request = request_generator('/test')

        resp = router._process_error('fake_result', request=request)
        self.assertIsInstance(resp, response.InternalServerError)
        self.assertEqual(resp.code, 500)
Exemplo n.º 6
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')
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
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')
Exemplo n.º 10
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)
Exemplo n.º 11
0
    def test_register_route(self):

        router = Router()
        ctl = StubController()
        self.assertTrue(len(router.routes['GET']) == 0)
        route = Route('GET', '/defer', ctl.deferred)
        router.register_route(ctl, route, ctl.deferred)
        self.assertTrue(len(router.routes['GET']) == 1)
        self.assertTrue('/defer' in router.routes['GET'])
        self.assertTrue('StubController' in router.routes['GET']['/defer'])
Exemplo n.º 12
0
    def test_register_route(self):

        router = Router()
        ctl = StubController()
        self.assertTrue(len(router.routes['GET']) == 0)
        route = Route('GET', '/defer', ctl.deferred)
        router.register_route(ctl, route, ctl.deferred)
        self.assertTrue(len(router.routes['GET']) == 1)
        self.assertTrue('/defer' in router.routes['GET'])
        self.assertTrue('StubController' in router.routes['GET']['/defer'])
Exemplo n.º 13
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
        )
Exemplo n.º 14
0
    def test_route_decorator(self):

        router = Router()
        decorator = router.route('/decorator_test')

        def test(self, request, **kwargs):
            return None

        decorated_func = decorator(test)
        self.assertIsInstance(decorated_func.route, Route)
        self.assertEqual(decorated_func.route.url, '/decorator_test')
Exemplo n.º 15
0
    def test_route_decorator(self):

        router = Router()
        decorator = router.route('/decorator_test')

        def test(self, request, **kwargs):
            return None

        decorated_func = decorator(test)
        self.assertIsInstance(decorated_func.route, Route)
        self.assertEqual(decorated_func.route.url, '/decorator_test')
Exemplo n.º 16
0
    def test_process_return_objects_json_converted(self):

        router = Router()
        request = request_generator('/test')

        class FakeResult(object):
            name = 'Test'
            type = 'JSON'

        resp = router._process(FakeResult(), request)
        self.assertIsInstance(resp, response.Ok)
        self.assertEqual(resp.subject, {'name': 'Test', 'type': 'JSON'})
Exemplo n.º 17
0
    def test_process_return_objects_json_converted(self):

        router = Router()
        request = request_generator('/test')

        class FakeResult(object):
            name = 'Test'
            type = 'JSON'

        resp = router._process(FakeResult(), request)
        self.assertIsInstance(resp, response.Ok)
        self.assertEqual(resp.subject, {'name': 'Test', 'type': 'JSON'})
Exemplo n.º 18
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)
Exemplo n.º 19
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)
Exemplo n.º 20
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'])
Exemplo n.º 21
0
    def test_register_route_raise_route_error_on_invalid_HTTP_method(self):

        router = Router()
        ctl = StubController()
        route = Route('FAIL', '/defer', ctl.deferred)
        self.assertRaises(RouterError, router.register_route, ctl, route,
                          ctl.deferred)
Exemplo n.º 22
0
    def test_lookup_returns_none_on_invalid_controller_or_router(self):

        route_dispatcher = RouteDispatcher(
            Router(), StubController(), DummyRequest(['/test'])
        )

        self.assertEqual(route_dispatcher.lookup(), None)
Exemplo n.º 23
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']
        )
Exemplo n.º 24
0
    def test_process_serialize_object_inside_objects(self):

        import decimal

        router = Router()
        request = request_generator('/test')

        class FakeData(object):
            data = 'hdsihas8h9277t27gsj'
            pepe = decimal.Decimal('10.0')

        class FakeResult(object):
            name = 'Test'
            data = FakeData()

        resp = router._process(FakeResult(), request)
        self.assertIsInstance(resp, response.Ok)
        self.assertEqual(resp.subject, {
            'data': {'pepe': '10.0', 'data': 'hdsihas8h9277t27gsj'},
            'name': 'Test'
        })
Exemplo n.º 25
0
    def test_controller_render_delegates_on_routing(self):

        c = DummyController()

        router = ProxySpy(Router())
        c._router = router
        # request = self.get_request()
        request = ControllerRequest(['/test'], {})
        r = yield self._render(c, request)

        assert_that(router.dispatch, called().times(1))
        self.assertEqual(r.written[0], 'ERROR 404: /dummy/test not found')
Exemplo n.º 26
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)
Exemplo n.º 27
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)
Exemplo n.º 28
0
 def __init__(self):
     self.path = ''
     self._router = Router()
     self._router.install_routes(self)
     self.children = {}
Exemplo n.º 29
0
    def test_process_return_unknown_on_none_result(self):

        router = Router()
        self.assertIsInstance(router._process(None, None), response.Unknown)
Exemplo n.º 30
0
    def test_sanitize_urls_on_initialization(self):

        route_dispatcher = RouteDispatcher(Router(), StubController(),
                                           DummyRequest(['////test///one///']))
        self.assertEqual(route_dispatcher.url, '/test/one')
Exemplo n.º 31
0
# Copyright (c) 2012 Oscar Campos <*****@*****.**>
# See LICENSE for more details
"""
.. module:: application
    :platform: Linux
    :synopsis: Objects for build Mamba Applications

.. moduleauthor:: Oscar Campos <*****@*****.**>
"""

from .app import Mamba, ApplicationError, _app_ver
from .controller import (Controller, ControllerManager, ControllerProvider,
                         ControllerError)
from .appstyles import AppStyles
from .model import Model, ModelManager
from mamba.web.routing import Router

route = Router().route

__all__ = [
    'Mamba', 'ApplicationError', '_app_ver', 'Controller', 'ControllerManager',
    'ControllerProvider', 'ControllerError', 'AppStyles', 'Model',
    'ModelManager', 'route'
]
Exemplo n.º 32
0
 def __init__(self):
     self.path = ''
     self._router = Router()
     self._router.install_routes(self)
Exemplo n.º 33
0
    def test_process_return_unknown_on_none_result(self):

        router = Router()
        self.assertIsInstance(router._process(None, None), response.Unknown)