summaryrefslogtreecommitdiff
path: root/tests/responses
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2018-05-15 18:12:11 +0200
committerGitHub <noreply@github.com>2018-05-15 18:12:11 +0200
commita177f854c34718e473bcd0a2dc6c4fd935c8e327 (patch)
treed15e436c26edfd2037972c48095b4bcd2ad48505 /tests/responses
parent2dcc5d629a6439b5547cdd6e67815cabf608fcd4 (diff)
Fixed #16470 -- Allowed FileResponse to auto-set some Content headers.
Thanks Simon Charette, Jon Dufresne, and Tim Graham for the reviews.
Diffstat (limited to 'tests/responses')
-rw-r--r--tests/responses/test_fileresponse.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/responses/test_fileresponse.py b/tests/responses/test_fileresponse.py
new file mode 100644
index 0000000000..d6e6535b92
--- /dev/null
+++ b/tests/responses/test_fileresponse.py
@@ -0,0 +1,73 @@
+import io
+import os
+import sys
+import tempfile
+from unittest import skipIf
+
+from django.core.files.base import ContentFile
+from django.http import FileResponse
+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'])
+ 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(list(response), [b'binary content'])
+
+ @skipIf(sys.platform == 'win32', "Named pipes are Unix-only.")
+ def test_file_from_named_pipe_response(self):
+ with tempfile.TemporaryDirectory() as temp_dir:
+ pipe_file = os.path.join(temp_dir, 'named_pipe')
+ os.mkfifo(pipe_file)
+ pipe_for_read = os.open(pipe_file, os.O_RDONLY | os.O_NONBLOCK)
+ with open(pipe_file, 'wb') as pipe_for_write:
+ pipe_for_write.write(b'binary content')
+
+ response = FileResponse(os.fdopen(pipe_for_read, mode='rb'))
+ self.assertEqual(list(response), [b'binary content'])
+ response.close()
+ self.assertFalse(response.has_header('Ĉontent-Length'))
+
+ 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"')
+ response.close()
+
+ def test_compressed_response(self):
+ """
+ If compressed responses are served with the uncompressed Content-Type
+ and a compression Content-Encoding, browsers might automatically
+ uncompress the file, which is most probably not wanted.
+ """
+ test_tuples = (
+ ('.tar.gz', 'application/gzip'),
+ ('.tar.bz2', 'application/x-bzip'),
+ ('.tar.xz', 'application/x-xz'),
+ )
+ for extension, mimetype in test_tuples:
+ with self.subTest(ext=extension):
+ with tempfile.NamedTemporaryFile(suffix=extension) as tmp:
+ response = FileResponse(tmp)
+ self.assertEqual(response['Content-Type'], mimetype)
+ self.assertFalse(response.has_header('Content-Encoding'))
+
+ def test_unicode_attachment(self):
+ response = FileResponse(
+ 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'],
+ "attachment; filename*=utf-8''%E7%A5%9D%E6%82%A8%E5%B9%B3%E5%AE%89.odt"
+ )