1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #31789 -- Added a new headers interface to HttpResponse.

This commit is contained in:
Tom Carrick
2020-07-14 13:32:24 +02:00
committed by Mariusz Felisiak
parent 71ae1ab012
commit bcc2befd0e
47 changed files with 385 additions and 256 deletions

View File

@@ -12,23 +12,26 @@ from django.test import SimpleTestCase
class FileResponseTests(SimpleTestCase):
def test_file_from_disk_response(self):
response = FileResponse(open(__file__, 'rb'))
self.assertEqual(response['Content-Length'], str(os.path.getsize(__file__)))
self.assertIn(response['Content-Type'], ['text/x-python', 'text/plain'])
self.assertEqual(response['Content-Disposition'], 'inline; filename="test_fileresponse.py"')
self.assertEqual(response.headers['Content-Length'], str(os.path.getsize(__file__)))
self.assertIn(response.headers['Content-Type'], ['text/x-python', 'text/plain'])
self.assertEqual(
response.headers['Content-Disposition'],
'inline; filename="test_fileresponse.py"',
)
response.close()
def test_file_from_buffer_response(self):
response = FileResponse(io.BytesIO(b'binary content'))
self.assertEqual(response['Content-Length'], '14')
self.assertEqual(response['Content-Type'], 'application/octet-stream')
self.assertEqual(response.headers['Content-Length'], '14')
self.assertEqual(response.headers['Content-Type'], 'application/octet-stream')
self.assertFalse(response.has_header('Content-Disposition'))
self.assertEqual(list(response), [b'binary content'])
def test_file_from_buffer_unnamed_attachment(self):
response = FileResponse(io.BytesIO(b'binary content'), as_attachment=True)
self.assertEqual(response['Content-Length'], '14')
self.assertEqual(response['Content-Type'], 'application/octet-stream')
self.assertEqual(response['Content-Disposition'], 'attachment')
self.assertEqual(response.headers['Content-Length'], '14')
self.assertEqual(response.headers['Content-Type'], 'application/octet-stream')
self.assertEqual(response.headers['Content-Disposition'], 'attachment')
self.assertEqual(list(response), [b'binary content'])
@skipIf(sys.platform == 'win32', "Named pipes are Unix-only.")
@@ -47,9 +50,12 @@ class FileResponseTests(SimpleTestCase):
def test_file_from_disk_as_attachment(self):
response = FileResponse(open(__file__, 'rb'), as_attachment=True)
self.assertEqual(response['Content-Length'], str(os.path.getsize(__file__)))
self.assertIn(response['Content-Type'], ['text/x-python', 'text/plain'])
self.assertEqual(response['Content-Disposition'], 'attachment; filename="test_fileresponse.py"')
self.assertEqual(response.headers['Content-Length'], str(os.path.getsize(__file__)))
self.assertIn(response.headers['Content-Type'], ['text/x-python', 'text/plain'])
self.assertEqual(
response.headers['Content-Disposition'],
'attachment; filename="test_fileresponse.py"',
)
response.close()
def test_compressed_response(self):
@@ -67,7 +73,7 @@ class FileResponseTests(SimpleTestCase):
with self.subTest(ext=extension):
with tempfile.NamedTemporaryFile(suffix=extension) as tmp:
response = FileResponse(tmp)
self.assertEqual(response['Content-Type'], mimetype)
self.assertEqual(response.headers['Content-Type'], mimetype)
self.assertFalse(response.has_header('Content-Encoding'))
def test_unicode_attachment(self):
@@ -75,8 +81,11 @@ class FileResponseTests(SimpleTestCase):
ContentFile(b'binary content', name="祝您平安.odt"), as_attachment=True,
content_type='application/vnd.oasis.opendocument.text',
)
self.assertEqual(response['Content-Type'], 'application/vnd.oasis.opendocument.text')
self.assertEqual(
response['Content-Disposition'],
response.headers['Content-Type'],
'application/vnd.oasis.opendocument.text',
)
self.assertEqual(
response.headers['Content-Disposition'],
"attachment; filename*=utf-8''%E7%A5%9D%E6%82%A8%E5%B9%B3%E5%AE%89.odt"
)