Пример #1
0
def test_with_custom_class_methods():
    app = Luya('test_with_custom_class_methods')

    class DummyView(MethodView):
        global_var = 0

        def _iternal_method(self):
            self.global_var += 10

        def get(self, request):
            self._iternal_method()
            return text('I am get method and global var is {}'.format(
                self.global_var))

    app.add_route(DummyView.to_view(), '/')
    response = app.test_client.get('/')
    assert response.text == 'I am get method and global var is 10'
Пример #2
0
def test_dynamic_add_route_string():
    app = Luya('test_dynamic_add_route_string')

    results = []

    async def handler(request, name):
        results.append(name)
        return text('OK')

    app.add_route(handler, '/folder/<name:string>')
    response = app.test_client.get('/folder/test123')

    assert response.text == 'OK'
    assert results[0] == 'test123'

    response = app.test_client.get('/folder/favicon.ico')

    assert response.text == 'OK'
    assert results[1] == 'favicon.ico'
Пример #3
0
def test_dynamic_add_route_number():
    app = Luya('test_dynamic_add_route_number')

    results = []

    async def handler(request, weight):
        results.append(weight)
        return text('OK')

    app.add_route(handler, '/weight/<weight:number>')

    response = app.test_client.get('/weight/12345')
    assert response.text == 'OK'
    assert type(results[0]) is float

    response = app.test_client.get('/weight/1234.56')
    assert response.status == 200

    response = app.test_client.get('/weight/1234-56')
    assert response.status == 404
Пример #4
0
    def test_one_Blueprint(self):

        app = Luya()

        class Mv(MethodView):
            def __init__(self):
                pass

            def get(self, request):
                return response.html('<h1>class view test</h1>')

            def post(self, request):
                return response.html('<h1>post</h1>')

        app.add_route(Mv.to_view(), '/MethodView')

        request = request_class(url='/MethodView', method='GET')
        handler, kw = app.router.get_mapped_handle(request)
        self.assertEqual(kw, {})
        self.assertEqual(handler(request).status, 200)

        request = request_class(url='/MethodView', method='POST')
Пример #5
0
def test_add_route_duplicate():
    app = Luya('test_add_route_duplicate')

    with pytest.raises(ValueError):

        async def handler1(request):
            pass

        async def handler2(request):
            pass

        app.add_route(handler1, '/test')
        app.add_route(handler2, '/test')

    with pytest.raises(ValueError):

        async def handler1(request, dynamic):
            pass

        async def handler2(request, dynamic):
            pass

        app.add_route(handler1, '/test/<dynamic>/')
        app.add_route(handler2, '/test/<dynamic>/')