-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #537 from alphagov/ris-dm-gzip-middleware
add DMGzipMiddleware, a thin wrapper around Flask_gzip adding header-controllability & instrumentation
- Loading branch information
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
from .flask_init import init_app, init_manager | ||
|
||
|
||
__version__ = '48.5.0' | ||
__version__ = '48.6.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from dmutils.flask import DMGzipMiddleware | ||
|
||
|
||
class TestDMGzipMiddleware: | ||
def test_with_compression_safe_header(self, app): | ||
DMGzipMiddleware(app, compress_by_default=False) | ||
|
||
@app.route('/') | ||
def some_route(): | ||
return "!" * 9000, 200, {"X-Compression-Safe": "1"} | ||
|
||
response = app.test_client().get('/', headers={"Accept-Encoding": "gzip"}) | ||
assert response.status_code == 200 | ||
assert response.headers["Content-Encoding"] == "gzip" | ||
assert len(response.get_data()) < 9000 | ||
assert "X-Compression-Safe" not in response.headers | ||
|
||
def test_with_compression_unsafe_header(self, app): | ||
DMGzipMiddleware(app, compress_by_default=False) | ||
|
||
@app.route('/') | ||
def some_route(): | ||
return "!" * 9000, 200, {"X-Compression-Safe": "0"} | ||
|
||
response = app.test_client().get('/', headers={"Accept-Encoding": "gzip"}) | ||
assert response.status_code == 200 | ||
assert response.headers.get("Content-Encoding") != "gzip" | ||
assert len(response.get_data()) == 9000 | ||
assert "X-Compression-Safe" not in response.headers | ||
|
||
def test_with_compression_default_false(self, app): | ||
DMGzipMiddleware(app) | ||
|
||
@app.route('/') | ||
def some_route(): | ||
return "!" * 9000, 200 | ||
|
||
response = app.test_client().get('/', headers={"Accept-Encoding": "gzip"}) | ||
assert response.status_code == 200 | ||
assert response.headers.get("Content-Encoding") != "gzip" | ||
assert len(response.get_data()) == 9000 | ||
assert "X-Compression-Safe" not in response.headers | ||
|
||
def test_with_compression_default_true(self, app): | ||
DMGzipMiddleware(app, compress_by_default=True) | ||
|
||
@app.route('/') | ||
def some_route(): | ||
return "!" * 9000, 200 | ||
|
||
response = app.test_client().get('/', headers={"Accept-Encoding": "gzip"}) | ||
assert response.status_code == 200 | ||
assert response.headers["Content-Encoding"] == "gzip" | ||
assert len(response.get_data()) < 9000 | ||
assert "X-Compression-Safe" not in response.headers |