|
1 | 1 | from inspect import iscoroutinefunction |
2 | 2 |
|
3 | | -from django.http import HttpRequest, HttpResponse |
| 3 | +from django.http import HttpRequest, HttpResponse, StreamingHttpResponse |
4 | 4 | from django.test import SimpleTestCase |
5 | 5 | from django.views.decorators.gzip import gzip_page |
6 | 6 |
|
@@ -44,3 +44,33 @@ async def async_view(request): |
44 | 44 | response = await async_view(request) |
45 | 45 | self.assertEqual(response.status_code, 200) |
46 | 46 | self.assertEqual(response.get("Content-Encoding"), "gzip") |
| 47 | + |
| 48 | + def test_streaming_response_yields_chunks_incrementally(self): |
| 49 | + @gzip_page |
| 50 | + def stream_view(request): |
| 51 | + return StreamingHttpResponse(self.content.encode() for _ in range(5)) |
| 52 | + |
| 53 | + request = HttpRequest() |
| 54 | + request.META["HTTP_ACCEPT_ENCODING"] = "gzip" |
| 55 | + response = stream_view(request) |
| 56 | + compressed_chunks = list(response) |
| 57 | + # Each input chunk should produce compressed output, not buffer |
| 58 | + # everything into a single chunk. |
| 59 | + self.assertGreater(len(compressed_chunks), 2) |
| 60 | + |
| 61 | + async def test_async_streaming_response_yields_chunks_incrementally(self): |
| 62 | + @gzip_page |
| 63 | + async def stream_view(request): |
| 64 | + async def content(): |
| 65 | + for _ in range(5): |
| 66 | + yield self.content.encode() |
| 67 | + |
| 68 | + return StreamingHttpResponse(content()) |
| 69 | + |
| 70 | + request = HttpRequest() |
| 71 | + request.META["HTTP_ACCEPT_ENCODING"] = "gzip" |
| 72 | + response = await stream_view(request) |
| 73 | + compressed_chunks = [chunk async for chunk in response] |
| 74 | + # Each input chunk should produce compressed output, not buffer |
| 75 | + # everything into a single chunk. |
| 76 | + self.assertGreater(len(compressed_chunks), 2) |
0 commit comments