From 6a755441ed6ad51b631acfc8071948812a61fba8 Mon Sep 17 00:00:00 2001 From: Taoufik Date: Fri, 9 Aug 2019 17:23:06 +0200 Subject: [PATCH] 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 --- rest_framework/test.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rest_framework/test.py b/rest_framework/test.py index edacf0066..8d207e1ea 100644 --- a/rest_framework/test.py +++ b/rest_framework/test.py @@ -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.