Skip to content

Commit 079b215

Browse files
christopherpickeringalexprengere
authored andcommitted
added flag
1 parent 8bc8912 commit 079b215

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*.sw[klmnop]
44
# python
55
*.py[co~]
6+
.python-version
67
# Ignoring build dir
78
/build
89
/dist

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,4 @@ Within your Flask application's settings you can provide the following settings
114114
| `COMPRESS_CACHE_BACKEND` | Specified the backend for storing the cached response data. | `None` |
115115
| `COMPRESS_REGISTER` | Specifies if compression should be automatically registered. | `True` |
116116
| `COMPRESS_ALGORITHM` | Supported compression algorithms. | `['br', 'gzip', 'deflate']` |
117+
| `COMPRESS_STREAMS` | Compress content streams. | `True` |

flask_compress/flask_compress.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def init_app(self, app):
7777
('COMPRESS_CACHE_KEY', None),
7878
('COMPRESS_CACHE_BACKEND', None),
7979
('COMPRESS_REGISTER', True),
80+
('COMPRESS_STREAMS', True),
8081
('COMPRESS_ALGORITHM', ['br', 'gzip', 'deflate']),
8182
]
8283

@@ -178,7 +179,7 @@ def after_request(self, response):
178179
response.mimetype not in app.config["COMPRESS_MIMETYPES"] or
179180
response.status_code < 200 or
180181
response.status_code >= 300 or
181-
response.is_streamed or
182+
(response.is_streamed and app.config["COMPRESS_STREAMS"] is False)or
182183
"Content-Encoding" in response.headers or
183184
(response.content_length is not None and
184185
response.content_length < app.config["COMPRESS_MIN_SIZE"])):

tests/test_flask_compress.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def test_block_size_default(self):
5151
""" Tests COMPRESS_BR_BLOCK default value is correctly set. """
5252
self.assertEqual(self.app.config['COMPRESS_BR_BLOCK'], 0)
5353

54+
def test_stream(self):
55+
""" Tests COMPRESS_STREAMS default value is correctly set. """
56+
self.assertEqual(self.app.config['COMPRESS_STREAMS'], True)
57+
5458
class InitTests(unittest.TestCase):
5559
def setUp(self):
5660
self.app = Flask(__name__)
@@ -353,13 +357,12 @@ class StreamTests(unittest.TestCase):
353357
def setUp(self):
354358
self.app = Flask(__name__)
355359
self.app.testing = True
360+
self.app.config["COMPRESS_STREAMS"] = False
356361

357362
self.file_path = os.path.join(os.getcwd(), 'tests', 'templates',
358363
'large.html')
359364
self.file_size = os.path.getsize(self.file_path)
360365

361-
Compress(self.app)
362-
363366
@self.app.route('/stream/large')
364367
def stream():
365368
def _stream():
@@ -370,6 +373,7 @@ def _stream():
370373

371374
def test_no_compression_stream(self):
372375
""" Tests compression is skipped when response is streamed"""
376+
Compress(self.app)
373377
client = self.app.test_client()
374378
for algorithm in ('gzip', 'deflate', 'br', ''):
375379
headers = [('Accept-Encoding', algorithm)]
@@ -378,6 +382,17 @@ def test_no_compression_stream(self):
378382
self.assertEqual(response.is_streamed, True)
379383
self.assertEqual(self.file_size, len(response.data))
380384

385+
def test_disabled_stream(self):
386+
"""Test that stream compression can be disabled."""
387+
Compress(self.app)
388+
self.app.config["COMPRESS_STREAMS"] = True
389+
client = self.app.test_client()
390+
for algorithm in ('gzip', 'deflate', 'br'):
391+
headers = [('Accept-Encoding', algorithm)]
392+
response = client.get('/stream/large', headers=headers)
393+
self.assertIn('Content-Encoding', response.headers)
394+
self.assertGreater(self.file_size, len(response.data))
395+
381396

382397
if __name__ == '__main__':
383398
unittest.main()

0 commit comments

Comments
 (0)