def test_should_use_custom_error_handler(): app = Flask(__name__) api = FlaskRestly() api.set_custom_error_handler(lambda error: ('', 201)) api.init_app(app) @resource(name='test') class SomeResource: @get('/') def get(self): raise Exception() with app.app_context(): SomeResource() with app.test_client() as client: response = client.get('/api/rest/v1/test') assert response.status_code == 201 assert response.get_json() is None
def test_should_serialize_given_response_to_protobuf(data, expected_data): app = Flask(__name__) api = FlaskRestly() app.config['RESTLY_SERIALIZER'] = protobuf api.init_app(app) @resource(name='test') class SomeResource: @get('/') @body(Entity) def get(self): return data with app.app_context(): SomeResource() with app.test_client() as client: response = client.get('/api/rest/v1/test') data = response.get_data().strip(bytes('\n', 'utf8')) assert response.headers.get('Content-Type') == 'application/x-protobuf' assert data == expected_data
def test_should_deserialize_given_request_data_to_dictionary(request_data, expected_data): app = Flask(__name__) api = FlaskRestly() app.config['RESTLY_SERIALIZER'] = protobuf api.init_app(app) @resource(name='test') class SomeResource: @post('/') @body(incoming=Entity, outgoing=Entity) def get(self, body): print(body) return dict(id=body.get('id')) with app.app_context(): SomeResource() with app.test_client() as client: response = client.post('/api/rest/v1/test', data=request_data) data = response.get_data() assert data == expected_data
from flask import Flask from flask_restly import FlaskRestly from flask_restly.decorator import resource, get app = Flask(__name__) # json is default serializer # from flask_restly.serializer import json # app.config['RESTLY_SERIALIZER'] = json rest = FlaskRestly(app) rest.init_app(app) @resource(name='employees') class EmployeesResource: @get('/<id>', serialize=lambda result, _: str(result.get('id'))) def get_employee(self, id): return dict(id=int(id)) @get('/') def get_employees(self): return dict(entites=[dict(id=1), dict(id=2)]) with app.app_context(): EmployeesResource() if __name__ == "__main__": app.run(host='127.0.0.1', port=5001, debug=True)