1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #30550 -- Fixed decoding of non-UTF-8 bytes objects in response.json().

This commit is contained in:
Jon Dufresne 2019-06-06 21:40:15 -07:00 committed by Mariusz Felisiak
parent 498092377b
commit d6ea4898c4
4 changed files with 11 additions and 1 deletions

View File

@ -650,7 +650,7 @@ class Client(RequestFactory):
'Content-Type header is "{0}", not "application/json"'
.format(response.get('Content-Type'))
)
response._json = json.loads(response.content.decode(), **extra)
response._json = json.loads(response.content.decode(response.charset), **extra)
return response._json
def _handle_redirects(self, response, data='', content_type='', **extra):

View File

@ -1204,6 +1204,11 @@ class RequestMethodStringDataTests(SimpleTestCase):
response = self.client.get('/json_response/')
self.assertEqual(response.json(), {'key': 'value'})
def test_json_charset(self):
response = self.client.get('/json_response_latin1/')
self.assertEqual(response.charset, 'latin1')
self.assertEqual(response.json(), {'a': 'Å'})
def test_json_structured_suffixes(self):
valid_types = (
'application/vnd.api+json',

View File

@ -31,6 +31,7 @@ urlpatterns = [
path('check_unicode/', views.return_unicode),
path('check_binary/', views.return_undecodable_binary),
path('json_response/', views.return_json_response),
path('json_response_latin1/', views.return_json_response_latin1),
path('parse_encoded_text/', views.return_text_file),
path('check_headers/', views.check_headers),
path('check_headers_redirect/', RedirectView.as_view(url='/check_headers/')),

View File

@ -109,6 +109,10 @@ def return_json_response(request):
return JsonResponse({'key': 'value'}, **kwargs)
def return_json_response_latin1(request):
return HttpResponse(b'{"a":"\xc5"}', content_type='application/json; charset=latin1')
def return_text_file(request):
"A view that parses and returns text as a file."
match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])