Fixed #27622 -- Allowed test client to accept vendor tree JSON content types.
This commit is contained in:
parent
d20e046bbd
commit
0b5d4c49d6
|
@ -34,6 +34,8 @@ __all__ = ('Client', 'RedirectCycleError', 'RequestFactory', 'encode_file', 'enc
|
||||||
BOUNDARY = 'BoUnDaRyStRiNg'
|
BOUNDARY = 'BoUnDaRyStRiNg'
|
||||||
MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
|
MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
|
||||||
CONTENT_TYPE_RE = re.compile(r'.*; charset=([\w\d-]+);?')
|
CONTENT_TYPE_RE = re.compile(r'.*; charset=([\w\d-]+);?')
|
||||||
|
# JSON Vendor Tree spec: https://tools.ietf.org/html/rfc6838#section-3.2
|
||||||
|
JSON_CONTENT_TYPE_RE = re.compile(r'^application\/(vnd\..+\+)?json$')
|
||||||
|
|
||||||
|
|
||||||
class RedirectCycleError(Exception):
|
class RedirectCycleError(Exception):
|
||||||
|
@ -690,7 +692,7 @@ class Client(RequestFactory):
|
||||||
|
|
||||||
def _parse_json(self, response, **extra):
|
def _parse_json(self, response, **extra):
|
||||||
if not hasattr(response, '_json'):
|
if not hasattr(response, '_json'):
|
||||||
if 'application/json' not in response.get('Content-Type'):
|
if not JSON_CONTENT_TYPE_RE.match(response.get('Content-Type')):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'Content-Type header is "{0}", not "application/json"'
|
'Content-Type header is "{0}", not "application/json"'
|
||||||
.format(response.get('Content-Type'))
|
.format(response.get('Content-Type'))
|
||||||
|
|
|
@ -1209,6 +1209,12 @@ class RequestMethodStringDataTests(SimpleTestCase):
|
||||||
response = self.client.get('/json_response/')
|
response = self.client.get('/json_response/')
|
||||||
self.assertEqual(response.json(), {'key': 'value'})
|
self.assertEqual(response.json(), {'key': 'value'})
|
||||||
|
|
||||||
|
def test_json_vendor(self):
|
||||||
|
for content_type in ('application/vnd.api+json', 'application/vnd.api.foo+json'):
|
||||||
|
response = self.client.get('/json_response/', {'content_type': content_type})
|
||||||
|
self.assertEqual(response['Content-Type'], content_type)
|
||||||
|
self.assertEqual(response.json(), {'key': 'value'})
|
||||||
|
|
||||||
def test_json_multiple_access(self):
|
def test_json_multiple_access(self):
|
||||||
response = self.client.get('/json_response/')
|
response = self.client.get('/json_response/')
|
||||||
self.assertIs(response.json(), response.json())
|
self.assertIs(response.json(), response.json())
|
||||||
|
|
|
@ -106,7 +106,9 @@ def return_undecodable_binary(request):
|
||||||
|
|
||||||
|
|
||||||
def return_json_response(request):
|
def return_json_response(request):
|
||||||
return JsonResponse({'key': 'value'})
|
content_type = request.GET.get('content_type')
|
||||||
|
kwargs = {'content_type': content_type} if content_type else {}
|
||||||
|
return JsonResponse({'key': 'value'}, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def return_json_file(request):
|
def return_json_file(request):
|
||||||
|
|
Loading…
Reference in New Issue