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