1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #35727 -- Added HttpResponse.text property.

Signed-off-by: SaJH <wogur981208@gmail.com>
This commit is contained in:
SaJH
2024-09-11 21:23:23 +09:00
committed by Sarah Boyce
parent ec7d69035a
commit 4a685bc0dc
4 changed files with 48 additions and 0 deletions

View File

@@ -530,6 +530,22 @@ class HttpResponseTests(SimpleTestCase):
headers={"Content-Type": "text/csv"},
)
def test_text_updates_when_content_updates(self):
response = HttpResponse("Hello, world!")
self.assertEqual(response.text, "Hello, world!")
response.content = "Updated content"
self.assertEqual(response.text, "Updated content")
def test_text_charset(self):
for content_type, content in [
(None, b"Ol\xc3\xa1 Mundo"),
("text/plain; charset=utf-8", b"Ol\xc3\xa1 Mundo"),
("text/plain; charset=iso-8859-1", b"Ol\xe1 Mundo"),
]:
with self.subTest(content_type=content_type):
response = HttpResponse(content, content_type=content_type)
self.assertEqual(response.text, "Olá Mundo")
class HttpResponseSubclassesTests(SimpleTestCase):
def test_redirect(self):
@@ -756,6 +772,13 @@ class StreamingHttpResponseTests(SimpleTestCase):
with self.assertWarnsMessage(Warning, msg):
self.assertEqual(b"hello", await anext(aiter(r)))
def test_text_attribute_error(self):
r = StreamingHttpResponse(iter(["hello", "world"]))
msg = "This %s instance has no `text` attribute." % r.__class__.__name__
with self.assertRaisesMessage(AttributeError, msg):
r.text
class FileCloseTests(SimpleTestCase):
def setUp(self):