From 544b2ef29f0f2577912f88cf746ae0ca5877b5f8 Mon Sep 17 00:00:00 2001 From: roboslone Date: Tue, 27 Dec 2016 07:29:31 +0300 Subject: [PATCH] Fixed #27640 -- Fixed HttpResponse's __repr__() without a 'Content-Type' header. --- django/http/response.py | 16 ++++++++++------ tests/httpwrappers/tests.py | 9 +++++++++ tests/responses/tests.py | 5 +++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/django/http/response.py b/django/http/response.py index 48b4b525b9..e9e283e840 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -107,6 +107,10 @@ class HttpResponseBase(six.Iterator): else: __str__ = serialize_headers + @property + def _content_type_for_repr(self): + return ', "%s"' % self['Content-Type'] if 'Content-Type' in self else '' + def _convert_to_charset(self, value, charset, mime_encode=False): """Converts headers key/value to ascii/latin-1 native strings. @@ -299,10 +303,10 @@ class HttpResponse(HttpResponseBase): self.content = content def __repr__(self): - return '<%(cls)s status_code=%(status_code)d, "%(content_type)s">' % { + return '<%(cls)s status_code=%(status_code)d%(content_type)s>' % { 'cls': self.__class__.__name__, 'status_code': self.status_code, - 'content_type': self['Content-Type'], + 'content_type': self._content_type_for_repr, } def serialize(self): @@ -429,10 +433,10 @@ class HttpResponseRedirectBase(HttpResponse): url = property(lambda self: self['Location']) def __repr__(self): - return '<%(cls)s status_code=%(status_code)d, "%(content_type)s", url="%(url)s">' % { + return '<%(cls)s status_code=%(status_code)d%(content_type)s, url="%(url)s">' % { 'cls': self.__class__.__name__, 'status_code': self.status_code, - 'content_type': self['Content-Type'], + 'content_type': self._content_type_for_repr, 'url': self.url, } @@ -479,10 +483,10 @@ class HttpResponseNotAllowed(HttpResponse): self['Allow'] = ', '.join(permitted_methods) def __repr__(self): - return '<%(cls)s [%(methods)s] status_code=%(status_code)d, "%(content_type)s">' % { + return '<%(cls)s [%(methods)s] status_code=%(status_code)d%(content_type)s>' % { 'cls': self.__class__.__name__, 'status_code': self.status_code, - 'content_type': self['Content-Type'], + 'content_type': self._content_type_for_repr, 'methods': self['Allow'], } diff --git a/tests/httpwrappers/tests.py b/tests/httpwrappers/tests.py index be136f3bb7..3ab21751f5 100644 --- a/tests/httpwrappers/tests.py +++ b/tests/httpwrappers/tests.py @@ -536,6 +536,10 @@ class HttpResponseSubclassesTests(SimpleTestCase): response.content = "Hello dear" self.assertNotIn('content-type', response) + def test_not_modified_repr(self): + response = HttpResponseNotModified() + self.assertEqual(repr(response), '') + def test_not_allowed(self): response = HttpResponseNotAllowed(['GET']) self.assertEqual(response.status_code, 405) @@ -548,6 +552,11 @@ class HttpResponseSubclassesTests(SimpleTestCase): expected = '' self.assertEqual(repr(response), expected) + def test_not_allowed_repr_no_content_type(self): + response = HttpResponseNotAllowed(('GET', 'POST')) + del response['Content-Type'] + self.assertEqual(repr(response), '') + class JsonResponseTests(SimpleTestCase): def test_json_response_non_ascii(self): diff --git a/tests/responses/tests.py b/tests/responses/tests.py index 668d5420f0..5dba0e04cb 100644 --- a/tests/responses/tests.py +++ b/tests/responses/tests.py @@ -136,6 +136,11 @@ class HttpResponseTests(SimpleTestCase): expected = '' self.assertEqual(repr(response), expected) + def test_repr_no_content_type(self): + response = HttpResponse(status=204) + del response['Content-Type'] + self.assertEqual(repr(response), '') + def test_wrap_textiowrapper(self): content = "Café :)" r = HttpResponse()