Refs #24121 -- Added __repr__() to StreamingHttpResponse and subclasses.

This commit is contained in:
Nicolas Restrepo 2021-06-17 07:04:04 -04:00 committed by Mariusz Felisiak
parent 7a9745fed4
commit bbb3965826
3 changed files with 21 additions and 0 deletions

View File

@ -402,6 +402,13 @@ class StreamingHttpResponse(HttpResponseBase):
# See the `streaming_content` property methods. # See the `streaming_content` property methods.
self.streaming_content = streaming_content self.streaming_content = streaming_content
def __repr__(self):
return '<%(cls)s status_code=%(status_code)d%(content_type)s>' % {
'cls': self.__class__.__qualname__,
'status_code': self.status_code,
'content_type': self._content_type_for_repr,
}
@property @property
def content(self): def content(self):
raise AttributeError( raise AttributeError(

View File

@ -663,6 +663,13 @@ class StreamingHttpResponseTests(SimpleTestCase):
r = StreamingHttpResponse(iter(['hello', 'world'])) r = StreamingHttpResponse(iter(['hello', 'world']))
self.assertEqual(r.getvalue(), b'helloworld') self.assertEqual(r.getvalue(), b'helloworld')
def test_repr(self):
r = StreamingHttpResponse(iter(['hello', 'café']))
self.assertEqual(
repr(r),
'<StreamingHttpResponse status_code=200, "text/html; charset=utf-8">',
)
class FileCloseTests(SimpleTestCase): class FileCloseTests(SimpleTestCase):

View File

@ -89,3 +89,10 @@ class FileResponseTests(SimpleTestCase):
response.headers['Content-Disposition'], response.headers['Content-Disposition'],
"attachment; filename*=utf-8''%E7%A5%9D%E6%82%A8%E5%B9%B3%E5%AE%89.odt" "attachment; filename*=utf-8''%E7%A5%9D%E6%82%A8%E5%B9%B3%E5%AE%89.odt"
) )
def test_repr(self):
response = FileResponse(io.BytesIO(b'binary content'))
self.assertEqual(
repr(response),
'<FileResponse status_code=200, "application/octet-stream">',
)