mirror of
https://github.com/ansible/awx.git
synced 2026-03-03 17:51:06 -03:30
Merge pull request #2400 from YunfanZhang42/fix_content_type
Handle exception with invalid HTTP content_type.
This commit is contained in:
@@ -23,7 +23,7 @@ from django.utils.translation import ugettext_lazy as _
|
|||||||
from django.contrib.auth import views as auth_views
|
from django.contrib.auth import views as auth_views
|
||||||
|
|
||||||
# Django REST Framework
|
# Django REST Framework
|
||||||
from rest_framework.exceptions import PermissionDenied, AuthenticationFailed, ParseError, NotAcceptable
|
from rest_framework.exceptions import PermissionDenied, AuthenticationFailed, ParseError, NotAcceptable, UnsupportedMediaType
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
@@ -197,6 +197,10 @@ class APIView(views.APIView):
|
|||||||
except (PermissionDenied, ParseError) as exc:
|
except (PermissionDenied, ParseError) as exc:
|
||||||
request.drf_request_user = None
|
request.drf_request_user = None
|
||||||
self.__init_request_error__ = exc
|
self.__init_request_error__ = exc
|
||||||
|
except UnsupportedMediaType as exc:
|
||||||
|
exc.detail = _('You did not use correct content_type in your HTTP request. \
|
||||||
|
If you are using our REST API, the content_type must be application/json')
|
||||||
|
self.__init_request_error__ = exc
|
||||||
return drf_request
|
return drf_request
|
||||||
|
|
||||||
def finalize_response(self, request, response, *args, **kwargs):
|
def finalize_response(self, request, response, *args, **kwargs):
|
||||||
|
|||||||
@@ -107,3 +107,13 @@ def test_filterable_fields(options, instance, admin_user):
|
|||||||
assert filterable_info['filterable'] is True
|
assert filterable_info['filterable'] is True
|
||||||
|
|
||||||
assert 'filterable' not in non_filterable_info
|
assert 'filterable' not in non_filterable_info
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_handle_content_type(post, admin):
|
||||||
|
''' Tower should return 415 when wrong content type is in HTTP requests '''
|
||||||
|
post(reverse('api:project_list'),
|
||||||
|
{'name': 't', 'organization': None},
|
||||||
|
admin,
|
||||||
|
content_type='text/html',
|
||||||
|
expect=415)
|
||||||
|
|||||||
Reference in New Issue
Block a user