Set _json_parse encoding to response.charset

* Otherwise it will raise a `UnicodeDecodeError`
* Tried to push a fix to django 1.11 but it doesn't receive bugfixes anymore
This commit is contained in:
Taoufik 2019-08-09 17:23:06 +02:00 committed by taoufik
parent b1c4d8b59b
commit 6a755441ed

View File

@ -11,6 +11,7 @@ from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.handlers.wsgi import WSGIHandler
from django.test import override_settings, testcases
from django.test.client import JSON_CONTENT_TYPE_RE
from django.test.client import Client as DjangoClient
from django.test.client import ClientHandler
from django.test.client import RequestFactory as DjangoRequestFactory
@ -20,6 +21,7 @@ from django.utils.http import urlencode
from rest_framework.compat import coreapi, requests
from rest_framework.settings import api_settings
from rest_framework.utils import json
def force_authenticate(request, user=None, token=None):
@ -267,6 +269,16 @@ class APIClient(APIRequestFactory, DjangoClient):
self.handler = ForceAuthClientHandler(enforce_csrf_checks)
self._credentials = {}
def _parse_json(self, response, **extra):
if not hasattr(response, '_json'):
if not JSON_CONTENT_TYPE_RE.match(response.get('Content-Type')):
raise ValueError(
'Content-Type header is "{0}", not "application/json"'
.format(response.get('Content-Type'))
)
response._json = json.loads(response.content.decode(response.charset), **extra)
return response._json
def credentials(self, **kwargs):
"""
Sets headers that will be used on every outgoing request.