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]
def test_app_request_class(): class Req(Request): pass Rafter(request_class=Req) class Req2(object): pass with pytest.raises(RuntimeError): Rafter(request_class=Req2)
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'}
def test_app_filters(): def filter1(get_response, params): async def func(request, *args, **kwargs): return await get_response(request, *args, **kwargs) return func def filter2(get_response, params): async def func(request, *args, **kwargs): resp = await get_response(request, *args, **kwargs) return text(resp.body.decode('utf-8') + 'xyz') return func app = Rafter() @app.resource('/', filters=[filter1]) async def test(request): return text('abc') @app.resource('/tr', filters=[filter1, filter2]) async def testTR(request): return text('abc') request, response = app.test_client.get('/') assert response.status == 200 assert response.body == b'abc' request, response = app.test_client.get('/tr') assert response.status == 200 assert response.body == b'abcxyz'
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'
def test_app_resource(): app = Rafter() @app.resource('/') def test(request): pass @app.resource('/strm', stream=True) def strm(request): pass route = app.router._get('/strm', 'GET', '') assert route[0].is_stream is True
def test_app(): Rafter(name='abc') # No positional arguments with pytest.raises(TypeError): Rafter('name')
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)
# -*- coding: utf-8 -*- from rafter import Rafter, ApiError, Response # Our main Rafter App app = Rafter() @app.resource('/') async def main_view(request): # Simply return arbitrary data and the response filter # will convert it to a sanic.response.json response. return {'data': 'Hello there!'} @app.resource('/p/<param>') async def with_params(request, param): # Just return the request's param in a list. return [param] @app.resource('/status') async def status(request): # Return a 201 response with some data return Response({'test': 'abc'}, 201) @app.resource('/error') async def error_response(request): # Return an error response with a status code and some extra data. raise ApiError('Something bad happened!', 501, extra_data=':(')
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
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)