from flask import Flask, request
from flask_restful import Api, Resource, reqparse
app = Flask(__name__)
api = Api(app)
class Echo(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('text', help='Type in some text', required=True)
args = parser.parse_args()
return { "res": f"Text: {args['text']}" }
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('text', help='Type in some text')
args = parser.parse_args()
return { "Answer": f"You said: {args['text']}" }
api.add_resource(Echo, '/echo')
import api
def test_echo():
web = api.app.test_client()
# rv = web.get('/echo?text=hello')
# assert rv.status == '200 OK'
# assert rv.headers['Content-Type'] == 'application/json'
# assert rv.json == {'res': 'Text: hello'}
#
# rv = web.post('/echo', data={'text': 'ciao'})
# assert rv.status == '200 OK'
# assert rv.headers['Content-Type'] == 'application/json'
# assert rv.json == {'Answer': 'You said: ciao'}
#
#
# # If the parameter is missing the parser just returns None
# rv = web.get('/echo')
# assert rv.status == '400 BAD REQUEST'
# assert rv.headers['Content-Type'] == 'application/json'
# assert rv.json == {'message': {'text': 'Type in some text'}}