diff options
author | Andy McKay <amckay@mozilla.com> | 2015-05-08 22:33:26 -0700 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2015-05-25 10:42:30 -0400 |
commit | 4525a0c4669dccb3a4c5d865184f8bb8e4a89ec8 (patch) | |
tree | f54f254b0043ffbb78e1f7a81f27c705650254c8 /tests/test_client_regress | |
parent | 46ce72e8d2588bf62e4d4e7f08019e7ad2ef0fe6 (diff) | |
download | django-4525a0c4669dccb3a4c5d865184f8bb8e4a89ec8.tar.gz |
Fixed #24773 -- Added a json() method on test client responses.
Diffstat (limited to 'tests/test_client_regress')
-rw-r--r-- | tests/test_client_regress/tests.py | 10 | ||||
-rw-r--r-- | tests/test_client_regress/urls.py | 1 | ||||
-rw-r--r-- | tests/test_client_regress/views.py | 6 |
3 files changed, 16 insertions, 1 deletions
diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index f47ae10b56..efc44289cd 100644 --- a/tests/test_client_regress/tests.py +++ b/tests/test_client_regress/tests.py @@ -1270,6 +1270,16 @@ class RequestMethodStringDataTests(SimpleTestCase): response = self.client.head('/body/', data='', content_type='application/json') self.assertEqual(response.content, b'') + def test_json(self): + response = self.client.get('/json_response/') + self.assertEqual(response.json(), {'key': 'value'}) + + def test_json_wrong_header(self): + response = self.client.get('/body/') + msg = 'Content-Type header is "text/html; charset=utf-8", not "application/json"' + with self.assertRaisesMessage(ValueError, msg): + self.assertEqual(response.json(), {'key': 'value'}) + @override_settings(ROOT_URLCONF='test_client_regress.urls',) class QueryStringTests(SimpleTestCase): diff --git a/tests/test_client_regress/urls.py b/tests/test_client_regress/urls.py index fd7c131797..9821ea910d 100644 --- a/tests/test_client_regress/urls.py +++ b/tests/test_client_regress/urls.py @@ -30,6 +30,7 @@ urlpatterns = [ url(r'^request_methods/$', views.request_methods_view), url(r'^check_unicode/$', views.return_unicode), url(r'^check_binary/$', views.return_undecodable_binary), + url(r'^json_response/$', views.return_json_response), url(r'^parse_unicode_json/$', views.return_json_file), url(r'^check_headers/$', views.check_headers), url(r'^check_headers_redirect/$', RedirectView.as_view(url='/check_headers/')), diff --git a/tests/test_client_regress/views.py b/tests/test_client_regress/views.py index 6046564ee4..d21448cba1 100644 --- a/tests/test_client_regress/views.py +++ b/tests/test_client_regress/views.py @@ -3,7 +3,7 @@ import json from django.conf import settings from django.contrib.auth.decorators import login_required from django.core.serializers.json import DjangoJSONEncoder -from django.http import HttpResponse, HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect, JsonResponse from django.shortcuts import render_to_response from django.template import RequestContext from django.template.loader import render_to_string @@ -108,6 +108,10 @@ def return_undecodable_binary(request): ) +def return_json_response(request): + return JsonResponse({'key': 'value'}) + + def return_json_file(request): "A view that parses and returns a JSON string as a file." match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE']) |