Пример #1
0
def test_blueprint_extra_kwargs():
    bp = Blueprint('test', url_prefix='/prefix')
    app = Rafter()

    def simple_filter(get_response, params):
        async def decorated_filter(request, *args, **kwargs):
            if request.args.get('a') == '1':
                return Response([1, 2, 3])

            return await get_response(request, *args, **kwargs)

        return decorated_filter

    @bp.resource('/test', validators=[simple_filter])
    async def main(request):
        return {'test': 'abc'}

    app.blueprint(bp)

    request, response = app.test_client.get('/test')
    assert response.status == 404
    assert response.json == {
        'status': 404,
        'message': 'Requested URL /test not found'
    }

    request, response = app.test_client.get('/prefix/test')
    assert response.status == 200
    assert response.json == {'test': 'abc'}

    request, response = app.test_client.get('/prefix/test?a=1')
    assert response.status == 200
    assert response.json == [1, 2, 3]
Пример #2
0
def test_blueprints_resource():
    bp = Blueprint(name='test')
    app = Rafter()

    @bp.resource('/')
    async def main(request):
        return {'test': 'abc'}

    app.blueprint(bp)

    request, response = app.test_client.get('/')
    assert response.status == 200
    assert response.json == {'test': 'abc'}
Пример #3
0
def test_list_default():
    app = Rafter(name='test_get')

    app.blueprint(openapi_blueprint)

    @app.get('/test')
    @doc.consumes(doc.List(int, description="All the numbers"),
                  location="body")
    def test(request):
        return json({"test": True})

    request, response = app.test_client.get('/openapi/spec.json')

    response_schema = json_loads(response.body.decode())
    parameter = response_schema['paths']['/test']['get']['parameters'][0]

    assert response.status == 200
    assert parameter['type'] == 'array'
    assert parameter['items']['type'] == 'integer'
Пример #4
0
def header_filter(get_response, params):
    async def decorated_filter(request, *args, **kwargs):
        response = await get_response(request, *args, **kwargs)
        response.headers['x-test'] = 'abc'
        return response

    return decorated_filter


@bpv1.resource('/')
async def v1_root(request):
    return {'version': 1}


@bpv1.resource('/test')
async def v1_test(request):
    return [3, 2, 1]


@bpv2.resource('/', validators=[header_filter])
async def v2_root(request):
    return {'version': 2}


app = Rafter()
app.blueprint(bpv1)
app.blueprint(bpv2, url_prefix='/v2')

if __name__ == "__main__":
    app.run(host="127.0.0.1", port=5000)
Пример #5
0
def test_get_docs():
    app = Rafter(name='test_get')
    app.blueprint(openapi_blueprint)

    request, response = app.test_client.get('/openapi/spec.json')
    assert response.status == 200
Пример #6
0
from rafter import Rafter
from rafter_openapi import swagger_blueprint, openapi_blueprint
from blueprints.car import blueprint as car_blueprint
from blueprints.driver import blueprint as driver_blueprint
from blueprints.garage import blueprint as garage_blueprint
from blueprints.manufacturer import blueprint as manufacturer_blueprint

app = Rafter()

app.blueprint(openapi_blueprint)
app.blueprint(swagger_blueprint)
app.blueprint(car_blueprint)
app.blueprint(driver_blueprint)
app.blueprint(garage_blueprint)
app.blueprint(manufacturer_blueprint)

app.config.API_VERSION = '1.0.0'
app.config.API_TITLE = 'Car API'
app.config.API_TERMS_OF_SERVICE = 'Use with caution!'
app.config.API_CONTACT_EMAIL = '*****@*****.**'

app.run(host="0.0.0.0", debug=True)