1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #27516 -- Made test client's response.json() cache the parsed JSON.

This commit is contained in:
Adam Chainz 2016-11-21 13:14:03 +00:00 committed by Tim Graham
parent 0783aa7deb
commit 19e2114634
2 changed files with 12 additions and 6 deletions

View File

@ -683,12 +683,14 @@ class Client(RequestFactory):
self.cookies = SimpleCookie()
def _parse_json(self, response, **extra):
if 'application/json' not in response.get('Content-Type'):
raise ValueError(
'Content-Type header is "{0}", not "application/json"'
.format(response.get('Content-Type'))
)
return json.loads(response.content.decode(), **extra)
if not hasattr(response, '_json'):
if 'application/json' not in 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(), **extra)
return response._json
def _handle_redirects(self, response, **extra):
"Follows any redirects by requesting responses from the server using GET."

View File

@ -1209,6 +1209,10 @@ class RequestMethodStringDataTests(SimpleTestCase):
response = self.client.get('/json_response/')
self.assertEqual(response.json(), {'key': 'value'})
def test_json_multiple_access(self):
response = self.client.get('/json_response/')
self.assertIs(response.json(), response.json())
def test_json_wrong_header(self):
response = self.client.get('/body/')
msg = 'Content-Type header is "text/html; charset=utf-8", not "application/json"'