8000 fix for #58 by dutronlabs · Pull Request #59 · flask-api/flask-api · GitHub
[go: up one dir, main page]

Skip to content

fix for #58 #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 20, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added tests, ready for pr submit
  • Loading branch information
Chris Dutra committed Jul 18, 2016
commit fb5e5bbc7170e12460470671f1ed26c4cebbf2d6
48 changes: 47 additions & 1 deletion flask_api/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: utf8
from __future__ import unicode_literals
from flask import abort, make_response, request
from flask import abort, make_response, request, jsonify
from flask_api.decorators import set_renderers
from flask_api import exceptions, renderers, status, FlaskAPI
import json
Expand All @@ -19,6 +19,30 @@ class JSONVersion2(renderers.JSONRenderer):
media_type = 'application/json; api-version="2.0"'


# This is being used to test issue #58, source is taken from flask apierrors doc page
class InvalidUsage(Exception):
status_code = 400

def __init__(self, message, status_code=None, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload

def to_dict(self):
rv = dict(self.payload or ())
rv['message'] = self.message
return rv


@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response


@app.route('/set_status_and_headers/')
def set_status_and_headers():
headers = {'Location': 'http://example.com/456'}
Expand All @@ -43,6 +67,16 @@ def api_exception():
raise exceptions.PermissionDenied()


@app.route('/custom_exception/')
def custom_exception():
raise InvalidUsage('Invalid usage test.', status_code=410)


@app.route('/custom_exception_no_code/')
def custom_exception_no_status_code():
raise InvalidUsage('Invalid usage test.')


@app.route('/abort_view/')
def abort_view():
abort(status.HTTP_403_FORBIDDEN)
Expand Down Expand Up @@ -95,6 +129,18 @@ def test_api_exception(self):
expected = '{"message": "You do not have permission to perform this action."}'
self.assertEqual(response.get_data().decode('utf8'), expected)

def test_custom_exception(self):
with app.test_client() as client:
response = client.get('/custom_exception/')
self.assertEqual(response.status_code, status.HTTP_410_GONE)
self.assertEqual(response.content_type, 'application/json')

def test_custom_exception_default_code(self):
with app.test_client() as client:
response = client.get('/custom_exception_no_code/')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.content_type, 'application/json')

def test_abort_view(self):
with app.test_client() as client:
response = client.get('/abort_view/')
Expand Down
0