Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akira-dev committed Nov 22, 2017
1 parent 37343e0 commit 9d533a2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
24 changes: 17 additions & 7 deletions flask_rest_jsonapi/decorators.py
Expand Up @@ -18,18 +18,28 @@ def check_headers(func):
@wraps(func)
def wrapper(*args, **kwargs):
if request.method in ('POST', 'PATCH'):
if 'Content-Type' not in request.headers or request.headers['Content-Type'] != 'application/vnd.api+json':
if 'Content-Type' in request.headers and\
'application/vnd.api+json' in request.headers['Content-Type'] and\
request.headers['Content-Type'] != 'application/vnd.api+json':
error = jsonify(jsonapi_errors([{'source': '',
'detail': "Content-Type header must be application/vnd.api+json",
'title': 'InvalidRequestHeader',
'status': '415'}]))
return make_response(error, 415, {'Content-Type': 'application/vnd.api+json'})
if request.headers.get('Accept') and 'application/vnd.api+json' not in request.accept_mimetypes:
error = jsonify(jsonapi_errors([{'source': '',
'detail': "Accept header must be application/vnd.api+json",
'title': 'InvalidRequestHeader',
'status': '406'}]))
return make_response(error, 406, {'Content-Type': 'application/vnd.api+json'})
if 'Accept' in request.headers:
flag = False
for accept in request.headers['Accept'].split(','):
if accept == 'application/vnd.api+json':
flag = False
break
if 'application/vnd.api+json' in accept and accept != 'application/vnd.api+json':
flag = True
if flag is True:
error = jsonify(jsonapi_errors([{'source': '',
'detail': "Accept header must be application/vnd.api+json",
'title': 'InvalidRequestHeader',
'status': '406'}]))
return make_response(error, 406, {'Content-Type': 'application/vnd.api+json'})
return func(*args, **kwargs)
return wrapper

Expand Down
4 changes: 1 addition & 3 deletions flask_rest_jsonapi/exceptions.py
Expand Up @@ -8,7 +8,7 @@ class JsonApiException(Exception):

title = 'Unknown error'
status = '500'
source = ''
source = None

def __init__(self, detail, source=None, title=None, status=None, code=None, id_=None, links=None, meta=None):
"""Initialize a jsonapi exception
Expand All @@ -17,8 +17,6 @@ def __init__(self, detail, source=None, title=None, status=None, code=None, id_=
:param str detail: the detail of the error
"""
self.detail = detail
if source is not None:
self.source = source
self.source = source
self.code = code
self.id = id_
Expand Down
10 changes: 6 additions & 4 deletions tests/test_sqlalchemy_data_layer.py
Expand Up @@ -338,6 +338,7 @@ def test_add_pagination_links(app):
assert len(last_page_dict['page[number]']) == 1
assert last_page_dict['page[number]'][0] == '5'


def test_Node(person_model, person_schema, monkeypatch):
from copy import deepcopy
filt = {
Expand Down Expand Up @@ -376,8 +377,7 @@ def test_check_method_requirements(monkeypatch):
request = type('request', (object,), dict(method='GET'))
monkeypatch.setattr(flask_rest_jsonapi.decorators, 'request', request)
with pytest.raises(Exception):
flask_rest_jsonapi.\
decorators.check_method_requirements(lambda: 1)(self())
flask_rest_jsonapi.decorators.check_method_requirements(lambda: 1)(self())


def test_json_api_exception():
Expand Down Expand Up @@ -738,14 +738,16 @@ def test_single_accept_header(client, register_routes):
response = client.get('/persons', content_type='application/vnd.api+json', headers={'Accept': 'application/vnd.api+json'})
assert response.status_code == 200


def test_multiple_accept_header(client, register_routes):
with client:
response = client.get('/persons', content_type='application/vnd.api+json', headers={'Accept': '*/*, application/vnd.api+json'})
response = client.get('/persons', content_type='application/vnd.api+json', headers={'Accept': '*/*, application/vnd.api+json, application/vnd.api+json;q=0.9'})
assert response.status_code == 200


def test_wrong_accept_header(client, register_routes):
with client:
response = client.get('/persons', content_type='application/vnd.api+json', headers={'Accept': 'error'})
response = client.get('/persons', content_type='application/vnd.api+json', headers={'Accept': 'application/vnd.api+json;q=0.7, application/vnd.api+json;q=0.9'})
assert response.status_code == 406


Expand Down

0 comments on commit 9d533a2

Please sign in to comment.