Flask REST API - Echo



examples/flask/api_echo/api.py
from flask import Flask, request
from flask_restful import Api, Resource

app = Flask(__name__)

api = Api(app)

class Echo(Resource):
    def get(self):
        return { "prompt": "Type in something" }
    def post(self):
        return { "echo": "This" }

api.add_resource(Echo, '/echo')

examples/flask/api_echo/test_api.py
import api

def test_echo():
    web = api.app.test_client()

    rv = web.get('/echo')
    assert rv.status == '200 OK'
    assert rv.headers['Content-Type'] == 'application/json'
    assert rv.json == {"prompt": "Type in something"}


    rv = web.post('/echo')
    assert rv.status == '200 OK'
    assert rv.headers['Content-Type'] == 'application/json'
    assert rv.json == {"echo": "This"}