mirror of
https://github.com/ansible/awx.git
synced 2026-01-13 11:00:03 -03:30
Merge pull request #2400 from YunfanZhang42/fix_content_type
Handle exception with invalid HTTP content_type.
This commit is contained in:
commit
e9a2100a62
@ -23,7 +23,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from django.contrib.auth import views as auth_views
|
||||
|
||||
# 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.response import Response
|
||||
from rest_framework import status
|
||||
@ -197,6 +197,10 @@ class APIView(views.APIView):
|
||||
except (PermissionDenied, ParseError) as exc:
|
||||
request.drf_request_user = None
|
||||
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
|
||||
|
||||
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' 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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user