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

Fixed #27640 -- Fixed HttpResponse's __repr__() without a 'Content-Type' header.

This commit is contained in:
roboslone 2016-12-27 07:29:31 +03:00 committed by Tim Graham
parent 4701abd56e
commit 544b2ef29f
3 changed files with 24 additions and 6 deletions

View File

@ -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'],
}

View File

@ -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), '<HttpResponseNotModified status_code=304>')
def test_not_allowed(self):
response = HttpResponseNotAllowed(['GET'])
self.assertEqual(response.status_code, 405)
@ -548,6 +552,11 @@ class HttpResponseSubclassesTests(SimpleTestCase):
expected = '<HttpResponseNotAllowed [GET, OPTIONS] status_code=405, "text/plain">'
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), '<HttpResponseNotAllowed [GET, POST] status_code=405>')
class JsonResponseTests(SimpleTestCase):
def test_json_response_non_ascii(self):

View File

@ -136,6 +136,11 @@ class HttpResponseTests(SimpleTestCase):
expected = '<HttpResponse status_code=201, "text/html; charset=utf-8">'
self.assertEqual(repr(response), expected)
def test_repr_no_content_type(self):
response = HttpResponse(status=204)
del response['Content-Type']
self.assertEqual(repr(response), '<HttpResponse status_code=204>')
def test_wrap_textiowrapper(self):
content = "Café :)"
r = HttpResponse()