1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
# -*- coding: utf-8 -*-
"""
Example to test cascading $refs and basePath
"""
from flask import Flask, jsonify, Blueprint
from flasgger import swag_from, Swagger
try:
import simplejson as json
except ImportError:
import json
try:
from http import HTTPStatus
except ImportError:
import httplib as HTTPStatus
swagger_description = 'test_refs_basePath_API.yaml'
app = Flask(__name__)
swagger = Swagger(app, template_file=swagger_description)
api_blueprint = Blueprint('api', 'api', url_prefix='/api')
@api_blueprint.route('/get_cost', methods=['POST'])
@swag_from(swagger.template, definition='GetCostRequest', validation=True)
def get_cost():
result = dict(description='The best place',
cost=dict(currency='EUR', value=123456))
return jsonify([result])
app.register_blueprint(api_blueprint)
def test_swag(client, specs_data):
"""
This test is runs automatically in Travis CI
:param client: Flask app test client
:param specs_data: {'url': {swag_specs}} for every spec in app
"""
with client.get('apispec_1.json') as response:
assert response.status_code == HTTPStatus.OK
response_data = json.loads(response.data)
assert response_data.get('basePath') == '/api/', \
'wrong basePath: %r' % response_data
paths = response_data.get('paths')
assert '/get_cost' in paths, \
'get_cost NOK: %r' % response_data
assert '/api/get_costs' not in paths, \
'/api/get_cost NOK: %r' % response_data
req_data = dict(level=2,
location=dict(name='my preferred location',
position=dict(latitude=47.352735,
longitude=0.593682)))
with client.post('/api/get_cost', data=json.dumps(req_data),
content_type='application/json') as response:
assert response.status_code == HTTPStatus.OK, \
'bad status: %r' % response.data
if __name__ == '__main__':
app.run(debug=True)
|