From 3d2cae0896ee8026d1c2c5d31e4c4c8f74f2fef4 Mon Sep 17 00:00:00 2001 From: Collin Anderson Date: Sat, 3 Jan 2015 12:06:24 -0500 Subject: Fixed #24072 -- Added FileResponse for streaming binary files. --- tests/middleware/tests.py | 18 ++++++++++++++++-- tests/wsgi/tests.py | 22 ++++++++++++++++++++++ tests/wsgi/urls.py | 3 ++- 3 files changed, 40 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py index 22e2250e71..2b5b50f9e5 100644 --- a/tests/middleware/tests.py +++ b/tests/middleware/tests.py @@ -10,8 +10,8 @@ from unittest import skipIf from django.conf import settings from django.core import mail from django.http import ( - HttpRequest, HttpResponse, StreamingHttpResponse, HttpResponsePermanentRedirect, - HttpResponseRedirect, + HttpRequest, HttpResponse, StreamingHttpResponse, FileResponse, + HttpResponseRedirect, HttpResponsePermanentRedirect, ) from django.middleware.clickjacking import XFrameOptionsMiddleware from django.middleware.common import CommonMiddleware, BrokenLinkEmailsMiddleware @@ -624,6 +624,20 @@ class GZipMiddlewareTest(TestCase): self.assertEqual(r.get('Content-Encoding'), 'gzip') self.assertFalse(r.has_header('Content-Length')) + def test_compress_file_response(self): + """ + Tests that compression is performed on FileResponse. + """ + open_file = lambda: open(__file__, 'rb') + with open_file() as file1: + file_resp = FileResponse(file1) + file_resp['Content-Type'] = 'text/html; charset=UTF-8' + r = GZipMiddleware().process_response(self.req, file_resp) + with open_file() as file2: + self.assertEqual(self.decompress(b''.join(r)), file2.read()) + self.assertEqual(r.get('Content-Encoding'), 'gzip') + self.assertIsNot(r.file_to_stream, file1) + def test_compress_non_200_response(self): """ Tests that compression is performed on responses with a status other than 200. diff --git a/tests/wsgi/tests.py b/tests/wsgi/tests.py index e8f29c60d8..13760d0b61 100644 --- a/tests/wsgi/tests.py +++ b/tests/wsgi/tests.py @@ -51,6 +51,28 @@ class WSGITest(TestCase): bytes(response), b"Content-Type: text/html; charset=utf-8\r\n\r\nHello World!") + def test_file_wrapper(self): + """ + Verify that FileResponse uses wsgi.file_wrapper. + """ + class FileWrapper(object): + def __init__(self, filelike, blksize=8192): + filelike.close() + application = get_wsgi_application() + environ = RequestFactory()._base_environ( + PATH_INFO='/file/', + REQUEST_METHOD='GET', + **{'wsgi.file_wrapper': FileWrapper} + ) + response_data = {} + + def start_response(status, headers): + response_data['status'] = status + response_data['headers'] = headers + response = application(environ, start_response) + self.assertEqual(response_data['status'], '200 OK') + self.assertIsInstance(response, FileWrapper) + class GetInternalWSGIApplicationTest(unittest.TestCase): @override_settings(WSGI_APPLICATION="wsgi.wsgi.application") diff --git a/tests/wsgi/urls.py b/tests/wsgi/urls.py index 3ed0d5e8a8..e7505c717b 100644 --- a/tests/wsgi/urls.py +++ b/tests/wsgi/urls.py @@ -1,5 +1,5 @@ from django.conf.urls import url -from django.http import HttpResponse +from django.http import HttpResponse, FileResponse def helloworld(request): @@ -7,4 +7,5 @@ def helloworld(request): urlpatterns = [ url("^$", helloworld), + url(r'^file/$', lambda x: FileResponse(open(__file__, 'rb'))), ] -- cgit v1.3